# Determine the normal between two objects

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

## Recommended Posts

I'm wondering if there is a well known algorithm for what I'm trying to do. I'm trying to determine how best to go about finding the equation (actually just the slope) of the normal line where to objects collide based upon the overlapping pixel(s)...or something like that. (See image below for what I mean.) I want to know if there is a way to find out what angle the object should bounce off. Any suggestions? Thanks!

##### Share on other sites
You mention pixels, so I'm assuming these are sprites in a 2d context. This is IMO, but I think you'd have better luck representing your objects with geometric primitives for the purpose of collision detection and response. The problems you're trying to solve - finding a normal and point of collision, reflecting the velocity, etc. - all have established solutions in geometrical terms. Circles, for example, are very easy to work with.

Also, in a 2d game environment it's best not to think of lines in terms of slope (even though that's the way we learn about them in beginning algebra). That representation isn't particularly useful for 2d simulations; the vector form is to be preferred.

The problem you describe has been discussed ad infinitum on these boards, but with the search function disabled the threads may be hard to find. If you're interested in the geometric (rather than sprite-based) solution, though, I'm sure someone would be glad to explain the basics to you.

##### Share on other sites
I'm not completely sure what you're asking for, but it seems like vector reflection might be it.

(Look at the bottom diagram on that link.)

##### Share on other sites
If the objects are circles and the overlap is shallow, the direction from the center of one circle to the center of the other is a simple approximation.

For more accuracy you can calculate the time of impact (or an approximation assuming constant velocity, or constant acceleration, or whatever) and find the angle at the time of impact. This can be done analytically, or you can find it numerically with a search for the time of impact (using a binary search or something with respect to time) to some level of precision (precision being measured either by the size of the time interval you know the collision to have occured in, or the distance of the objects from each other).

Polygons are more complicated but it's still the same thing. You're looking for the normal at where the collision occured between a vertex and an edge (or in 3d a vertex and a face or an edge and an edge, and in the edge-edge collision I think the edges both lie on the collision plane so the direction of their normal can be found with a cross product).

There are lots of approximations that can be done to simplify things (at the cost of accuracy); for example, you can make use of the velocity of the objects and ignore their acceleration, or you can even ignore the velocity and just look for the closest features on the colliding objects.

If you're using sprites instead of geometry, there's a method to approximate the angle by calculating how many pixels overlap if you shift one of the objects right, left, up, and down from its current position. The idea is more overlap = more penetration, so from this you can calculate the direction that reduces the penetration between the two sprites. of course this method is really rough and how well it works depends on the sprites, how they collide, if they were going fast then it might help to find how they were oriented at the time of impact or something, etc.

##### Share on other sites
Thanks for the replies, esp. whoever is "Anonymous"...that is exactly what kind of info I was looking for!! Thank you!!!

1. 1
Rutin
40
2. 2
3. 3
4. 4
5. 5

• 18
• 20
• 12
• 14
• 9
• ### Forum Statistics

• Total Topics
633363
• Total Posts
3011511
• ### Who's Online (See full list)

There are no registered users currently online

×