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

fix: Fixes 0 gravity #66

Merged
merged 1 commit into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -3337,19 +3337,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 @@ -4224,16 +4230,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