I have a 3d moving box and a stationary box. I can detect collisions ok but now I would like to slide the moving box against the stationary box as a collision response. For this I need the normal of the face that collides with the moving box. Does anyone know how I figure this out? It doesn't seem like the collision test gives me this information. Any help is appreciated.
Answer
In order to calculate the normal to a plane (the collided AABB face) you need:
- 3 points of this plane (2 non parallel edges).
- The cross product of two edge vectors of this plane (usually from the same point of origin).
This will produce a vector perpendicular to this plane (but a plane have 2 sides).
Here is an example of how to calculate the cross product.
Let A, B, C and D be the four points of your AABB collided side.
[B]---(c)
| |
[A]---[D]
If we use ABC, then the normal to the plane is
ADxAB (if we want a normal pointing out of the screen)
ABxAD (if we want a normal pointing inside the screen)
(it's just because our example is facing the screen, if the plane is seen from left side, the fist normal will point right and the second will point left)
As you can see the order in which you take the points will impact the side the normal will point to. That's why developers usually store their points in a specified order (clockwise or counter-clockwise, but the same for all faces) so they know in which order to use them.
See this on more details now:
AB = [ Bx-Ax, By-Ay, Bz-Az ]
AD = [ Dx-Ax, Dy-Ay, Dz-Az ]
Here is how to get the first normal
ADxAB = [ ADx, ADy, ADz ] X [ ABx, ABy, ABz ]
= [ (ADy*ABz - ADz*ABy), (ADz*ABx - ADx*ABz), (ADx*ABy - ADy*ABx) ]
And here come the second normal pointing to the other side
ABxAD = [ ABx, ABy, ABz ] X [ ADx, ADy, ADz ]
= [ (ABy*ADz - ABz*ADy), (ABz*ADx - ABx*ADz), (ABx*ADy - ABy*ADx) ]
If you replace the ABx, ABy, ABz etc. values with the differences calculated just before (Bx-Ax, By-Ay, Bz-Az etc.) the cross products you will get the normal vectors.
No comments:
Post a Comment