Skip to content

Method to create Vec2d from magnitude and angle #261

@aatle

Description

@aatle

Often, I am given a magnitude and an angle in radians, and I need to create a vector from this.
The current ways to do it:

Vec2d(magnitude, 0.0).rotated(angle)
Vec2d(cos(angle), sin(angle)) * magnitude

It would be nice to have a classmethod that can do this more cleanly:

@classmethod
def from_polar(cls, r, theta):
    return cls(cos(theta) * r, sin(theta) * r)

Vec2d.from_polar(magnitude, angle)

I count 6 possible places I could use this in my codebase.
Example:
Apply an impulse of x magnitude, at an angle pointing towards the mouse cursor.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions