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

Add polar and spherical coordinate system support #12942

merged 34 commits into from
Sep 14, 2022

Conversation

james-pre
Copy link
Contributor

Also fixes a misspelling with the Vector3.GetAngleBetweenVectors jsdoc parameters

@azure-pipelines
Copy link

Please make sure to label your PR with "bug", "new feature" or "breaking change" label(s).
To prevent this PR from going to the changelog marked it with the "skip changelog" label.

@azure-pipelines
Copy link

Please make sure to label your PR with "bug", "new feature" or "breaking change" label(s).
To prevent this PR from going to the changelog marked it with the "skip changelog" label.

Copy link
Contributor

@BabylonJSGuide BabylonJSGuide left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning polar coordinates as a Vector2 is confusing. If Polar was to be implemented it should have its own class with properties Polar.radius, Polar.angle and methods Polar.toVector2, Polar.fromVector2. However since polar coordinates can be obtained simply from a Vector 2, V, using

radius = V.length();
angle = Math.sign(V.x) * Math.acos(V.x / radius) for -π ≤ angle ≤ π and add π if you want 0 ≤ angle ≤ 2π

the question is whether it is worth doing.

Copy link
Contributor

@BabylonJSGuide BabylonJSGuide left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again returning polar (actually spherical) coordinates as a Vector3 is confusing and would require a new class Spherical with properties radius, theta, phi where theta is the inclination and phi the azimuth together with appropriate methods.

Obtaining spherical from a Vector3 V could be achieved by

radius = V.length();
theta = Math.acos(V.z / radius) 0 ≤ theta ≤ π

getPhi would need a function

const getPhi = (V) => {
    if (V.x === 0) {
        if (V.y > 0) {
            return Math.PI / 2
        }
        else if (V.y < 0) {
            return -Math.PI / 2;
        }
        else return NaN;
    }
    if (V.x > 0 ) {
        return Math.atan(V.y / V.x)
    }
    if (V.y >= 0) {
            return Math.atan(V.y / V.x) + Math.PI;
    }
    else {
        return Math.atan(V.y / V.x) - Math.PI
    }
}

Whether implementing or not there is no need need for the extra and confusing getRotationBetweenVectors

@azure-pipelines
Copy link

Please make sure to label your PR with "bug", "new feature" or "breaking change" label(s).
To prevent this PR from going to the changelog marked it with the "skip changelog" label.

@james-pre james-pre changed the title Add polar coordinate conversions to Vector2 and Vector3 Add polar and spherical coordinate system support Sep 4, 2022
@james-pre
Copy link
Contributor Author

@BabylonJSGuide,

I've updated the code to have separate classes for both coordinate systems

@azure-pipelines
Copy link

Please make sure to label your PR with "bug", "new feature" or "breaking change" label(s).
To prevent this PR from going to the changelog marked it with the "skip changelog" label.

packages/dev/core/src/Maths/math.vector.ts Outdated Show resolved Hide resolved
packages/dev/core/src/Maths/math.vector.ts Outdated Show resolved Hide resolved
packages/dev/core/src/Maths/math.vector.ts Outdated Show resolved Hide resolved
packages/dev/core/src/Maths/math.vector.ts Outdated Show resolved Hide resolved
packages/dev/core/src/Maths/math.vector.ts Outdated Show resolved Hide resolved
@BabylonJSGuide
Copy link
Contributor

BabylonJSGuide commented Sep 4, 2022

As can been seen the use of 'GetAngleBetweenVectors' and 'GetRotationBetweenVectors' or is it 'RotationBetweenVectors' or are they the same or different, whether static or a method , over complicates the coding and are unnecessary for Polar or Spherical coordinates.

Confusion over static or method was mine.

toSpherical could be achieved by including a, probably smarter version of the getPhi, function above simplifying the code.

@azure-pipelines
Copy link

Please make sure to label your PR with "bug", "new feature" or "breaking change" label(s).
To prevent this PR from going to the changelog marked it with the "skip changelog" label.

@azure-pipelines
Copy link

Please make sure to label your PR with "bug", "new feature" or "breaking change" label(s).
To prevent this PR from going to the changelog marked it with the "skip changelog" label.

@james-pre
Copy link
Contributor Author

@BabylonJSGuide and @Popov72,

Thank you both for all the feedback. I've added toRef functions for all the related functions.

As for the (so far) controversial RotationBetweenVectors, the function is very useful for determining the amount of rotation on all axes between to Vector3s. Vector3.GetAngleBetweenVectors doesn't do this, it instead finds only 1 angle relative to a plane, which complicates things on the user's end. RotationBetweenVectors is designed as an easy way for developers to get the full rotation without doing 3D geometry.

As for the value of phi, RotationBetweenVectors returns the correct values in all cases I've tried. The function does not convert co-terminal angles back to their 0 < θ < 2π form and treats 0/0 as 0 not NaN, which is intended.

@BabylonJSGuide
Copy link
Contributor

BabylonJSGuide commented Sep 10, 2022

the cone would face the target vector

Do not understand what this means. What part of the cone would face the target? The idea of using a cone is that the tip of the cone indicates what is pointing to the target. Are you saying that the target can never be on the y axis?

Imagine the cone as some sort of spaceship with the tip pointing forward. So in the lower image the fin is vertical and the spaceship is moving left, the diagonal white line gives the direction for the second vector. Is this a possible scenario?

Using the tip of the cone as that which must face the target please describe or produce an image of a possible scenario.

@BabylonJSGuide
Copy link
Contributor

BabylonJSGuide commented Sep 12, 2022

