Skip to content

Commit

Permalink
Add static Vec2.normalize
Browse files Browse the repository at this point in the history
  • Loading branch information
shakiba committed Dec 23, 2024
1 parent bee0e16 commit 922ff97
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/clever-pants-carry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"planck": patch
---

Add static Vec2.normalize
14 changes: 14 additions & 0 deletions src/common/Vec2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,20 @@ export class Vec2 {
return length;
}

/**
* Returns a new unit vector from the provided vector.
*
* @returns new unit vector
*/
static normalize(v: Vec2Value): Vec2 {
const length = Vec2.lengthOf(v);
if (length < EPSILON) {
return Vec2.zero();
}
const invLength = 1.0 / length;
return Vec2.neo(v.x * invLength, v.y * invLength);
}

/**
* Get the length of this vector (the norm).
*
Expand Down

0 comments on commit 922ff97

Please sign in to comment.