C# XNA 精确碰撞?

发布于 2024-12-08 22:38:28 字数 158 浏览 0 评论 0原文

嘿,我正在制作一个桨/乒乓球游戏,现在我正在尝试找出如何在我的桨和我的球之间进行精确碰撞。目前,如果球击中左侧,设置 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:

enter image description here

Is there any way to do this? any help would be most appreciated.

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

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

发布评论

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

评论(4

双手揣兜 2024-12-15 22:38:28

您的示例可以通过否定 y 速度并保持 x 速度来处理。

球和球拍角之间的碰撞更加困难。您需要找到准确的碰撞点,然后计算从球中心到碰撞点的矢量。最后取消沿该轴的速度,同时保持正交轴方向的分量。

Your examples can be handled by negating the y speed and keeping the x 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.

辞旧 2024-12-15 22:38:28

还要小心鬼影。我的意思是,球的速度可以足够大,以至于不存在球实际与球棒相交的任何帧(或更新例程调用),因此您不会检测到碰撞。看我的插图。其中,两个不同帧中的球确实与球棒相交,尽管没有直接相交。这样做的结果将是球“穿过”球棒并神奇地出现在另一侧。

要解决这个问题,你不能用球的位置来计算是否有交叉点,你需要计算之间的差每帧球的新旧位置,并查看该线(差异)是否在任何点与球棒相交。

解决这个问题的简单方法是将蝙蝠视为一条水平线,然后您可以进行简单的线与线相交检查。如果它们相交,就会发生碰撞。更复杂的方法是进行直线/矢量矩形相交。这样做的好处是,您还能够检测与角点的碰撞,这是乒乓球/突破游戏的重要组成部分。

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.

枫以 2024-12-15 22:38:28

我知道这对你目前的情况没有帮助,但你可以尝试使用像 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.

葮薆情 2024-12-15 22:38:28
protected override void Update(GameTime gameTime)
{
    ballrect = new Rectangle((int)ballpos.X, (int)ballpos.Y,
                             ball.Width, ball.Height);

    paddlerect = new Rectangle((int)paddlepos.X, (int)paddlepos.Y,
                               paddle.Width, paddle.Height);

    ms = Mouse.GetState();
    paddlepos.X = ms.X;

    if (ballrect.Intersects(paddlerect))
    {
        bhpaddle();
    }

    ballpos += ballvel;

    if (ballrect.Y>= paddlerect.Y)
    {
        ballpos = new Vector2(400, 480);
        bhpaddle();
        ballpos += ballvel;
        l--;
    }

    base.Update(gameTime);
}



public void bhpaddle()
{
    ballvel.Y *= -1;
}
protected override void Update(GameTime gameTime)
{
    ballrect = new Rectangle((int)ballpos.X, (int)ballpos.Y,
                             ball.Width, ball.Height);

    paddlerect = new Rectangle((int)paddlepos.X, (int)paddlepos.Y,
                               paddle.Width, paddle.Height);

    ms = Mouse.GetState();
    paddlepos.X = ms.X;

    if (ballrect.Intersects(paddlerect))
    {
        bhpaddle();
    }

    ballpos += ballvel;

    if (ballrect.Y>= paddlerect.Y)
    {
        ballpos = new Vector2(400, 480);
        bhpaddle();
        ballpos += ballvel;
        l--;
    }

    base.Update(gameTime);
}



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