Skip to content

Commit

Permalink
Merge pull request #13012 from dr-vortex/master
Browse files Browse the repository at this point in the history
Add function to get angles between two vectors (Vector3.GetAnglesBetweenVectorsForDirectionChange)

Former-commit-id: e954562b579ddbb1c7a6c75589d09621c9a49551
  • Loading branch information
sebavan committed Sep 26, 2022
2 parents 3b04ec7 + e24076d commit 9102f60
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
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 @@ -1744,6 +1744,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

0 comments on commit 9102f60

Please sign in to comment.