#### Archived

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

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

## 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 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 on other sites
We definately need a resource explaining this with say some code examples.

Kressilac

##### 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 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 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

1. 1
Rutin
46
2. 2
3. 3
4. 4
5. 5
JoeJ
19

• 11
• 13
• 9
• 10
• 12
• ### Forum Statistics

• Total Topics
633003
• Total Posts
3009825
• ### Who's Online (See full list)

There are no registered users currently online

×