Advertisement

Advanced collision detection

Started by December 30, 1999 10:48 AM
4 comments, last by jtecin 24 years, 11 months ago
Well assuming bounding box collision detection. One nasty but simple way would be to just have the ball surronded by 4 boxs(one for each side instead one around the whole object).
What I am trying to do is find which side of the brick or square in the center of the screen the ball hits. Basically, I want the ball to bounce realistically off this center "brick". Do you know any tutorials or samples of this?
Advertisement
We definately need a resource explaining this with say some code examples.

Kressilac

Derek Licciardi (Kressilac)Elysian Productions Inc.
This isn't a very hard problem if you ask me. You said you can detect a collition, so all you need to do now is determine the side of the box that the ball hit right?

To start, I would say that the ball either hits one side of the box, or a corner. If it his a side, then it should reflect off the side at an angle opposite of the incoming angle, reletive to the side. If it hits a corner, all you need to do is reverse its x, and y velocities.

To determine what side(s) it hit, first I would find the point on the ball that is closest to the center of the box. And then I would use that point to do some simple tests.

Here is some 1/2 Pseudo code:

bx, and by are the point on the ball closest to the box.

brect is the boxes RECT.

if (by < brect.top)
{
if(bx < brect.left)
{
ball hit at top left corner
}
else if(bx > brect.right)
{
ball hit top right corner
}
else
{
ball hit just top edge
}
}
else if(by > brect.bottom)
{
do same type of thing as above
}
else if(bx < brect.left)
{
do same type of thing as above
}
else if(bx > brect.right)
{
do same type of thing as above
}


there, obviously this could be optimized, and integrated into your basic collition code, but this is the basic idea.

If this doesn't make sense, or you have questions, email me at Pseudo_me@email.com.

Pseudo

Okay, let's say I have a ball randomly bouncing around a screen. In the center of the screen is some kind of square brick. For each frame I have the x coordinate, y coordinate, previous x and previous y coordinates, x velocity and y velocity of the ball. I can find out IF there is a collision, but I have trouble finding WHERE the collision takes place (which side of the square it hits: left, right, top, or bottom). Any help would be appreciated.
oh yeah, if you'd like, I can wip up a little picture to deminstrate what I said before. if you think it would help, just email me: pseudo_me@email.com

This topic is closed to new replies.

Advertisement