开发者

C# XNA Precise Collisions?

开发者 https://www.devze.com 2023-04-11 10:06 出处:网络
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 p开发者_如何学Goaddle and my ball.. Currently it is basic collision if ball hits

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 p开发者_如何学Goaddle 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:

C# XNA Precise Collisions?

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.

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.

C# XNA Precise Collisions?

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.


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号