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 angles between two points needed to face the target from the start
james-pre marked this conversation as resolved.
Show resolved Hide resolved
* @param start the starting point
* @param target the target point
* @param ref the vector3 to store the result
* @returns the updated ref with the angles between the two points in the form (pitch, yaw, roll)
*/
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 angles between two points needed to face the target from the start
james-pre marked this conversation as resolved.
Show resolved Hide resolved
* @param start the starting vector
* @param target the target vector
* @returns the angles between the two points in the form (pitch, yaw, roll)
*/
public static PitchYawRollToMoveBetweenPoints(start: Vector3, target: Vector3): Vector3 {
const ref = Vector3.Zero();
james-pre marked this conversation as resolved.
Show resolved Hide resolved
return Vector3.PitchYawRollToMoveBetweenPointsToRef(start, target, ref);
}

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