Archived

This topic is now archived and is closed to further replies.

Advanced collision detection

This topic is 6563 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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).

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites