简体   繁体   English

C#XNA精确碰撞?

[英]C# XNA Precise Collisions?

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: 嗨,我正在制作一个球拍/乒乓球游戏,现在我正试图找出如何在球拍和球之间进行精确的碰撞。目前,如果球碰到左侧,则基本碰撞就是x速度和y速度..等,但我想弄清楚如何使其朝一个方向反弹,如下图所示:

在此输入图像描述

Is there any way to do this? 有没有办法做到这一点? any help would be most appreciated. 非常感激任何的帮助。

Your examples can be handled by negating the y speed and keeping the x speed. 可以通过否定y速度并保持x速度来处理您的示例。

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. 意思是说,球的速度可以足够大,以至于没有任何框架(或Update例行调用)在其中球实际与球棒相交,因此您不会检测到碰撞。 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这样已经制造的物理引擎来轻松地为您处理这些碰撞。

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM