I'm programming a simple physics system for vehicles in Unity and I'm trying to figure out how to calculate the average slope based on four points in order to apply the gravitational acceleration. I can get the terrain height at the vehicle wheels positions like this:
Terrain.activeTerrain.SampleHeight(wheel.position);
How can I calculate the average terrain slope based on those four points? I thought about calculating direction vectors between different points, but I'm not sure how to combine them into a single slope vector.
(Assuming that all 4 wheels are always coplanar) I think you could take any 3 wheels, take the height sample
and from that resulting 3 points generate a plane
Now you can take any of these points and take a vector in straight up direction.
Then you can map that vector onto the plane using closest point on plane.
As a last step normalize the vector between that mapped point and your start wheel point and you should have the direction of the slope + the Y component tells you how steep it is
where both
planeVector.magnitude
anddirection.y
should be moving between0
(completely horizontal surface) and-1
(completely vertical surface)Something like that I guess ^^