Search code examples
c++3dangleplane

Angles of quad in 3D space


I'm working on a physics simulation of projectiles, and I'm stuck on the ground collision. I have a ground made of quads,

enter image description here

I have the points stored in an array, so I thought that if I take the quad where the collision happen and calculate the angles of the quad (in x and z directions) I can then use that to change the velocity of the projectile.

This is where I'm stuck. I thought I should find the lowest and the highest point, then find the vector between them, but this will not give the angles in all directions, which is what I want. I know there must be a way of doing this, but how?


Solution

  • What you want is a normal of a quad.

    Here's an answer that shows you how to get a quad's normal

    After you got the normal, you need to calculate the force of the collision's response. Its direction is the normal of the quad and the strength is the strength the projectile exerts in the direction of the quad. Exerted force is calculated by using dot product of the projectile's velocity and reversed quad's normal (Here's a wiki link for the dot product)

    The response vector should be this:

    Vector3 responseForce = dot(projectile.vel, -1 * quad.normal) * quad.normal;
    projectile.vel += responseForce;