Skip to content

Commit

Permalink
feat: [#3171] normalize zero vector (#3179)
Browse files Browse the repository at this point in the history
Closes #3171

## Changes:

- `Vector.normalize()` return zero-vector (`(0,0)`) instead of `(0,1)` when normalizing a vector with a magnitude of 0
  • Loading branch information
Autsider666 authored Aug 27, 2024
1 parent 18d3063 commit 46ba314
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).

### Breaking Changes

- `ex.Vector.normalize()` return zero-vector (`(0,0)`) instead of `(0,1)` when normalizing a vector with a magnitude of 0
- `ex.Gif` transparent color constructor arg is removed in favor of the built in Gif file mechanism
- Remove core-js dependency, it is no longer necessary in modern browsers. Technically a breaking change for older browsers
- `ex.Particle` and `ex.ParticleEmitter` now have an API that looks like modern Excalibur APIs
Expand Down
12 changes: 6 additions & 6 deletions src/engine/Math/vector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,15 +227,15 @@ export class Vector implements Clonable<Vector> {
}

/**
* Normalizes a vector to have a magnitude of 1.
* Normalizes a non-zero vector to have a magnitude of 1. Zero vectors return a new zero vector.
*/
public normalize(): Vector {
const d = this.distance();
if (d > 0) {
return new Vector(this.x / d, this.y / d);
} else {
return new Vector(0, 1);
const distance = this.distance();
if (distance === 0) {
return Vector.Zero;
}

return new Vector(this.x / distance, this.y / distance);
}

/**
Expand Down

0 comments on commit 46ba314

Please sign in to comment.