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

Vec2.rotate(degrees), Vec2.angle, Vec2.angleTo methods #5622

Merged
merged 9 commits into from Sep 18, 2023

Conversation

Maksims
Copy link
Contributor

@Maksims Maksims commented Sep 11, 2023

Adds two methods to Vec2 to help with converting to radians and rotating it.

New APIs:

// pc.Vec2
vec.angle() // returns Euler angle of a vector
vec.angleTo(vector) // returns shortest Euler angle from one vector to another
vec.rotate(degrees) // rotate a vector by degrees (by Euler angle)

I confirm I have read the contributing guidelines and signed the Contributor License Agreement.

Copy link
Contributor

@mvaligursky mvaligursky left a comment

Choose a reason for hiding this comment

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

I think rotate should take an angle in degrees, like all public facing functionality in math.
And the radians should be get angle that returns the angle in degrees as well.

@Maksims
Copy link
Contributor Author

Maksims commented Sep 11, 2023

I think rotate should take an angle in degrees, like all public facing functionality in math. And the radians should be get angle that returns the angle in degrees as well.

Good point. I've updated the PR and added additional method: angleTo.

src/core/math/vec2.js Outdated Show resolved Hide resolved
@mvaligursky
Copy link
Contributor

Also, please update the PR's title.

@Maksims Maksims changed the title Vec2.rotate(radians) and Vec2.radians methods Vec2.rotate(degrees), Vec2.angle, Vec2.angleTo methods Sep 12, 2023
@slimbuck
Copy link
Member

Could you add a src = this param to rotate()? I did this for some vec2, vec3 and quat functions recently, eg https://github.com/playcanvas/engine/blob/main/src/core/math/vec2.js#L416. It makes the interface a little more consistent, a little more flexible and in some cases will result in one fewer vector copy.

@Maksims
Copy link
Contributor Author

Maksims commented Sep 12, 2023

@slimbuck, not sure where would src argument would go in rotate method. As this method is within semantics of mulScalar or addScalar, where only modifier argument is provided.

@slimbuck
Copy link
Member

It would go like this:

    rotate(degrees, src = this) {
        const angle = Math.atan2(src.x, src.y) + (degrees * math.DEG_TO_RAD);
        const len = Math.sqrt(src.x * src.x + src.y * src.y);
        this.x = Math.sin(angle) * len;
        this.y = Math.cos(angle) * len;
        return this;
    }

You're right rotate could be considered closer to mulScalar than normalize since rotate takes takes a parameter like mulScalar, but I'd say we could easily add src parameter to mulScalar one day too, why not?

@Maksims
Copy link
Contributor Author

Maksims commented Sep 12, 2023

but I'd say we could easily add src parameter to mulScalar one day too, why not?

Sure, shall then this be left as is, to ensure consistency. And if src to be added to similar methods, that would come from a future PR?

@slimbuck
Copy link
Member

Sure :)

src/core/math/vec2.js Outdated Show resolved Hide resolved
src/core/math/vec2.js Outdated Show resolved Hide resolved
src/core/math/vec2.js Outdated Show resolved Hide resolved
src/core/math/vec2.js Outdated Show resolved Hide resolved
src/core/math/vec2.js Outdated Show resolved Hide resolved
src/core/math/vec2.js Outdated Show resolved Hide resolved
* // Outputs 90..
* console.log("The angle of the vector is: " + angle);
*/
angle() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Question: should this be a function or a getter?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I believe it should be a function, to stay consistent with similar methods, such as: length()

Copy link
Contributor

Choose a reason for hiding this comment

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

That's a good and fair point. But I'm wondering whether length, for example, should also be a getter these days. If we do decide to leverage getters in the math API, that's for another PR... 😄

Maksims and others added 6 commits September 17, 2023 21:29
Co-authored-by: Will Eastcott <will@playcanvas.com>
Co-authored-by: Will Eastcott <will@playcanvas.com>
Co-authored-by: Will Eastcott <will@playcanvas.com>
Co-authored-by: Will Eastcott <will@playcanvas.com>
Co-authored-by: Will Eastcott <will@playcanvas.com>
Co-authored-by: Will Eastcott <will@playcanvas.com>
@mvaligursky mvaligursky merged commit 13a2be2 into playcanvas:main Sep 18, 2023
7 checks passed
@Maksims Maksims deleted the vec2-rotate branch September 18, 2023 09:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants