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
9 changes: 7 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,10 @@
},
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
}
},
james-pre marked this conversation as resolved.
Show resolved Hide resolved
"prettier.tabWidth": 4,
"prettier.printWidth": 180,
"prettier.useTabs": false,
"prettier.singleQuote": false,
"files.insertFinalNewline": false,
}
29 changes: 29 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,35 @@ export class Vector3 {
return Scalar.NormalizeRadians(angle);
}

/**
* Gets the angles between two vectors needed to change direction
* @param start the starting vector
* @param target the target vector
* @param ref the vector to store the result
* @returns the updated ref with the between the two vectors in the form (pitch, yaw, roll)
*/
public static GetAnglesBetweenVectorsForDirectionChangeToRef(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 vectors needed to change direction
* @param start the starting vector
* @param target the target vector
* @returns the angles between the two vectors in the form (pitch, yaw, roll)
*/
public static GetAnglesBetweenVectorsForDirectionChange(start: Vector3, target: Vector3): Vector3 {
james-pre marked this conversation as resolved.
Show resolved Hide resolved
const diff = TmpVectors.Vector3[0];
target.subtractToRef(start, diff);
const theta = Math.atan2(diff.x, diff.z) || 0,
phi = Math.atan2(Math.sqrt(diff.x ** 2 + diff.z ** 2), diff.y) || 0;
return new Vector3(phi, theta, 0);
}

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