Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function to get angles between two vectors (Vector3.GetAnglesBetweenVectorsForDirectionChange) #13012

Merged
merged 9 commits into from
Sep 26, 2022
Merged
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
}
}
26 changes: 26 additions & 0 deletions packages/dev/core/src/Maths/math.vector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1716,6 +1716,32 @@ export class Vector3 {
return Scalar.NormalizeRadians(angle);
}

/**
* Gets the rotation that aligns the roll axis (Y) to the line joining the start point to the target point and stores it in the ref Vector3
* @param start the starting point
* @param target the target point
* @param ref the vector3 to store the result
* @returns ref in the form (pitch, yaw, 0)
*/
public static PitchYawRollToMoveBetweenPointsToRef(start: Vector3, target: Vector3, ref: Vector3): Vector3 {
const diff = TmpVectors.Vector3[0];
target.subtractToRef(start, diff);
ref.y = Math.atan2(diff.x, diff.z) || 0;
ref.x = Math.atan2(Math.sqrt(diff.x ** 2 + diff.z ** 2), diff.y) || 0;
return ref;
}

/**
* Gets the rotation that aligns the roll axis (Y) to the line joining the start point to the target point
* @param start the starting point
* @param target the target point
* @returns the rotation in the form (pitch, yaw, 0)
*/
public static PitchYawRollToMoveBetweenPoints(start: Vector3, target: Vector3): Vector3 {
const ref = TmpVectors.Vector3[0];
return Vector3.PitchYawRollToMoveBetweenPointsToRef(start, target, ref);
}

/**
* Slerp between two vectors. See also `SmoothToRef`
* Slerp is a spherical linear interpolation
Expand Down