I have a set of points (which are Vector3s), all in the same plane (making the shape of a polygon). I want to rotate these points to a different plane, so that they all have the same y value, with the shape of the polygon being maintained. This will make the polygon’s plane parallel to the ground. How can I create this rotation?
The question is a bit confusing. In what plane are the points defined in another baseplane (X-Y, Y-Z)? Or in an arbitrary rotated plane.
If it’s the first case you probably just need to swap the right components of each vector. You want them in the X-Z plane, so if the poins are defined in the X-Y plane you just want to swap Y and Z. If it’s the Y-Z plane you want to swap X and Y. You may need to invert one or both components (X and Z) if the mesh is mirrored.
If it’s an arbitrary plane without having guiding coordinate vectors you want to:
- calculate the plane normal from 3 non collinear points. For this you just construct the two vectors between the three points. Lets call them (BA) and (CA). So the vectors from point A to point B and the vector from point A to point C. Then just use the cross product between those two vectors
- Calculate a quaternion with Quaternion.FromToRotation using the calculated plane normal and the world up vector
- Rotate all points using the quaternion.
- Finally calculate the center of the plane by using the Bounds struct and encapsulate all points. Now you can take the center of the bounds and subtract it from all points to define the points around the world / local center.
Note that this only works if your points are really belonging to the same plane. As a final post processing step you may want to set the y coordinate manually to “0” to avoid tiny errors due to floating point imprecision.
Put in the values you want the points to rotate by.
Quaternion rotation = Quaternion.Euler(0f, 0f, 0f);
// rotate the points
for (int i = 0; i < points.length; i++)
{
point _= rotation * point*;*_
}