@dr-vortex I will not be able to add an accepting review until I understand the purpose of your function 'PitchYawRollForDirectionChangeToRef' and that it works as you expect. In the following PG I have set up the asym cone so that the tip of the cone is in the forward position and the fin is up and by baking the transformation it currently has rotation zero.

https://playground.babylonjs.com/#QYA3JC#2

Using the tip and fin in your description please describe the purpose of your function and demonstrate how it works and that it does work as expected by adding it to the PG and applying it to the asym cone.

@james-pre
Copy link
Contributor Author

Experimenting with the function, the theta function atan2(z, x) should be atan2(x, z), and we can use atan2 instead of asin for phi. I've made this PG that demonstrates the function with an animation.

@azure-pipelines
Copy link

Please make sure to label your PR with "bug", "new feature" or "breaking change" label(s).
To prevent this PR from going to the changelog marked it with the "skip changelog" label.

@BabylonJSGuide
Copy link
Contributor

BabylonJSGuide commented Sep 13, 2022

Experimenting with the function

For an acceptable PR a function should be beyond experimenting.

Using the tip and fin in your description please describe the purpose of your function.

Please include the description as above. Is the intention it only works with paths in the XZ plane?

For 3D paths is the following behaving as expected https://playground.babylonjs.com/#QYA3JC#6 ?

@james-pre
Copy link
Contributor Author

No, the function should work without a plane. The animation just sets the rotation at every increment of the frame rate (-some) to the rotation between the last node and the current node in the path. I believe the issue with the cone not facing in the direction of travel is an oversight of the animation, not the function.

@BabylonJSGuide
Copy link
Contributor

BabylonJSGuide commented Sep 13, 2022

The Polar class is working https://playground.babylonjs.com/#B1WXN8
With the change required above Spherical is working https://playground.babylonjs.com/#ML0BEE#1

@dr-vortex my suggestion is that for the moment your PR is only for math.polar.ts and any changes to Vector2 and Vector3 in the math.vector.ts are removed from the PR which I could then approve. Once you know for sure your functions and animations are working as you require then submit a PR with with the working functions.

@sebavan will know more but in terms of the build failures then you need to look at https://doc.babylonjs.com/divingDeeper/developWithBjs/howToStart and check your local build. I think the common failures are not putting an entry into 'whats new', not commenting on parameters and not having the required format such as extra space, extra lines etc, you can check this with (I think)

npm run format:fix

as per doc page above.

@sebavan
Copy link
Member

sebavan commented Sep 13, 2022

Thanks both for all the love you provide for this feature !!!

Yup, about the build, the format fix will help a lot, and the rest of the issues are:

image

@azure-pipelines
Copy link

Please make sure to label your PR with "bug", "new feature" or "breaking change" label(s).
To prevent this PR from going to the changelog marked it with the "skip changelog" label.

@azure-pipelines
Copy link

@james-pre
Copy link
Contributor Author

@sebavan
What settings do you use for prettier? I'd like to format it correctly but don't know what settings to use.

@sebavan
Copy link
Member

sebavan commented Sep 13, 2022

they are enclosed in the project but the simpler is to run

npm run format:fix

Done without npm run format:fix
Why do we use spaces instead of actual tabs?!
@sebavan
Copy link
Member

sebavan commented Sep 14, 2022

@RaananW can you think of smthg else to help @dr-vortex with the formatting ?

@james-pre
Copy link
Contributor Author

Hm...

I cloned my fork, then did npm run format:fix which pointed out that some lets should be consts, I fixed that, then ran it again along with format document (Ctrl + Shift + I in VS Code). No changes or errors the second time.

@james-pre
Copy link
Contributor Author

@sebavan and @BabylonJSGuide,

Ready to merge, finally! Thank you both for all the help and feedback! I will start a draft PR and work on some operator functions (add, subtract, etc.) for Polar and Spherical.

@RaananW
Copy link
Member

RaananW commented Sep 14, 2022

@RaananW can you think of smthg else to help @dr-vortex with the formatting ?

the best option is npm run format:fix or the prettify extension for your favorite IDE

@RaananW RaananW merged commit 5690d68 into BabylonJS:master Sep 14, 2022
RaananW pushed a commit that referenced this pull request Dec 9, 2022
* Add polar coordinate conversions to Vector2 and Vector3

* Fixed Vector3.toPolar to return length

* Added seperate class for Polar and Spherical coordinate systems

* Fixed pascal case, terminology, and theta issue

* Added toRef for polar and spherical related functions.

* Upates

* Moved Spherical and Polar to math.polar.ts

* Removed FromPolar/FromSpherical

* Whitespace

* Change Spherical length to radius, fixed PitchYawRollForDirectionChangeToRef bug.

* Removed PitchYawRollForDirectionChangeToRef offset (unneeded)

* Fixed variable order

* Fixed theta/phi equations

* Removed need for PitchYawRollForDirectionChange in math.polar.ts

* Added Spherical typedoc comment

* atan -> atan2

* Remove Vector.GetAngleBetweenVectors, changed Spherical.FromVector3ToRef to physics convention

* Fixed Spherical.FromVector3ToRef

* PitchYawRollForDirectionChangeToRef
atan -> atan2

* atan2 for both theta and phi

* Removed uneeded line

* Fixed toVector3toRef, Removed pitchYawRoll
pitchYawRoll will be a different PR

* Fixed issues Sebavan pointed out

* Formatting
Done without npm run format:fix

* Formatting (spaces)
Why do we use spaces instead of actual tabs?!

* Formatting (let -> const)

* Formatting (literally 1 extra line at the end)

* The extra line, again?

Former-commit-id: 714d150fac835d48c9e60755ee0721f8ce23cc28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants