Javascript画布碰撞事件块移动

发布于 2024-12-28 04:37:00 字数 701 浏览 0 评论 0原文

为了检测一张图像何时在画布上另一张图像的范围内,我这样做:

if (herohitboxX <= (villageMain.x + 200) && villageMain.x <= (herohitboxX) && herohitboxY <= (villageMain.y + 200) && villageMain.y <= (herohitboxY) ) { console.log('inside bldg') }

这很有效,但我在弄清楚如何不让玩家走进建筑物/树/物体的同时又让他们仍然存在一个巨大的问题远离该物体。

我可以通过在触摸时移动它们来让玩家滑过物体,并阻止它们从第 1 个方向返回,但我无法阻止所有 4 个侧面的移动......

主要问题是它只检测它们是否重叠,但它不会告诉您玩家来自哪个方向。当玩家在建筑物内时,简单地否定玩家的移动方向是行不通的(如果他们按下右侧并触摸物体,则向左走,否则如果他们没有触摸并按下右侧,则向右走)。或者,如果他们向上走并触摸建筑物,请向下发送。不应该这样吗?

我唯一能想到的就是做 4 个细矩形/线代表周边的每一侧,然后不允许从它所面对的方向移动。但必须有一些更简单的方法!它是什么?

存储以前的位置并进行比较?现在并没有真正需要注意的移动方向,如果在关键侦听器中检测到您向上,它会移动背景图像并调整玩家精灵位置。有什么建议吗?

To detect when 1 image is within range of another image on the canvas I do:

if (herohitboxX <= (villageMain.x + 200) && villageMain.x <= (herohitboxX) && herohitboxY <= (villageMain.y + 200) && villageMain.y <= (herohitboxY) ) { console.log('inside bldg') }

which works great but I'm having a huge problem figuring out how to not let the player walk into the building/tree/object, but while letting them still move away from the object.

I can make the player slide through the object by moving them while touching, and block them from coming back from that 1 direction, but I can't block movement from all 4 sides....

The main problem is it only detects if they're overlapping, but it doesn't tell you what direction the player was coming from. Simply negating the players movement direction when they're inside the building didn't work(if they're pressing right and touching object, go left, else if they're not touching and pressing right, go right). Or if they were walking UP and touch the building, send DOWN. Shouldn't that work????

The only thing I can think of is to do 4 thin rectangles/lines representing each side of the perimeter, then just not allowing movement from the direction it's facing. But there has to be some easier way! What is it?

Store previous location and make a comparison?? Right now there isn't really a movement direction to look to, if you UP is detected in key listener, it moves the background image and adjusts the players sprite position. Any tips?

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

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

发布评论

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

评论(1

吹梦到西洲 2025-01-04 04:37:00

据推测,此代码发生在玩家移动或尝试移动期间。

确定两个对象的中心。如果玩家的中心试图远离另一个对象的中心,则允许移动发生,即使它们相交。

如果没有,请停止移动。

换句话说,你有玩家的中心和物体的中心。这使得 line1.

并且您有一个移动命令,可以为玩家的中心创建一个潜在的新位置(旧位置加上一些方向)。计算此值即可得到第 2 行。

现在,如果第二条线比第一条线长,则意味着玩家正在远离相交的对象,因此应该允许。也许他们在右边遇到了一个物体,他们想向上或向左移动。这两者都使 line2 比 line1 长。

仅当玩家试图缩短线路(通过直接向物体移动)时才应将其阻挡。


线条提示

假设玩家的中心是 px, py 并且假设对象的中心是 ox, oy。那么他们之间的距离就是:

var dx = px - ox;
var dy = py - oy;
var dist = Math.sqrt(dx * dx + dy * dy);

但是这里有一些有趣的事情。我们实际上并不需要知道距离,我们只需要将其与另一个距离进行比较。这意味着我们可以跳过(可能很慢的)Math.sqrt 调用,并将 line1 的平方距离与 line2 的平方距离进行比较。所以这是第 1 行:

var dx = px - ox;
var dy = py - oy;
var line1 = dx * dx + dy * dy;

对于第 2 行,我们需要知道如果移动命令成功,玩家会在哪里,我们称之为 newpx, newpy

var dx2 = newpx - ox;
var dy2 = newpy - oy;
var line2 = dx2 * dx2 + dy2 * dy2;

所以如果 line2 > line1,允许移动!否则,阻止移动发生。

请注意,所有这些都假设玩家一次只能沿一个基本方向移动(上、下、左、右),而不是右上。如果是这种情况,你就必须更加小心了。

Presumably this code happens during the player moving or trying to move.

Determine the center of both objects. If the player's center is attempting to move away from the other object's center, allow the move to take place even though they intersect.

If not, stop the move.

Put another way, you have player's center and object's center. This makes line1.

And you have a move command that would create a potential new place for the player's center (the old location plus some direction). Calculate this to get line2.

Now if the second line is longer than the first line, that means the player is moving away from the intersecting object and so it should be allowed. Maybe they are up against an object on their right and they want to move up or left. Both of these make line2 longer than line1.

It should only be blocked if the player is attempting to make the line shorter (by moving directly towards the object).


tips for lines

Suppose the player's center was px, py and suppose the object's center is ox, oy. Then the distance between them is:

var dx = px - ox;
var dy = py - oy;
var dist = Math.sqrt(dx * dx + dy * dy);

But there's something funny here. We don't actually need to know the distance, we just need to compare it to another distance. This means we can skip the (potentially slow) Math.sqrt call, and compared the squared distance of line1 to the squared distance of line2. So here's line1:

var dx = px - ox;
var dy = py - oy;
var line1 = dx * dx + dy * dy;

And for line2 we need to know where the player would be if the move command was successful, we call that newpx, newpy.

var dx2 = newpx - ox;
var dy2 = newpy - oy;
var line2 = dx2 * dx2 + dy2 * dy2;

So if line2 > line1, allow the move! Otherwise, block the move from occurring.

note that all of this assumes that the player can only move in one cardinal direction at a time (up, down, left, right) and not up-right. If that is the case you'll have to be a little more careful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文