How to calculate box rotation from array of points?

Hello, I’m a bit new to working with 3D space and rotation. I have a list of four Vector3 points that represent the corners of a rectangle. What I want to do is use those points to create a box mesh that is rotated to exactly match the angle of rotation of the points.

Here is a playground showing what I mean. In it you can see I’ve drawn a simple line mesh between the points. That is great and the rectangle drawn is at the expected angle given the data. I’ve also created a box mesh and configured its dimensions to match and placed its center point in the proper center of the points. So far so good, however I cannot figure out how to rotate the box so that its top face is parallel with the face of the rectangle.

Any help is greatly appreciated!

I guess you could try to find the normal to the plane defined by your 4 points and use the mesh.lookAt function to orient it in the right direction.

I’ve messing with that idea for a bit and I don’t think it’s what I need. Maybe you can update that playground to show an example?

Here an example PG that works:


Thanks @Takemura

1 Like