碰撞时物体穿墙

发布于 2024-12-01 11:40:37 字数 326 浏览 1 评论 0原文

我正在制作一个使用非常简单的碰撞检测的游戏。我没有使用 box 2D,因为它太过分了。基本上,它是乒乓球和足球的混合体。当球获得速度并且具有非常高的速度时,它最终会穿过它应该碰撞的墙壁。该代码适用于慢速和常规速度,但不适用于非常快的运动。

这是我的代码的片段:

pos.x 是一个向量,它保存我的球的 x 位置。

if (pos.x - radius < wallLeft)
{
        pos.x = wallLeft + radius;
        vel.x *= -1;
}

我可以做些什么来改善这一点? 谢谢

I'm making a game which uses very simple collision detection. I'm not using box 2D because it's an overkill. Basically, it's a mix of Pong and fooseball. As the ball gains speed and has a very high velocity it ends up going through the wall it's supposed to collide with. The code works with slow and regular speeds, but not with very fast motion.

This is a snipet of my code:

pos.x is a vector which holds the x position of my ball.

if (pos.x - radius < wallLeft)
{
        pos.x = wallLeft + radius;
        vel.x *= -1;
}

What could i do to improve this?
thanks

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

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

发布评论

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

评论(3

痴意少年 2024-12-08 11:40:37

尝试增加 wallLeft 一点,这样球的速度永远不会大于 wallLeft,似乎在你的球低于 0 后它会出现故障(或者你有一些我不知道的代码),不熟悉框架或其余代码如何工作,但这是解决它的最简单方法。如果你不想这样做,可能会有一个代码在球的 x 小于 0 的情况下执行某些操作,并且你必须使其更宽松一点,也许可以这样做,以便如果球的 x 小于 0小于-50,或类似的值(调整这个数字直到它起作用)

Try increasing wallLeft a bit, so that the balls speed is never greater than wallLeft, it seems that after your ball goes below 0 it glitches (or you have some code for that that I don't know), not familiar with the framework or how the rest of your code works, but that's the easiest way to solve it. If you don't want to do that, there's probably a code somewhere that does something if the ball's x is less than 0, and you'll have to make that a bit more lenient, maybe make it so that if the ball's x is less than -50, or something like that (play around with the number until it works)

挥剑断情 2024-12-08 11:40:37

可以说,如果 (pos.x - radius) == wallLeft 那么球已经接触墙壁并且其速度可以逆转;如果您将其添加为循环中的附加测试是否有帮助?

Arguably if (pos.x - radius) == wallLeft then the ball is already touching the wall and its velocity can be reversed; if you add this as an additional test in the loop does it help?

内心激荡 2024-12-08 11:40:37

我唯一的想法是,速度太高了,以至于在将其添加到位置时会发生溢出,使得 pos.x > 。再次 wallLeft + 半径。

The only idea I am having is that the speed is so high that you get an overflow when adding it to the position, making pos.x > wallLeft + radius again.

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