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 polar and spherical coordinate system support #12942

Merged
merged 34 commits into from
Sep 14, 2022
Merged
Changes from 2 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
a53cbca
Add polar coordinate conversions to Vector2 and Vector3
james-pre Sep 2, 2022
72610af
Fixed Vector3.toPolar to return length
james-pre Sep 2, 2022
a461644
Merge pull request #1 from BabylonJS/master
james-pre Sep 3, 2022
119dc22
Added seperate class for Polar and Spherical coordinate systems
james-pre Sep 4, 2022
9826cd0
Fixed pascal case, terminology, and theta issue
james-pre Sep 4, 2022
fb54ef6
Added toRef for polar and spherical related functions.
james-pre Sep 4, 2022
55372dd
Merge pull request #2 from BabylonJS/master
james-pre Sep 7, 2022
226b012
Upates
james-pre Sep 7, 2022
006be27
Moved Spherical and Polar to math.polar.ts
james-pre Sep 7, 2022
e9e5c8f
Merge pull request #3 from BabylonJS/master
james-pre Sep 7, 2022
31dbcbf
Removed FromPolar/FromSpherical
james-pre Sep 7, 2022
0ae0d37
Whitespace
james-pre Sep 7, 2022
b69853f
Change Spherical length to radius, fixed PitchYawRollForDirectionChan…
james-pre Sep 8, 2022
239a961
Removed PitchYawRollForDirectionChangeToRef offset (unneeded)
james-pre Sep 8, 2022
0e19159
Fixed variable order
james-pre Sep 8, 2022
8f1dcd9
Fixed theta/phi equations
james-pre Sep 8, 2022
18bccec
Removed need for PitchYawRollForDirectionChange in math.polar.ts
james-pre Sep 8, 2022
932d6a8
Added Spherical typedoc comment
james-pre Sep 8, 2022
ae157ab
Merge pull request #4 from BabylonJS/master
james-pre Sep 8, 2022
62f1539
atan -> atan2
james-pre Sep 8, 2022
9e3885d
Merge pull request #5 from BabylonJS/master
james-pre Sep 8, 2022
aa43fb4
Remove Vector.GetAngleBetweenVectors, changed Spherical.FromVector3To…
james-pre Sep 9, 2022
c1f6209
Merge pull request #6 from BabylonJS/master
james-pre Sep 9, 2022
7de4f7a
Fixed Spherical.FromVector3ToRef
james-pre Sep 9, 2022
a520e90
PitchYawRollForDirectionChangeToRef
james-pre Sep 9, 2022
16a0155
atan2 for both theta and phi
james-pre Sep 12, 2022
435ec60
Removed uneeded line
james-pre Sep 12, 2022
382a50c
Fixed toVector3toRef, Removed pitchYawRoll
james-pre Sep 13, 2022
0119e2c
Fixed issues Sebavan pointed out
james-pre Sep 13, 2022
a1e768f
Formatting
james-pre Sep 13, 2022
908bc30
Formatting (spaces)
james-pre Sep 13, 2022
9d9d81a
Formatting (let -> const)
james-pre Sep 14, 2022
5b33ab7
Formatting (literally 1 extra line at the end)
james-pre Sep 14, 2022
22c429d
The extra line, again?
james-pre Sep 14, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 70 additions & 2 deletions packages/dev/core/src/Maths/math.vector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ export class Vector2 {
return result;
}

/**
james-pre marked this conversation as resolved.
Show resolved Hide resolved
* Gets the polar coordinates of the current Vector2
* @returns a new Vector2 with the polar coordinates
*/
public toPolar(): Vector2{
let theta = Vector2.GetAngleBetweenVectors(Vector2.Zero(), this)
return new Vector2(this.length(), theta);
}

/**
* Sets the Vector2 coordinates with the given Vector2 coordinates
* @param source defines the source Vector2
Expand Down Expand Up @@ -482,6 +491,28 @@ export class Vector2 {
result.y = array[offset + 1];
}

/**
* Gets the angle between 2 vectors
* @param vector0 defines 1st vector
* @param vector1 defines 2nd vector
* @returns the angle between vector0 and vector1
*/
public static GetAngleBetweenVectors(vector0: Vector2, vector1: Vector2): Number{
let diff = vector1.subtract(vector0);
james-pre marked this conversation as resolved.
Show resolved Hide resolved
return Math.atan(diff.y / diff.x);
}

/**
* Converts a polar coordinate Vector2 to its rectangular coordinates
james-pre marked this conversation as resolved.
Show resolved Hide resolved
* @param polar the polar coordinates
* @returns the rectangular coordinates
*/
public static fromPolar(polar: Vector2): Vector2{
let x = polar.x * Math.cos(polar.y);
let y = polar.x * Math.sin(polar.y);
return new Vector2(x, y);
}

/**
* Gets a new Vector2 located for "amount" (float) on the CatmullRom spline defined by the given four Vector2
* @param value1 defines 1st point of control
Expand Down Expand Up @@ -943,6 +974,15 @@ export class Vector3 {
return Quaternion.RotationYawPitchRoll(this._y, this._x, this._z);
}

/**
* Converts the current Vector3 to its polar coordinates
james-pre marked this conversation as resolved.
Show resolved Hide resolved
* @returns the current polar coordinates (r, theta, phi)
*/
public toPolar(): Vector3{
james-pre marked this conversation as resolved.
Show resolved Hide resolved
james-pre marked this conversation as resolved.
Show resolved Hide resolved
let angles = Vector3.GetRotationBetweenVectors(Vector3.Zero(), this);
james-pre marked this conversation as resolved.
Show resolved Hide resolved
return new Vector3(this.length(), angles.x, angles.y);
}

/**
* Adds the given vector to the current Vector3
* Example Playground https://playground.babylonjs.com/#R1F8YU#4
Expand Down Expand Up @@ -1661,11 +1701,39 @@ export class Vector3 {
return s;
}

/**
* Gets the rectangular coordinates of a Vector3 from its polar coordinates
* @param theta defines the angle for the vertical
* @param phi defines the angle for the horizontal
* @param length defines the length of the Vector3 (in polar coordinate space)
* @returns the rectangular coordinate Vector3
*/
public static FromPolar(theta: Number, phi: Number, length: Number): Vector3{
let x = length * Math.sin(theta) * Math.cos(phi);
let y = length * Math.sin(theta) * Math.sin(phi);
let z = length * Math.cos(theta);
return new Vector3(x, y, z);
}

/**
* Get rotation between two vectors
* @param origin defines the starting point
* @param target defines the ending point
* @returns the rotation between the vectors
*/
public static GetRotationBetweenVectors(origin: DeepImmutable<Vector3>, target: DeepImmutable<Vector3>): Vector3{
sebavan marked this conversation as resolved.
Show resolved Hide resolved
let diff: Vector3 = target.subtract(origin),
distance = Math.sqrt(diff.x**2 + diff.y**2 + diff.z**2),
phi = Math.acos(diff.z / distance) || 0,
theta = Math.asin(diff.y / (Math.sin(phi) * distance)) || 0;
return new Vector3(theta, Math.sign(diff.x || 1) * phi, 0);
}

/**
* Get angle between two vectors
* Example Playground https://playground.babylonjs.com/#R1F8YU#86
* @param vector0 angle between vector0 and vector1
* @param vector1 angle between vector0 and vector1
* @param vector0 the starting point
* @param vector1 the ending point
* @param normal direction of the normal
* @return the angle between vector0 and vector1
*/
Expand Down