kippy 0.5.1 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +22 -1
- package/dist/game.js +2 -2
- package/dist/physics.d.ts +18 -1
- package/dist/physics.js +141 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -229,6 +229,27 @@ collider.layer; // Set with the matching parameter above, default is (1 << 0)
|
|
|
229
229
|
collider.mask; // Set with the matching parameter above, default is 0xFFFFFFFF
|
|
230
230
|
```
|
|
231
231
|
|
|
232
|
+
or a `BoxCollider`:
|
|
233
|
+
```js
|
|
234
|
+
const collider = new BoxCollider({
|
|
235
|
+
width, // Circle collider's width, type number
|
|
236
|
+
height, // Circle collider's height, type number
|
|
237
|
+
offset, // Offset from entity's position, type Vector2
|
|
238
|
+
isTrigger, // If true, trigger callbacks are called and collision physics like bouncing
|
|
239
|
+
// will not apply. Otherwise, collision callbacks are called and physics apply
|
|
240
|
+
layer, // A bit mask to determine what collision layer this collider is at
|
|
241
|
+
mask, // A bit mask to check what colliders to collide
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
// You can mutate these props to configure the box collider
|
|
245
|
+
collider.width; // Set with the matching parameter above, required
|
|
246
|
+
collider.height; // Set with the matching parameter above, required
|
|
247
|
+
collider.offset; // Set with the matching parameter above, default is Vector2(0, 0)
|
|
248
|
+
collider.isTrigger; // Set with the matching parameter above, default is false
|
|
249
|
+
collider.layer; // Set with the matching parameter above, default is (1 << 0)
|
|
250
|
+
collider.mask; // Set with the matching parameter above, default is 0xFFFFFFFF
|
|
251
|
+
```
|
|
252
|
+
|
|
232
253
|
And you can handle when two objects collide:
|
|
233
254
|
```js
|
|
234
255
|
collider.onCollisionEnter = (other, info) => {};
|
|
@@ -276,7 +297,7 @@ To be added, for now use web's built-in `Audio` class.
|
|
|
276
297
|
|
|
277
298
|
### Sleep system
|
|
278
299
|
|
|
279
|
-
When a body's velocity is too low for too long, the body will enter sleep state, which means its position will not be affected by the physics engine until a force is applied
|
|
300
|
+
When a body's velocity is too low for too long, the body will enter sleep state, which means its position will not be affected by the physics engine until a force is applied, a collision happens, or the velocity is above threshold again, this is to prevent jittering and optimize performance.
|
|
280
301
|
|
|
281
302
|
You can configure it inside `RigidBody`:
|
|
282
303
|
```js
|
package/dist/game.js
CHANGED
|
@@ -43,8 +43,6 @@ export class Game {
|
|
|
43
43
|
const dt = (timestamp - this.lastTime) / 1000;
|
|
44
44
|
this.lastTime = timestamp;
|
|
45
45
|
if (this.scene) {
|
|
46
|
-
// Update input info
|
|
47
|
-
this.input.update();
|
|
48
46
|
// Update game logic
|
|
49
47
|
this.scene.update(dt);
|
|
50
48
|
// Update physics info
|
|
@@ -52,6 +50,8 @@ export class Game {
|
|
|
52
50
|
// Render
|
|
53
51
|
this.ctx.clearRect(0, 0, this.ctx.canvas.width, this.ctx.canvas.height);
|
|
54
52
|
this.scene.render();
|
|
53
|
+
// Update input info
|
|
54
|
+
this.input.update();
|
|
55
55
|
}
|
|
56
56
|
else {
|
|
57
57
|
throw new Error("Can not run game loop without a scene");
|
package/dist/physics.d.ts
CHANGED
|
@@ -44,7 +44,24 @@ export declare class CircleCollider {
|
|
|
44
44
|
mask: number;
|
|
45
45
|
constructor(options: CircleColliderOptions);
|
|
46
46
|
}
|
|
47
|
-
export
|
|
47
|
+
export interface BoxColliderOptions {
|
|
48
|
+
width: number;
|
|
49
|
+
height: number;
|
|
50
|
+
offset?: Vector2;
|
|
51
|
+
isTrigger?: boolean;
|
|
52
|
+
layer?: number;
|
|
53
|
+
mask?: number;
|
|
54
|
+
}
|
|
55
|
+
export declare class BoxCollider {
|
|
56
|
+
width: number;
|
|
57
|
+
height: number;
|
|
58
|
+
offset: Vector2;
|
|
59
|
+
isTrigger: boolean;
|
|
60
|
+
layer: number;
|
|
61
|
+
mask: number;
|
|
62
|
+
constructor(options: BoxColliderOptions);
|
|
63
|
+
}
|
|
64
|
+
export type Collider = CircleCollider | BoxCollider;
|
|
48
65
|
export interface SpatialGridOptions {
|
|
49
66
|
cellSize?: number;
|
|
50
67
|
grid?: Map<string, Set<Entity>>;
|
package/dist/physics.js
CHANGED
|
@@ -43,6 +43,22 @@ export class CircleCollider {
|
|
|
43
43
|
this.mask = options.mask ?? 0xFFFFFFFF;
|
|
44
44
|
}
|
|
45
45
|
}
|
|
46
|
+
export class BoxCollider {
|
|
47
|
+
width;
|
|
48
|
+
height;
|
|
49
|
+
offset;
|
|
50
|
+
isTrigger;
|
|
51
|
+
layer;
|
|
52
|
+
mask;
|
|
53
|
+
constructor(options) {
|
|
54
|
+
this.width = options.width;
|
|
55
|
+
this.height = options.height;
|
|
56
|
+
this.offset = options.offset ?? new Vector2(0, 0);
|
|
57
|
+
this.isTrigger = options.isTrigger ?? false;
|
|
58
|
+
this.layer = options.layer ?? (1 << 0);
|
|
59
|
+
this.mask = options.mask ?? 0xFFFFFFFF;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
46
62
|
export class SpatialGrid {
|
|
47
63
|
cellSize;
|
|
48
64
|
grid;
|
|
@@ -123,6 +139,16 @@ export class SpatialGrid {
|
|
|
123
139
|
maxY: entity.position.y + radius
|
|
124
140
|
};
|
|
125
141
|
}
|
|
142
|
+
else if (entity.collider instanceof BoxCollider) {
|
|
143
|
+
const halfWidth = entity.collider.width;
|
|
144
|
+
const halfHeight = entity.collider.height;
|
|
145
|
+
return {
|
|
146
|
+
minX: entity.position.x - halfWidth,
|
|
147
|
+
maxX: entity.position.x + halfWidth,
|
|
148
|
+
minY: entity.position.y - halfHeight,
|
|
149
|
+
maxY: entity.position.y + halfHeight
|
|
150
|
+
};
|
|
151
|
+
}
|
|
126
152
|
else {
|
|
127
153
|
throw new Error("Collider type not supported");
|
|
128
154
|
}
|
|
@@ -137,8 +163,11 @@ export class Physics {
|
|
|
137
163
|
// Update velocity/apply force
|
|
138
164
|
for (const entity of entities) {
|
|
139
165
|
if (entity.body instanceof RigidBody) {
|
|
140
|
-
// Wake if force applied
|
|
141
|
-
if (entity.body.force.magnitudeSquared() > 0 ||
|
|
166
|
+
// Wake if force applied or velocity above threshold
|
|
167
|
+
if (entity.body.force.magnitudeSquared() > 0 ||
|
|
168
|
+
entity.body.torque !== 0 ||
|
|
169
|
+
entity.body.velocity.magnitudeSquared() > entity.body.sleepThreshold ** 2 ||
|
|
170
|
+
Math.abs(entity.body.rotationVelocity) > entity.body.sleepThreshold) {
|
|
142
171
|
entity.body.wake();
|
|
143
172
|
}
|
|
144
173
|
// Skip sleeping bodies with no forces
|
|
@@ -308,7 +337,7 @@ export class Physics {
|
|
|
308
337
|
// Check collision
|
|
309
338
|
const posA = entityA.position.add(entityA.collider.offset);
|
|
310
339
|
const posB = entityB.position.add(entityB.collider.offset);
|
|
311
|
-
// Check different types of colliders
|
|
340
|
+
// Check different types of colliders
|
|
312
341
|
if (entityA.collider instanceof CircleCollider && entityB.collider instanceof CircleCollider) {
|
|
313
342
|
const distance = posA.distance(posB);
|
|
314
343
|
const radiusSum = entityA.collider.radius + entityB.collider.radius;
|
|
@@ -337,6 +366,110 @@ export class Physics {
|
|
|
337
366
|
}
|
|
338
367
|
};
|
|
339
368
|
}
|
|
369
|
+
else if (entityA.collider instanceof BoxCollider && entityB.collider instanceof BoxCollider) {
|
|
370
|
+
const halfAx = entityA.collider.width / 2;
|
|
371
|
+
const halfAy = entityA.collider.height / 2;
|
|
372
|
+
const halfBx = entityB.collider.width / 2;
|
|
373
|
+
const halfBy = entityB.collider.height / 2;
|
|
374
|
+
const delta = posB.sub(posA);
|
|
375
|
+
const overlapX = halfAx + halfBx - Math.abs(delta.x);
|
|
376
|
+
const overlapY = halfAy + halfBy - Math.abs(delta.y);
|
|
377
|
+
if (overlapX <= 0 || overlapY <= 0) {
|
|
378
|
+
return { isTrigger: false };
|
|
379
|
+
}
|
|
380
|
+
let normal;
|
|
381
|
+
let penetration;
|
|
382
|
+
if (overlapX < overlapY) {
|
|
383
|
+
penetration = overlapX;
|
|
384
|
+
normal = new Vector2(delta.x < 0 ? -1 : 1, 0);
|
|
385
|
+
}
|
|
386
|
+
else {
|
|
387
|
+
penetration = overlapY;
|
|
388
|
+
normal = new Vector2(0, delta.y < 0 ? -1 : 1);
|
|
389
|
+
}
|
|
390
|
+
// Contact point: center of overlapping region
|
|
391
|
+
const contactX = posA.x + Math.sign(delta.x) * (halfAx - overlapX / 2);
|
|
392
|
+
const contactY = posA.y + Math.sign(delta.y) * (halfAy - overlapY / 2);
|
|
393
|
+
let accumulatedImpulse = 0;
|
|
394
|
+
const lastContactInfo = this.collisionPairs.get(entityA)?.get(entityB) ||
|
|
395
|
+
this.collisionPairs.get(entityB)?.get(entityA);
|
|
396
|
+
if (lastContactInfo) {
|
|
397
|
+
accumulatedImpulse = lastContactInfo.accumulatedNormalImpulse;
|
|
398
|
+
}
|
|
399
|
+
return {
|
|
400
|
+
isTrigger,
|
|
401
|
+
info: {
|
|
402
|
+
normal,
|
|
403
|
+
penetration,
|
|
404
|
+
contact: new Vector2(contactX, contactY),
|
|
405
|
+
accumulatedNormalImpulse: accumulatedImpulse
|
|
406
|
+
}
|
|
407
|
+
};
|
|
408
|
+
}
|
|
409
|
+
else if ((entityA.collider instanceof CircleCollider && entityB.collider instanceof BoxCollider) ||
|
|
410
|
+
(entityA.collider instanceof BoxCollider && entityB.collider instanceof CircleCollider)) {
|
|
411
|
+
const isCircleA = entityA.collider instanceof CircleCollider;
|
|
412
|
+
const circlePos = isCircleA ? posA : posB;
|
|
413
|
+
const boxPos = isCircleA ? posB : posA;
|
|
414
|
+
const circle = (isCircleA ? entityA : entityB).collider;
|
|
415
|
+
const box = (isCircleA ? entityB : entityA).collider;
|
|
416
|
+
const halfW = box.width / 2;
|
|
417
|
+
const halfH = box.height / 2;
|
|
418
|
+
// Box center -> circle center
|
|
419
|
+
const delta = circlePos.sub(boxPos);
|
|
420
|
+
const inside = Math.abs(delta.x) < halfW && Math.abs(delta.y) < halfH;
|
|
421
|
+
let normal;
|
|
422
|
+
let penetration;
|
|
423
|
+
let contact;
|
|
424
|
+
if (inside) {
|
|
425
|
+
const overlapX = halfW - Math.abs(delta.x);
|
|
426
|
+
const overlapY = halfH - Math.abs(delta.y);
|
|
427
|
+
if (overlapX < overlapY) {
|
|
428
|
+
const sign = delta.x < 0 ? -1 : 1;
|
|
429
|
+
const boxToCircle = new Vector2(sign, 0);
|
|
430
|
+
normal = isCircleA ? boxToCircle.neg() : boxToCircle; // A→B
|
|
431
|
+
penetration = circle.radius + overlapX;
|
|
432
|
+
contact = new Vector2(boxPos.x + sign * halfW, circlePos.y);
|
|
433
|
+
}
|
|
434
|
+
else {
|
|
435
|
+
const sign = delta.y < 0 ? -1 : 1;
|
|
436
|
+
const boxToCircle = new Vector2(0, sign);
|
|
437
|
+
normal = isCircleA ? boxToCircle.neg() : boxToCircle;
|
|
438
|
+
penetration = circle.radius + overlapY;
|
|
439
|
+
contact = new Vector2(circlePos.x, boxPos.y + sign * halfH);
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
else {
|
|
443
|
+
const clampedX = Math.max(-halfW, Math.min(halfW, delta.x));
|
|
444
|
+
const clampedY = Math.max(-halfH, Math.min(halfH, delta.y));
|
|
445
|
+
const closest = boxPos.add(new Vector2(clampedX, clampedY));
|
|
446
|
+
const diff = circlePos.sub(closest);
|
|
447
|
+
const distSq = diff.magnitudeSquared();
|
|
448
|
+
if (distSq >= circle.radius * circle.radius) {
|
|
449
|
+
return { isTrigger: false };
|
|
450
|
+
}
|
|
451
|
+
const dist = Math.sqrt(distSq);
|
|
452
|
+
const boxToCircle = dist > 0 ? diff.scale(1 / dist) : new Vector2(1, 0);
|
|
453
|
+
normal = isCircleA ? boxToCircle.neg() : boxToCircle;
|
|
454
|
+
penetration = circle.radius - dist;
|
|
455
|
+
contact = closest;
|
|
456
|
+
}
|
|
457
|
+
let accumulatedImpulse = 0;
|
|
458
|
+
const lastContactInfo = this.collisionPairs.get(entityA)?.get(entityB) ||
|
|
459
|
+
this.collisionPairs.get(entityB)?.get(entityA);
|
|
460
|
+
if (lastContactInfo) {
|
|
461
|
+
accumulatedImpulse = lastContactInfo.accumulatedNormalImpulse;
|
|
462
|
+
}
|
|
463
|
+
return {
|
|
464
|
+
isTrigger,
|
|
465
|
+
info: {
|
|
466
|
+
normal,
|
|
467
|
+
penetration,
|
|
468
|
+
contact,
|
|
469
|
+
accumulatedNormalImpulse: accumulatedImpulse
|
|
470
|
+
}
|
|
471
|
+
};
|
|
472
|
+
}
|
|
340
473
|
return {
|
|
341
474
|
isTrigger: false
|
|
342
475
|
};
|
|
@@ -387,11 +520,14 @@ export class Physics {
|
|
|
387
520
|
bodyA.velocity = bodyA.velocity.sub(info.normal.scale(actualImpulse * invMassA));
|
|
388
521
|
bodyB.velocity = bodyB.velocity.add(info.normal.scale(actualImpulse * invMassB));
|
|
389
522
|
// Apply angular impulse (torque from contact point)
|
|
390
|
-
|
|
523
|
+
// Disabled for now because colliders must rotate too and there isn't polygon collider solver for now
|
|
524
|
+
/*const rA = info.contact.sub(entityA.position);
|
|
391
525
|
const rB = info.contact.sub(entityB.position);
|
|
526
|
+
|
|
392
527
|
const angularImpulseA = rA.cross(info.normal.scale(-actualImpulse));
|
|
393
528
|
const angularImpulseB = rB.cross(info.normal.scale(actualImpulse));
|
|
529
|
+
|
|
394
530
|
bodyA.rotationVelocity += angularImpulseA * (isFinite(bodyA.inertia) ? 1 / bodyA.inertia : 0);
|
|
395
|
-
bodyB.rotationVelocity += angularImpulseB * (isFinite(bodyB.inertia) ? 1 / bodyB.inertia : 0)
|
|
531
|
+
bodyB.rotationVelocity += angularImpulseB * (isFinite(bodyB.inertia) ? 1 / bodyB.inertia : 0);*/
|
|
396
532
|
}
|
|
397
533
|
}
|