# How to align a 3D object with four points?

I'm sure there's a better way to articulate this question, but I lack the vocabulary.

Imagine a skateboard. As it rolls straight down a road, the "deck" is aligned with the road, just offset a few inches higher.

If the skateboard is on a ramp with an incline of say 20º, then then deck of the skateboard can be aligned with the ramp by rotating along it's x-axis by 20º.

But what if the skateboard approaches the ramp at an angle?

Perhaps a visual explanation might be easier to understand. If I know the points at the top of each pink/purple pole, and I wanted to place an object in the scene that aligned with those points, like this... Is there some known method for figuring this out?

I'm using Unity, if that makes a difference?

• Correct. As Philipp pointed out in their answer, there's two possible solutions to that question. Which I now understand is solvable by discarding the lowest of the four points. – gargantuan Jan 22 '20 at 17:30

## 1 Answer

The easy solution would be to simply give the object a BoxCollider and a Rigidbody and let the Unity physics engine figure out how it lands. But let's assume you have a good reason why you want to use your own physics and thus want to calculate it yourself.

You will have to cheat here a bit, because in many situations there will be two correct solutions. If you put a perfectly balanced 4-legged table on an uneven surface, it will wobble. There are two solutions to put it down, each one with one leg in the air. Neither solution is more correct than the other.

Applying this to your problem means that you will have to pick 3 of the 4 contact points and ignore the 4th. A good approach is to simply ignore the lowest of the 4 points.

You can now use the surface normal of this triangle as the surface normal of the board. A good explanation how to do this can be found in this stackoverflow answer. The resulting vector is a direction which you can convert into a quaternion using SetLookRotation and assign to transform.rotation.

The transform.position is the average of the two highest points.