Skip to content

Commit 3844363

Browse files
author
Marc Flerackers
committed
Added gravity vector, modified all logic to support up/down correctly with any direction
1 parent 85ed508 commit 3844363

File tree

4 files changed

+54
-22
lines changed

4 files changed

+54
-22
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- added support for texture larger than 2048x2048
99
- added `chooseMultiple()` and `shuffle()` helper functions
1010
- added `getSceneName()` to get the current scene name
11+
- added support for gravity direction
1112

1213
### v3000.1.17
1314

src/components/physics/body.ts

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ export function body(opt: BodyCompOpt = {}): BodyComp {
9191
this.onPhysicsResolve((col) => {
9292
if (internal.game.gravity) {
9393
if (col.isBottom() && this.isFalling()) {
94-
this.vel.y = 0;
94+
this.vel = this.vel.reject(
95+
internal.game.gravity.unit(),
96+
);
9597
curPlatform = col.target as GameObj<
9698
PosComp | BodyComp | AreaComp
9799
>;
@@ -102,7 +104,9 @@ export function body(opt: BodyCompOpt = {}): BodyComp {
102104
this.trigger("ground", curPlatform);
103105
}
104106
} else if (col.isTop() && this.isJumping()) {
105-
this.vel.y = 0;
107+
this.vel = this.vel.reject(
108+
internal.game.gravity.unit(),
109+
);
106110
this.trigger("headbutt", col.target);
107111
}
108112
}
@@ -119,7 +123,7 @@ export function body(opt: BodyCompOpt = {}): BodyComp {
119123
willFall = false;
120124
}
121125

122-
let updateY = true;
126+
let addGravity = true;
123127

124128
if (curPlatform) {
125129
if (
@@ -139,21 +143,28 @@ export function body(opt: BodyCompOpt = {}): BodyComp {
139143
);
140144
}
141145
lastPlatformPos = curPlatform.pos;
142-
updateY = false;
146+
addGravity = false;
143147
}
144148
}
145149

146-
if (updateY) {
147-
const prevVelY = this.vel.y;
150+
if (addGravity) {
151+
const prevVel = this.vel.clone();
148152

149-
this.vel.y += internal.game.gravity * this.gravityScale
150-
* k.dt();
151-
this.vel.y = Math.min(
152-
this.vel.y,
153-
opt.maxVelocity ?? MAX_VEL,
153+
// Apply gravity
154+
this.vel = this.vel.add(
155+
internal.game.gravity.scale(this.gravityScale * k.dt()),
154156
);
155157

156-
if (prevVelY < 0 && this.vel.y >= 0) {
158+
// Clamp velocity
159+
const maxVel = opt.maxVelocity ?? MAX_VEL;
160+
if (this.vel.slen() > maxVel * maxVel) {
161+
this.vel = this.vel.unit().scale(maxVel);
162+
}
163+
164+
if (
165+
prevVel.dot(internal.game.gravity) < 0
166+
&& this.vel.dot(internal.game.gravity) >= 0
167+
) {
157168
this.trigger("fall");
158169
}
159170
}
@@ -182,17 +193,19 @@ export function body(opt: BodyCompOpt = {}): BodyComp {
182193
},
183194

184195
isFalling(): boolean {
185-
return this.vel.y > 0;
196+
return this.vel.dot(internal.game.gravity) > 0;
186197
},
187198

188199
isJumping(): boolean {
189-
return this.vel.y < 0;
200+
return this.vel.dot(internal.game.gravity) < 0;
190201
},
191202

192203
jump(force: number) {
193204
curPlatform = null;
194205
lastPlatformPos = null;
195-
this.vel.y = -force || -this.jumpForce;
206+
this.vel = internal.game.gravity.unit().scale(
207+
-force || -this.jumpForce,
208+
);
196209
},
197210

198211
onGround(this: GameObj, action: () => void): EventController {

src/kaboom.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ export default (gopt: KaboomOpt = {}): KaboomCtx => {
705705
root: make([]),
706706

707707
// misc
708-
gravity: 0,
708+
gravity: vec2(0, 1),
709709
scenes: {},
710710
currentScene: null,
711711

@@ -3370,11 +3370,19 @@ export default (gopt: KaboomOpt = {}): KaboomCtx => {
33703370
}
33713371

33723372
function setGravity(g: number) {
3373-
game.gravity = g;
3373+
game.gravity = game.gravity.unit().scale(g);
33743374
}
33753375

33763376
function getGravity() {
3377-
return game.gravity;
3377+
return game.gravity.len();
3378+
}
3379+
3380+
function setGravityDirection(d: Vec2) {
3381+
game.gravity = d.unit().scale(game.gravity.len());
3382+
}
3383+
3384+
function getGravityDirection() {
3385+
return game.gravity.unit();
33783386
}
33793387

33803388
function setBackground(...args) {
@@ -4249,16 +4257,16 @@ export default (gopt: KaboomOpt = {}): KaboomCtx => {
42494257
return !this.displacement.isZero();
42504258
}
42514259
isLeft() {
4252-
return this.displacement.x > 0;
4260+
return this.displacement.cross(game.gravity) > 0;
42534261
}
42544262
isRight() {
4255-
return this.displacement.x < 0;
4263+
return this.displacement.cross(game.gravity) < 0;
42564264
}
42574265
isTop() {
4258-
return this.displacement.y > 0;
4266+
return this.displacement.dot(game.gravity) > 0;
42594267
}
42604268
isBottom() {
4261-
return this.displacement.y < 0;
4269+
return this.displacement.dot(game.gravity) < 0;
42624270
}
42634271
preventResolution() {
42644272
this.resolved = true;
@@ -4983,6 +4991,8 @@ export default (gopt: KaboomOpt = {}): KaboomCtx => {
49834991
toWorld,
49844992
setGravity,
49854993
getGravity,
4994+
setGravityDirection,
4995+
getGravityDirection,
49864996
setBackground,
49874997
getBackground,
49884998
getGamepads: app.getGamepads,

src/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1903,6 +1903,14 @@ export interface KaboomCtx {
19031903
* @group Info
19041904
*/
19051905
getGravity(): number;
1906+
/**
1907+
* Set gravity direction.
1908+
*/
1909+
setGravityDirection(d: Vec2): void;
1910+
/**
1911+
* Get gravity direction.
1912+
*/
1913+
getGravityDirection(): Vec2;
19061914
/**
19071915
* Set background color.
19081916
*

0 commit comments

Comments
 (0)