Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- /// Whip's Get Rotation Angles Method v12 - 2/16/18 ///
- Dependencies: VectorAngleBetween()
- * Fix to solve for zero cases when a vertical target vector is input
- * Fixed straight up case
- * Fixed sign on straight up case
- * Converted math to local space
- */
- void GetRotationAngles(Vector3D targetVector, IMyTerminalBlock reference, out double yaw, out double pitch)
- {
- var localTargetVector = Vector3D.TransformNormal(targetVector, MatrixD.Transpose(reference.WorldMatrix));
- var flattenedTargetVector = new Vector3D(localTargetVector.X, 0, localTargetVector.Z);
- yaw = VectorAngleBetween(Vector3D.Forward, flattenedTargetVector) * Math.Sign(localTargetVector.X); //right is positive
- if (Math.Abs(yaw) < 1E-6 && localTargetVector.Z > 0) //check for straight back case
- yaw = Math.PI;
- if (Vector3D.IsZero(flattenedTargetVector)) //check for straight up case
- pitch = MathHelper.PiOver2 * Math.Sign(localTargetVector.Y);
- else
- pitch = VectorAngleBetween(localTargetVector, flattenedTargetVector) * Math.Sign(localTargetVector.Y); //up is positive
- }
- double VectorAngleBetween(Vector3D a, Vector3D b) //returns radians
- {
- if (Vector3D.IsZero(a) || Vector3D.IsZero(b))
- return 0;
- else
- return Math.Acos(MathHelper.Clamp(a.Dot(b) / Math.Sqrt(a.LengthSquared() * b.LengthSquared()), -1, 1));
- }
Advertisement
Add Comment
Please, Sign In to add comment