移动器3:重叠时检测XY坐标

发布于 2025-01-25 23:04:03 字数 106 浏览 1 评论 0 原文

我正在尝试检测XY坐标在有对象(子弹)击中时的画布纹理(土地)的重叠。我可以通过使用:object.x and object.y找到对象的位置,但是位置不是画布纹理的重叠位置。 有人有想法吗?谢谢。

I am trying to detect the x y coordinate overlapping of a canvas texture (a land) when there is an object (a bullet) hitting it. I can find the position of the object by using: object.x and object.y, but the position is not the overlapping position of the canvas texture.
Does anyone have any ideas? Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无风消散 2025-02-01 23:04:03

在此处讨论一个可能的解决方案,如果您使用的是Arcade Physics ,请在此处讨论: https://phasser.discourse.group/t/pixelperfect-collision-collision-collision-orment-betest-betest-bet-------- not-pointer/6377

在此处使用一个工作演示:

要点是:使用物理重叠函数(文档),在 ProcessCallback function (所说的功能,说的功能如果重叠应触发),请检查“徽标”的像素(在您的情况下)是否可见 this.textures.getPixelalpha(x,y,y,logo”)= == 255 )。

根据您希望子弹重叠的方式,您必须调整 getPixelalpha 函数的位置(x,y)计算。在上述示例中,球/子弹的中心用于检测重叠。

  this.physics.add.overlap(
      bullet,
      land,
      function overlapCallback(bullet, land) {
          ...
      },
      function processCallback(bullet, land) {
         let x = Math.floor(ball.body.center.x - landTopLeft.x);
         let y = Math.floor(ball.body.center.y - landTopLeft.y); 
         return this.textures.getPixelAlpha(x, y, "land") === 255;
     });

A possible solution, if you are using arcade physics, is discussed here: https://phaser.discourse.group/t/pixelperfect-collision-or-hittest-between-object-and-any-coordinate-not-pointer/6377

With a working demo here: https://codepen.io/samme/pen/JjYYqaZ

The gist is: use the physics overlap function (documentation), and in the processCallback function (the function that, says if the overlap should trigger), check if the pixel of the "logo" (in your case the land), is visible or not this.textures.getPixelAlpha(x, y , "logo") === 255 (documenation).

Depending how you want the bullet to overlap, you would have to tweak the position (x, y) calculation for the getPixelAlpha function. In the above mentioned example the center of the ball/bullet is used to detect the overlap.

  this.physics.add.overlap(
      bullet,
      land,
      function overlapCallback(bullet, land) {
          ...
      },
      function processCallback(bullet, land) {
         let x = Math.floor(ball.body.center.x - landTopLeft.x);
         let y = Math.floor(ball.body.center.y - landTopLeft.y); 
         return this.textures.getPixelAlpha(x, y, "land") === 255;
     });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文