PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Help with Collision Detection

Ok, I give up. This should be so easy, but for the life of me can't figure out the best method. Basically, think of a single stationary block and a bouncing ball (ala Araknoids). I need to determine if the ball should reverse Y direction if it hits on the top or reverse X direction if it hits the block on the side. My basic game loop is:

1) Render ball
2) Animate ball (applies x and y speed to x&y; vals)
3) Go through blocks and see if ball has collided

It's step 3 that is messing up. I seem to find collisions from top or bottom find (ptIntersect.y is equal to the top or bottom coord of block), but the side collision is giving me problems.

Here are some images.

1) This shows the CollisionMask sample with graphics changed to look like what I am using. This is the ball hitting the block from the top. Note that the ball overlaps the block. This is because the Y speed is set to something like 5 pixels so it has the possibility of overlapping by at most 4 pixels (I move it to the top edge if a collision is detected, but collision detection occurs with overlapping).

Image

2) This shows the same example with the ball coming from the right. Things to notice: the SpriteCollision Y value is the same in 2nd image as first so I can't use that to determine which direction I should bounce the ball.

Image

Well, I hope this post made sense. Any help I can get I'd really appreciate.

Oh, and no, the game I am working on is not another breakout/araknoid clone. Something entirely different.

-Mark
Is it possible for you to use the direction vector of the ball's motion to determine the resulting direction after the collision?
markhsch · 26 Nov 2003, 23:59 UTC — Clarification
ppcStudios wrote:
Is it possible for you to use the direction vector of the ball's motion to determine the resulting direction after the collision?


The problem is that I can't determine if it hits the top of the block or hits the right side. If I can make that determination, the resulting vector is trivial. If it hit the top, the Y speed is reversed/negated/whatever but if it hits the right side, the X speed is reveresd/negated/... Does that clarify it?
markhsch · 27 Nov 2003, 00:04 UTC — Code and Binaries
Here's a link to the binaries to see what the current state is (use left and right arrows to move the ball).

http://www.schmidt6.com/simple.zip

Here's the collision detection code. In the code, mX, mY, mWidth, mHeight are the variables for the block.

Code:

bool CBlock::DetectCollision( CGapiSurface* backBuffer, CSprite* ball )
{
POINT ptIntersect;
if( backBuffer->Intersect( ball->get_X(), ball->get_Y(), ball->get_Surface(), NULL, mX, mY, &mSurface;, NULL, &ptIntersect; ) == GD_OK )
{
if( ptIntersect.x == -1 && ptIntersect.y == -1 )
return false;
else
{
// coming from top
// TEMP: Move to collision success routine
if( ptIntersect.y == mY && ptIntersect.x != mX+mWidth )
{
// from top
ball->put_Y( mY-ball->get_Height() ); // move ball so overlap doesn't occur
ball->put_YSpeed( -ball->get_YSpeed() );
}
else if( ptIntersect.y == ball->get_Y() && ptIntersect.x != mX+mWidth )
{
// from bottom
ball->put_Y( mY+mHeight ); // move ball so overlap doesn't occur
ball->put_YSpeed( -ball->get_YSpeed() ); // reverse direction
}
else if( ptIntersect.x == mX+mWidth )
{
// from right
ball->put_X( mX+mWidth ); // move ball so overlap doesn't occur
}
else // TODO
{
// from left
}

return true;
}
}

return false;
}
markhsch · 27 Nov 2003, 00:07 UTC — Whoops
If you download the binaries, put the demo.lvl file right at the root of your c: drive (i.e. c:\demo.lvl). Yes its a hack and I know it :lol:
I understand what you're talking about now - you're missing the actual collision and catching it after the object has already penetrated the surface.

What you will probably want to do each frame is save the positions of your objects prior to moving them. If you have a collision on that frame, you use the old positions to cast a ray from the center of your ball along the motion vector and see where it strikes the other object. Now you will know where on the object the collision actually occured.
This issue is called "tunneling" if you wish to search for it.
You're over complicating the problem, you're trying to make use of the edge of the ball (circle) to check for collisions against the block (rect) and by doing this you're missing the obvious. Here’s the secret, use the middle-point of the circle which is defined by an X and Y value. How?...

Make use of the following object types:
- RECT (top, left, bottom, right)
- CIRCLE (xpos, ypos, radius)

TEST1: (quick and dirty)
- Create a tempRECT that’s exactly the amount of the radius of the CIRCLE bigger than the original RECT on all its sides. So your collision area is a bit bigger than the original RECT.
- Now use tempRECT and check if the CIRCLE’s middle-point falls in this region. If it does we’ve got a possible collision, so take a closer look.

TEST2: (a closer look)
- Divide tempRECT into 8 regions. These regions represents the 4 CORNERS, the TOP, BOTTOM, LEFT and RIGHT.
- Now test the CIRCLE’s middle-point within each of those 8 regions. This will give you a pretty good idea of where the collision has taken place, and you will be able to take the appropriate action. If we're in one of the corner regions, we will need to take an even closer look to be able get a precise collision.

TEST3: (precise corner test)
- It’s simple, just check the distance between the original RECT’s corner-point and the CIRCLE’s middle-point. If the distance is smaller than or equal to the CIRCLE's radius, we've got a collision.

DESIGN SUGGESTION:
- Write a single function taking two params, a RECT and a CIRCLE. The function will then do the necesary tests as needed. All it returns is an INT indicating which region we've got a collision in. If it's 0 we're in the clear and our ball has missed the block. This way the function will be able to do this collision test for different sized blocks against different sized balls.

That's about it. I wanted to post some pics, but I don't have a place on the net to drop them. Pop me an e-mail and I'll try to draw some pics for you. I'm sure that there's probably even faster methods, this one works for me for now.

Goodluck!