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

Commit

Permalink
Merge branch bugfix/4-side-collision-response into main (#105)
Browse files Browse the repository at this point in the history
Fix collision response

Fix collision response letting Moving Entities get stuck when colliding on all 4 sides.
  • Loading branch information
Kitt3120 authored Jun 19, 2023
1 parent ccca5f8 commit 63932b2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 21 deletions.
33 changes: 20 additions & 13 deletions src/ts/engine/entitiy/entitymanager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,15 +237,16 @@ class EntityManager {
continue;
}

let bothEntitiesAreMovingEntities =
entityIsMovingEntity && otherEntityIsMovingEntity;

let movingEntities: MovingEntity[] = [];
if (entityIsMovingEntity) {
movingEntities.push(entity as MovingEntity);
}
if (otherEntityIsMovingEntity) {
movingEntities.push(otherEntity as MovingEntity);
}
let bothEntitiesAreMovingEntities =
entityIsMovingEntity && otherEntityIsMovingEntity;

let intersection = entity.boundingBox.intersect(otherEntity.boundingBox);
if (intersection.points.length === 0) {
Expand Down Expand Up @@ -287,18 +288,24 @@ class EntityManager {
.subtract(intersection.center)
.normalize();

if (
movingEntityCollisions.get(Direction.Top) ||
movingEntityCollisions.get(Direction.Bottom)
) {
pushDirection = Matrix2D.ignoreXMatrix.multiplyVector(pushDirection);
}
if (movingEntityCollisions.size === 4) {
pushDirection = pushDirection.multiplyScalar(0.25);
} else {
if (
movingEntityCollisions.get(Direction.Top) ||
movingEntityCollisions.get(Direction.Bottom)
) {
pushDirection =
Matrix2D.ignoreXMatrix.multiplyVector(pushDirection);
}

if (
movingEntityCollisions.get(Direction.Left) ||
movingEntityCollisions.get(Direction.Right)
) {
pushDirection = Matrix2D.ignoreYMatrix.multiplyVector(pushDirection);
if (
movingEntityCollisions.get(Direction.Left) ||
movingEntityCollisions.get(Direction.Right)
) {
pushDirection =
Matrix2D.ignoreYMatrix.multiplyVector(pushDirection);
}
}

pushDirection = pushDirection.multiplyScalar(
Expand Down
36 changes: 28 additions & 8 deletions src/ts/engine/entitiy/movingentity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,39 @@ class MovingEntity extends Entity {
public update(tickDelta: number): void {
super.update(tickDelta);

if (this._collisions[Direction.Top] && this._velocity.y < 0) {
if (
this._collisions[Direction.Top] &&
!this.collisions[Direction.Bottom] &&
this._velocity.y < 0
) {
this._velocity = Matrix2D.ignoreYMatrix.multiplyVector(this._velocity);
}
if (this._collisions[Direction.Right] && this._velocity.x > 0) {

if (
this._collisions[Direction.Right] &&
!this.collisions[Direction.Left] &&
this._velocity.x > 0
) {
this._velocity = Matrix2D.ignoreXMatrix.multiplyVector(this._velocity);
}

if (
this._collisions[Direction.Bottom] &&
!this.collisions[Direction.Top] &&
this._velocity.y > 0
) {
this._velocity = Matrix2D.ignoreYMatrix.multiplyVector(this._velocity);
}

if (
this._collisions[Direction.Left] &&
!this.collisions[Direction.Right] &&
this._velocity.x < 0
) {
this._velocity = Matrix2D.ignoreXMatrix.multiplyVector(this._velocity);
}

if (this._collisions[Direction.Bottom]) {
if (this._velocity.y > 0) {
this._velocity = Matrix2D.ignoreYMatrix.multiplyVector(this._velocity);
}
this._velocity = new Matrix2D(0.95, 0, 0, 1).multiplyVector(
this._velocity
);
Expand All @@ -85,9 +108,6 @@ class MovingEntity extends Entity {
this._velocity
);
}
if (this._collisions[Direction.Left] && this._velocity.x < 0) {
this._velocity = Matrix2D.ignoreXMatrix.multiplyVector(this._velocity);
}

let newLocation = this.location.add(
this._velocity.multiplyScalar(tickDelta / 100)
Expand Down

0 comments on commit 63932b2

Please sign in to comment.