Given is a point P in a plane, a rotation (quaternion) and a point P’ resulting from the rotation. Is there an elegant way or a function in Unity to calculate the center of the rotation? Thanks in advance.
I don’t believe there are any built in functions to do such a thing, but we can get there with some trigonometry and vector maths;
//Where: // - rotation = input quaternion // - p0 = starting point // - p1 = point resulting from rotating vector[p0 - centre] // - planeNormal = the relative 'up' vector of the plane //Find the total angle of rotation (in radians) float theta = Quaternion.Angle (Quaternion.identity, rotation) * Mathf.Deg2Rad; //Find the vector between p0 and p1 Vector3 p01 = p1 - p0; //Find the distance^2 between p0 and p1 float dist2 = p01.sqrMagnitude; //Form a triangle with vertices [p0, p1, centre], where sides [p0-centre] and [p1-centre] are equal in length //Use the cosine rule to find the length^2 (A2) of said sides; // - c^2 = a^2 + b^2 - 2ab cos (C) // - where c = dist, b = a; // - dist^2 = 2a^2 - 2a^2 cos (theta) // - dist^2 = 2a^2 (1 - cos (theta)) // - dist^2 / (1 - cos (theta)) = 2a^2 // - a^2 = dist^2 / 2(1 - cos (theta)) float sideA2 = dist2 / (2f * (1f - Mathf.Cos (theta))); //Find the height of the triangle using Pythagoras' theorem float height = Mathf.Sqrt (sideA2 - 0.25f * dist2); //Find the midpoint between p0 and p1 Vector3 midpoint = (p0 + p1) * 0.5f; //Find the direction of the centre from the midpoint (use the plane's normal to calculate the vector perpendicular to p01) Vector3 dir = Vector3.Cross (planeNormal, p01 / Mathf.Sqrt (dist2)).normalized; //Combine and offset to find the centre Vector3 centre = midpoint + dir * height;
I haven’t tested the above yet, but that should theoretically do it.