Box 2d Normal Vector for the surface of impact?

Started by
3 comments, last by WilkinzMicawber 7 years, 11 months ago

As far as I can tell from online tutorials, the normal vector returned from a collision in box2d is the shortest direction for the colliding object to go to no longer penetrate the object it ran into. What I need is the normal vector of the surface it ran into, which is the direction the wall is pointing. For example, if I run into a vertical wall from the left, the normal vector I would expect would be <-1,0>. Is there a way to access this vector?

My ultimate goal is to reflect a raycast so I can map the path a projectile will take with no friction. Do raycasts allow reflections in box2d/farseer? I know many physics engines do, but I don't see that capability in Farseer.

Advertisement

1. Yes, and generally that would be the normal vector for every physics engine. However, it may be inverted if the wall is not treated as the first object in a collision internally. This is solved by flipping the normal. IIRC in B2D it can be acessed by requesting a world manifold from a contact.

2. Yes, a world has an interface for ray casts and AABB queries, and the former returns the surface normal if the ray/segment hits your vertical wall.

Ah, so the normal value in the return of a raycast is the surface normal, not the intersection normal? I had assumed that the normal returned was the collision normal for the ray.

Yes.

For example, if a segment hits a face of a box, then the ray cast callback will be notified with the box, the face normal, and the intersection fraction which you can derive the intersection point from using linear interpolation:

p = (1.0 - fraction) * p1 + fraction * p2.

Great! Thanks, Irlan. I greatly appreciate it. I was about to try and calculate the normal using multiple raycasts and trigonometry.

This topic is closed to new replies.

Advertisement