Skip to content

Commit

Permalink
Added gravity vector, modified all logic to support up/down correctly…
Browse files Browse the repository at this point in the history
… with any direction
  • Loading branch information
Marc Flerackers committed May 27, 2024
1 parent 85ed508 commit 3844363
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- added support for texture larger than 2048x2048
- added `chooseMultiple()` and `shuffle()` helper functions
- added `getSceneName()` to get the current scene name
- added support for gravity direction

### v3000.1.17

Expand Down
43 changes: 28 additions & 15 deletions src/components/physics/body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ export function body(opt: BodyCompOpt = {}): BodyComp {
this.onPhysicsResolve((col) => {
if (internal.game.gravity) {
if (col.isBottom() && this.isFalling()) {
this.vel.y = 0;
this.vel = this.vel.reject(
internal.game.gravity.unit(),
);
curPlatform = col.target as GameObj<
PosComp | BodyComp | AreaComp
>;
Expand All @@ -102,7 +104,9 @@ export function body(opt: BodyCompOpt = {}): BodyComp {
this.trigger("ground", curPlatform);
}
} else if (col.isTop() && this.isJumping()) {
this.vel.y = 0;
this.vel = this.vel.reject(
internal.game.gravity.unit(),
);
this.trigger("headbutt", col.target);
}
}
Expand All @@ -119,7 +123,7 @@ export function body(opt: BodyCompOpt = {}): BodyComp {
willFall = false;
}

let updateY = true;
let addGravity = true;

if (curPlatform) {
if (
Expand All @@ -139,21 +143,28 @@ export function body(opt: BodyCompOpt = {}): BodyComp {
);
}
lastPlatformPos = curPlatform.pos;
updateY = false;
addGravity = false;
}
}

if (updateY) {
const prevVelY = this.vel.y;
if (addGravity) {
const prevVel = this.vel.clone();

this.vel.y += internal.game.gravity * this.gravityScale
* k.dt();
this.vel.y = Math.min(
this.vel.y,
opt.maxVelocity ?? MAX_VEL,
// Apply gravity
this.vel = this.vel.add(
internal.game.gravity.scale(this.gravityScale * k.dt()),
);

if (prevVelY < 0 && this.vel.y >= 0) {
// Clamp velocity
const maxVel = opt.maxVelocity ?? MAX_VEL;
if (this.vel.slen() > maxVel * maxVel) {
this.vel = this.vel.unit().scale(maxVel);
}

if (
prevVel.dot(internal.game.gravity) < 0
&& this.vel.dot(internal.game.gravity) >= 0
) {
this.trigger("fall");
}
}
Expand Down Expand Up @@ -182,17 +193,19 @@ export function body(opt: BodyCompOpt = {}): BodyComp {
},

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

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

jump(force: number) {
curPlatform = null;
lastPlatformPos = null;
this.vel.y = -force || -this.jumpForce;
this.vel = internal.game.gravity.unit().scale(
-force || -this.jumpForce,
);
},

onGround(this: GameObj, action: () => void): EventController {
Expand Down
24 changes: 17 additions & 7 deletions src/kaboom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ export default (gopt: KaboomOpt = {}): KaboomCtx => {
root: make([]),

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

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

function setGravity(g: number) {
game.gravity = g;
game.gravity = game.gravity.unit().scale(g);
}

function getGravity() {
return game.gravity;
return game.gravity.len();
}

function setGravityDirection(d: Vec2) {
game.gravity = d.unit().scale(game.gravity.len());
}

function getGravityDirection() {
return game.gravity.unit();
}

function setBackground(...args) {
Expand Down Expand Up @@ -4249,16 +4257,16 @@ export default (gopt: KaboomOpt = {}): KaboomCtx => {
return !this.displacement.isZero();
}
isLeft() {
return this.displacement.x > 0;
return this.displacement.cross(game.gravity) > 0;
}
isRight() {
return this.displacement.x < 0;
return this.displacement.cross(game.gravity) < 0;
}
isTop() {
return this.displacement.y > 0;
return this.displacement.dot(game.gravity) > 0;
}
isBottom() {
return this.displacement.y < 0;
return this.displacement.dot(game.gravity) < 0;
}
preventResolution() {
this.resolved = true;
Expand Down Expand Up @@ -4983,6 +4991,8 @@ export default (gopt: KaboomOpt = {}): KaboomCtx => {
toWorld,
setGravity,
getGravity,
setGravityDirection,
getGravityDirection,
setBackground,
getBackground,
getGamepads: app.getGamepads,
Expand Down
8 changes: 8 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1903,6 +1903,14 @@ export interface KaboomCtx {
* @group Info
*/
getGravity(): number;
/**
* Set gravity direction.
*/
setGravityDirection(d: Vec2): void;
/**
* Get gravity direction.
*/
getGravityDirection(): Vec2;
/**
* Set background color.
*
Expand Down

0 comments on commit 3844363

Please sign in to comment.