C# XNA 精确碰撞?
嘿,我正在制作一个桨/乒乓球游戏,现在我正在尝试找出如何在我的桨和我的球之间进行精确碰撞。目前,如果球击中左侧,设置 x 速度和 y 速度,这是基本碰撞..等等,但我想弄清楚如何让它在一个方向上反弹,可以这么说,如下所示:
有吗方法来做到这一点?任何帮助将不胜感激。
Hey guy's i'm making a Paddle/Pong game and right now i'm trying to find out how to do precise collision between my paddle and my ball.. Currently it is basic collision if ball hits left side set x speed and y speed.. etc but i want to figure out how to make it bounce of on a direction so to speak as illustrated below:
Is there any way to do this? any help would be most appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的示例可以通过否定
y
速度并保持x
速度来处理。球和球拍角之间的碰撞更加困难。您需要找到准确的碰撞点,然后计算从球中心到碰撞点的矢量。最后取消沿该轴的速度,同时保持正交轴方向的分量。
Your examples can be handled by negating the
y
speed and keeping thex
speed.Collisions between the ball and the corners of the paddle are more difficult. You need to find the exact collision point, then calculate the vector from the center of the ball to the collision point. And finally negate the speed along that axis while keeping the component in the direction of the orthogonal axis.
还要小心鬼影。我的意思是,球的速度可以足够大,以至于不存在球实际与球棒相交的任何帧(或更新例程调用),因此您不会检测到碰撞。看我的插图。其中,两个不同帧中的球确实与球棒相交,尽管没有直接相交。这样做的结果将是球“穿过”球棒并神奇地出现在另一侧。
要解决这个问题,你不能用球的位置来计算是否有交叉点,你需要计算之间的差每帧球的新旧位置,并查看该线(差异)是否在任何点与球棒相交。
解决这个问题的简单方法是将蝙蝠视为一条水平线,然后您可以进行简单的线与线相交检查。如果它们相交,就会发生碰撞。更复杂的方法是进行直线/矢量矩形相交。这样做的好处是,您还能够检测与角点的碰撞,这是乒乓球/突破游戏的重要组成部分。
Beware of ghosting too. And by that I mean that the velocity of the ball can be great enough that there isn't any frame (or Update routine call) in which the ball actually intersects with the bat, so you wouldn't detect a collision. See my illustration. In it, the ball in two different frames did intersect with the bat, even though there was no direct intersection. The result of this will be that the ball went 'through' the bat and magically appeared on the other side.
To solve this problem, you can't use the position of the ball to calculate if there was an intersection, you need to calculate the difference between the old and new position of the ball each frame and see if that line (the difference) intersected with the bat at any point.
The easy way to solve this would be to consider the bat as a horizontal line, then you could do a simple line-line intersection check. If they intersect, there was a collision. The more complicated way would be to do a line/vector-rectangle intersection. The advantage of that would be that you would also be able to detect collisions with corners which are an important part of a pong/breakout game.
我知道这对你目前的情况没有帮助,但你可以尝试使用像 Farseer 这样已经制作好的物理引擎来非常轻松地为你处理这些碰撞。
I know this wont be helpful in your current situation, but you can try using a already made physics engine like Farseer to handle those collisions for you very easily.