Calculating orientation of a plane formed by 4 points

Hey all!
I don’t post here much but I’m seeking help to solve a (probably) quick maths question.

Say I have 4 vectors like in the pic below (red parts) and I wanted to get the angle between all 4 vectors (grey part) how would I go about doing that?
image

Thanks

2 Likes

What type of angles do you want exactly? What use will this have?

I want to be able to calculate the angle of the grey part based off of the red points

You mean the rotation of the part itself? Rotations of 3D objects can be described as 3 different vectors which describe a space relative to those vectors. In this case we want a set of orthonormal vectors, vectors that are unit length and all perpendicular to each other, so that we can create a rotation matrix. Rotation matrices are what CFrames are made of. For this we want a “front” vector, “right” vector, and “up” vector to describe the 3 axis that make up our space. In 3D we can use the cross product between two vectors to give us a third vector that is perpendicular to both, so we only really need 2 vectors, or 3 points to calculate it. If you know which point you want to be in front left, which point you want to be on the back right, and which point you want to be on the back left, then calculating these vectors is easy:

front = (frontLeft - backLeft).unit
right = (backRight - backLeft).unit
up = right:Cross(front)

From here you can construct a CFrame using these vectors with the constructor that takes 12 numbers:

result = CFrame.new( x, y, z, right.x, up.x, -front.x, right.y, up.y, -front.y, right.z, up.z, -front.z )

Additionally, the position or center of the space can be described using 0.25 * (pos1 + pos2 + pos3 + pos4) assuming those 4 points describe a rectangle. Or you can just use the midpoint of two of the corners with: 0.5 * (corner + oppositeCorner). Both of those formulas use barycentric coordinates, and it won’t give you a perfect center for all point configurations, but it is guaranteed to give a point somewhere in the middle.

If you want a representation of it in Euler angles or axis angles, you can use pitch, yaw, roll = result:toEulerAnglesXYZ() or axis, angle = result:toAxisAngle().

I skipped over quite a few details about why this works, but you can learn this in linear algebra.

16 Likes

Not exactly sure what you mean. If you are asking for the literal angles between 4 vectors you can use the dot product between adjacent vectors to get each angle of the quadrilateral. This link gives a pretty good tutorial over that.

If you are asking for the cframe description of the gray part then @MettaurSp did a pretty good job of explaining it.

2 Likes

Thanks for your help with this, it works perfectly! :heart: