精灵与线的碰撞

发布于 2024-08-29 09:59:30 字数 83 浏览 2 评论 0原文

如果我有一个精灵,我将如何检查两点之间的碰撞?例如,在我正在制作的游戏中,我想绘制我的精灵碰撞的多条线。我认为如果我有很多平台,这比其他碰撞系统更灵活。

If I have a sprite, how would I check collision between two points? For example, in a game I am making, I would like to draw multiple lines that my sprite collides against. I'm thinking that this is more flexible than other collision systems if I had a lot of platforms.

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

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

发布评论

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

评论(1

与之呼应 2024-09-05 09:59:30

一些简单的逻辑可以帮助减少浪费的计算,即如果精灵的顶部低于这两个点,则不会发生碰撞。类似于简单的边界框碰撞检查。

完成此操作后,我建议您为您的线制定一个“公式”,然后检查精灵的哪些角位于该线的上方或下方。如果它们不都位于同一侧,就会发生碰撞。

例如,如果您的行是 y=x/2+2,从 x=-20 开始,到 x=20 结束,并且您的方形精灵 3 宽/高位于 (3,-1)那么你就有了 spirte 的四个点 (3,-1)(3,2)(6,2)(6,-1)。您计算出这两个 x 位置处的线的 y 值,这将在 x=3 处给出 y=3.5,在 x=6 处给出 y=5,所有这些 y 值都大于立方体的 y 值,因此立方体位于该线下方。

Some simple logic can help reduce wasted calculation, ie if the top of the sprite is lower then both of the points, you can't have a collision. Sort of a simple bounding box collision check.

Once you have done that, I would suggest that you get a 'formula' for your line, then check which of the corners of your sprite lay above or below that line. If they do not all lie on the same side, you have a collision.

for instance, if your line was of y=x/2+2, starting at x=-20 ending at x=20 and you had square sprite 3 wide/high at (3,-1) then you have the four points of your spirte (3,-1)(3,2)(6,2)(6,-1). You work out the y value of the line at those two x positions, which give you y=3.5 at x=3 and y=5 at x=6 all of those y values are greater then the y values of the cube, thus the cube is below the line.

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