Skip to content

Commit

Permalink
fix: Fixes 0 gravity (#66)
Browse files Browse the repository at this point in the history
Co-authored-by: Marc Flerackers <[email protected]>
  • Loading branch information
mflerackers and Marc Flerackers authored May 28, 2024
1 parent 40e5d8c commit 6a246db
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/components/physics/body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,17 +193,17 @@ export function body(opt: BodyCompOpt = {}): BodyComp {
},

isFalling(): boolean {
return this.vel.dot(internal.game.gravity) > 0;
return this.vel.dot(k.getGravityDirection()) > 0;
},

isJumping(): boolean {
return this.vel.dot(internal.game.gravity) < 0;
return this.vel.dot(k.getGravityDirection()) < 0;
},

jump(force: number) {
curPlatform = null;
lastPlatformPos = null;
this.vel = internal.game.gravity.unit().scale(
this.vel = k.getGravityDirection().scale(
-force || -this.jumpForce,
);
},
Expand Down
24 changes: 15 additions & 9 deletions src/kaboom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ export default (gopt: KaboomOpt = {}): KaboomCtx => {
root: make([]),

// misc
gravity: vec2(0, 1),
gravity: null,
scenes: {},
currentScene: null,

Expand Down Expand Up @@ -3677,19 +3677,25 @@ export default (gopt: KaboomOpt = {}): KaboomCtx => {
}

function setGravity(g: number) {
game.gravity = game.gravity.unit().scale(g);
// If g > 0 use either the current direction or use (0, 1)
// Else null
game.gravity = g ? (game.gravity || vec2(0, 1)).unit().scale(g) : null;
}

function getGravity() {
return game.gravity.len();
// If gravity > 0 return magnitude
// Else 0
return game.gravity ? game.gravity.len() : 0;
}

function setGravityDirection(d: Vec2) {
game.gravity = d.unit().scale(game.gravity.len());
// If gravity > 0 keep magnitude, otherwise use 1
game.gravity = d.unit().scale(game.gravity ? game.gravity.len() : 1);
}

function getGravityDirection() {
return game.gravity.unit();
// If gravity > 0 return magnitude, otherwise return (0, 1)
return game.gravity ? game.gravity.unit() : vec2(0, 1);
}

function setBackground(...args) {
Expand Down Expand Up @@ -4564,16 +4570,16 @@ export default (gopt: KaboomOpt = {}): KaboomCtx => {
return !this.displacement.isZero();
}
isLeft() {
return this.displacement.cross(game.gravity) > 0;
return this.displacement.cross(game.gravity || vec2(0, 1)) > 0;
}
isRight() {
return this.displacement.cross(game.gravity) < 0;
return this.displacement.cross(game.gravity || vec2(0, 1)) < 0;
}
isTop() {
return this.displacement.dot(game.gravity) > 0;
return this.displacement.dot(game.gravity || vec2(0, 1)) > 0;
}
isBottom() {
return this.displacement.dot(game.gravity) < 0;
return this.displacement.dot(game.gravity || vec2(0, 1)) < 0;
}
preventResolution() {
this.resolved = true;
Expand Down

0 comments on commit 6a246db

Please sign in to comment.