Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

2D Continuous Collision Detection

I'm trying to implement simple continuous collision detection for my pong game however i'm not sure i'm implementing or understand this right. AFAIR continuous collision detection is used for fast moving objects that may pass through another object circumventing normal collision detection.

So what I tried was that because the only fast moving object I have is a ball I would just need the position of the ball, its move speed, and the position of the object we are comparing to.

From this I figured it would be best that for example if the ball's move speed indicated it was moving left, I would compare it's left-most bound to the right-most bound of the other object. From this I would step through by adding the move speed to the left-most bound of the ball and compare to make sure it's greater than the other objects right bound. This would show that there is no left right collision.

I have something somewhat working, but unfortunately, the ball starts bouncing normally for a while then it acts as if it hits a paddle when nothing is there.

I'm a bit lost, any help would be appreciated!

static bool CheckContinuousCollision(PActor ball, PRect ballRect, PActor other, PRect otherRect)
{
    PVector ballMoveSpeed;
    int ballXLimit;
    int ballYLimit;

    ballMoveSpeed = ball.moveSpeed;

    // We are moving left
    if ( sgn(ball.moveSpeed.x) < 0 )
    {
        ballXLimit = std.math.abs(ballMoveSpeed.x) / 2;

        for ( int i = 0; i <= ballXLimit; i++ )
        {

                if ( ballRect.Left < otherRect.Right && otherRect.Left < ballRect.Left)
            {
                return true;
            }

            ballRect.Left -= i;
        }
    }

            //We are moving right
    if ( sgn(ball.moveSpeed.x) > 0)
    {
        ballXLimit = std.math.abs(ballMoveSpeed.x) / 2;

        for ( int i = 0; i < ballXLimit; i ++ )
        {

            if ( ballRect.Right > otherRect.Left && ballRect.Right < otherRect.Right )
            {
                return true;
            }   

            ballRect.Right += i;
        }
    }
            // we are not moving
    if ( sgn(ball.moveSpeed.x) == 0)
    {
        return false;
    }
}
like image 601
RedShft Avatar asked Mar 09 '12 01:03

RedShft


People also ask

How do you detect 2d collisions?

One of the simpler forms of collision detection is between two rectangles that are axis aligned — meaning no rotation. The algorithm works by ensuring there is no gap between any of the 4 sides of the rectangles. Any gap means a collision does not exist.

What is continuous collision detection?

Continuous Collision Detection (CCD) The purpose of Continuous Collision Detection (CCD) is to detect collisions of fast moving objects that would otherwise be missed. The opposite of CCD is Discrete Collision Detection which only checks for collision at one position for each frame.

What are the types of collision detection?

Two forms of collision detection: Continuous: very expensive. Simulate solid objects in real life. Discrete: objects will end up with penetrating each other.

What is Quadtree for collision?

Quadtree is a way of partitioning space so that it's easy to traverse and search. A quadtree recursively partitions two-dimensional space into squares, dividing each square into four equally-sized squares. Each distinct data point exists in a unique leaf node; Coincident points are represented by a linked list.


1 Answers

You seem to be checking the collision of only one dimension, i.e the X dimension of your ball versus your Other.

What you probably want is to compare whether the two objects collide in 2d space. This can be easily done by adjusting each objects Bounding Rectangle and checking whether the rectangles overlap. Then in your for loop you can adjust your Ball rectangle accordingly

like image 87
fileoffset Avatar answered Oct 11 '22 15:10

fileoffset