Skip to content
This repository was archived by the owner on Nov 13, 2023. It is now read-only.

Commit 63932b2

Browse files
authored
Merge branch bugfix/4-side-collision-response into main (#105)
Fix collision response Fix collision response letting Moving Entities get stuck when colliding on all 4 sides.
1 parent ccca5f8 commit 63932b2

File tree

2 files changed

+48
-21
lines changed

2 files changed

+48
-21
lines changed

src/ts/engine/entitiy/entitymanager.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -237,15 +237,16 @@ class EntityManager {
237237
continue;
238238
}
239239

240+
let bothEntitiesAreMovingEntities =
241+
entityIsMovingEntity && otherEntityIsMovingEntity;
242+
240243
let movingEntities: MovingEntity[] = [];
241244
if (entityIsMovingEntity) {
242245
movingEntities.push(entity as MovingEntity);
243246
}
244247
if (otherEntityIsMovingEntity) {
245248
movingEntities.push(otherEntity as MovingEntity);
246249
}
247-
let bothEntitiesAreMovingEntities =
248-
entityIsMovingEntity && otherEntityIsMovingEntity;
249250

250251
let intersection = entity.boundingBox.intersect(otherEntity.boundingBox);
251252
if (intersection.points.length === 0) {
@@ -287,18 +288,24 @@ class EntityManager {
287288
.subtract(intersection.center)
288289
.normalize();
289290

290-
if (
291-
movingEntityCollisions.get(Direction.Top) ||
292-
movingEntityCollisions.get(Direction.Bottom)
293-
) {
294-
pushDirection = Matrix2D.ignoreXMatrix.multiplyVector(pushDirection);
295-
}
291+
if (movingEntityCollisions.size === 4) {
292+
pushDirection = pushDirection.multiplyScalar(0.25);
293+
} else {
294+
if (
295+
movingEntityCollisions.get(Direction.Top) ||
296+
movingEntityCollisions.get(Direction.Bottom)
297+
) {
298+
pushDirection =
299+
Matrix2D.ignoreXMatrix.multiplyVector(pushDirection);
300+
}
296301

297-
if (
298-
movingEntityCollisions.get(Direction.Left) ||
299-
movingEntityCollisions.get(Direction.Right)
300-
) {
301-
pushDirection = Matrix2D.ignoreYMatrix.multiplyVector(pushDirection);
302+
if (
303+
movingEntityCollisions.get(Direction.Left) ||
304+
movingEntityCollisions.get(Direction.Right)
305+
) {
306+
pushDirection =
307+
Matrix2D.ignoreYMatrix.multiplyVector(pushDirection);
308+
}
302309
}
303310

304311
pushDirection = pushDirection.multiplyScalar(

src/ts/engine/entitiy/movingentity.ts

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,39 @@ class MovingEntity extends Entity {
6666
public update(tickDelta: number): void {
6767
super.update(tickDelta);
6868

69-
if (this._collisions[Direction.Top] && this._velocity.y < 0) {
69+
if (
70+
this._collisions[Direction.Top] &&
71+
!this.collisions[Direction.Bottom] &&
72+
this._velocity.y < 0
73+
) {
7074
this._velocity = Matrix2D.ignoreYMatrix.multiplyVector(this._velocity);
7175
}
72-
if (this._collisions[Direction.Right] && this._velocity.x > 0) {
76+
77+
if (
78+
this._collisions[Direction.Right] &&
79+
!this.collisions[Direction.Left] &&
80+
this._velocity.x > 0
81+
) {
82+
this._velocity = Matrix2D.ignoreXMatrix.multiplyVector(this._velocity);
83+
}
84+
85+
if (
86+
this._collisions[Direction.Bottom] &&
87+
!this.collisions[Direction.Top] &&
88+
this._velocity.y > 0
89+
) {
90+
this._velocity = Matrix2D.ignoreYMatrix.multiplyVector(this._velocity);
91+
}
92+
93+
if (
94+
this._collisions[Direction.Left] &&
95+
!this.collisions[Direction.Right] &&
96+
this._velocity.x < 0
97+
) {
7398
this._velocity = Matrix2D.ignoreXMatrix.multiplyVector(this._velocity);
7499
}
100+
75101
if (this._collisions[Direction.Bottom]) {
76-
if (this._velocity.y > 0) {
77-
this._velocity = Matrix2D.ignoreYMatrix.multiplyVector(this._velocity);
78-
}
79102
this._velocity = new Matrix2D(0.95, 0, 0, 1).multiplyVector(
80103
this._velocity
81104
);
@@ -85,9 +108,6 @@ class MovingEntity extends Entity {
85108
this._velocity
86109
);
87110
}
88-
if (this._collisions[Direction.Left] && this._velocity.x < 0) {
89-
this._velocity = Matrix2D.ignoreXMatrix.multiplyVector(this._velocity);
90-
}
91111

92112
let newLocation = this.location.add(
93113
this._velocity.multiplyScalar(tickDelta / 100)

0 commit comments

Comments
 (0)