Object going through wall during collision

橙三吉。 提交于 2021-01-29 01:30:17

问题


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


回答1:


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)




回答2:


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?




回答3:


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.



来源:https://stackoverflow.com/questions/7197513/object-going-through-wall-during-collision

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!