Rotate the point around the normal from two points (flip point)
Hello, I need to rotate a point C or D around (or shoot the point across) the axis normal. I need just reflection of all points between A and B across the normal (in 3D space ofcourse).
Thanks for any help.
Answer by Eno-Khaon · Jan 06, 2016 at 09:33 AM
Your center point for either rotation is the Vector Projection of C or D onto A-B (or B-A). Your rotational axis itself is simply the same vector (depending on your intended direction of rotation).
Therefore, if I'm not mistaken, you need a center point to rotate around in order to get a relative position for the object being rotated around the line, the axis to rotate around (the line itself), then the rotation to add to your existing rotation, as well as to utilize as an additive position offset.
Vector3 rotationCenter = A + Vector3.Project(C - A, B - A);
Vector3 rotationAxis = (B - A).normalized;
Vector3 relativePosition = C - rotationCenter;
Quaternion rotatedAngle = Quaternion.AngleAxis(degrees, rotationAxis);
Vector3 rotatedPosition = rotatedAngle * relativePosition;
// New object position
obj.transform.position = rotationCenter + rotatedPosition;
// New object rotation, if applied
obj.transform.rotation *= rotatedAngle;
Edit: Whoops! One last correction made to rotationCenter. I really need to stop programming stuff while tired.
Answer by Masterio · Jan 06, 2016 at 11:06 AM
Perfect!!! Thanks a lot for fast response:)
Final function looks like this ( if somebody need that ):
public Vector3 GetFlippedPosition(Vector3 A, Vector3 B, Vector3 C, float degrees = 180)
{
Vector3 rotationCenter = A + Vector3.Project(C - A, B - A);
Vector3 rotationAxis = (B - A).normalized;
Vector3 relativePosition = C - rotationCenter;
Quaternion rotatedAngle = Quaternion.AngleAxis(degrees, rotationAxis);
Vector3 rotatedPosition = rotatedAngle * relativePosition;
// New object position
return rotationCenter + rotatedPosition;
// new rotation
//obj.transform.rotation *= rotatedAngle;
}