-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
Conversation
Please make sure to label your PR with "bug", "new feature" or "breaking change" label(s). |
Please make sure to label your PR with "bug", "new feature" or "breaking change" label(s). |
There was a problem hiding this 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.
There was a problem hiding this 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
Please make sure to label your PR with "bug", "new feature" or "breaking change" label(s). |
I've updated the code to have separate classes for both coordinate systems |
Please make sure to label your PR with "bug", "new feature" or "breaking change" label(s). |
As can been seen the use of 'GetAngleBetweenVectors' and 'GetRotationBetweenVectors' or is it 'RotationBetweenVectors' or are they the same or different, 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. |
Please make sure to label your PR with "bug", "new feature" or "breaking change" label(s). |
Please make sure to label your PR with "bug", "new feature" or "breaking change" label(s). |
@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 As for the value of phi, |
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. |
@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. |
Experimenting with the function, the theta function |
Please make sure to label your PR with "bug", "new feature" or "breaking change" label(s). |
For an acceptable PR a function should be beyond experimenting.
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 ? |
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. |
The Polar class is working https://playground.babylonjs.com/#B1WXN8 @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)
as per doc page above. |
pitchYawRoll will be a different PR
Please make sure to label your PR with "bug", "new feature" or "breaking change" label(s). |
Snapshot stored with reference name: Test environment: To test a playground add it to the URL, for example: https://babylonsnapshots.z22.web.core.windows.net/refs/pull/12942/merge/index.html#WGZLGJ#4600 Links to test babylon tools with this snapshot: https://playground.babylonjs.com/?snapshot=refs/pull/12942/merge To test the snapshot in the playground with a playground ID add it after the snapshot query string: https://playground.babylonjs.com/?snapshot=refs/pull/12942/merge#BCU1XR#0 |
@sebavan |
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?!
@RaananW can you think of smthg else to help @dr-vortex with the formatting ? |
Hm... I cloned my fork, then did |
@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. |
the best option is |
* 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
Also fixes a misspelling with the
Vector3.GetAngleBetweenVectors
jsdoc parameters