Skip to content

Commit

Permalink
fix: [#2343] Only adjust velocities that are opposite the collision n…
Browse files Browse the repository at this point in the history
…ormal
  • Loading branch information
eonarheim committed Jun 7, 2022
1 parent 384fef6 commit b356a83
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/engine/Collision/Solver/ArcadeSolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ export class ArcadeSolver implements CollisionSolver {
}

public solvePosition(contact: CollisionContact) {

// if bounds no longer intersect skip to the next
// this removes jitter from overlapping/stacked solid tiles or a wall of solid tiles
if (!contact.colliderA.bounds.overlaps(contact.colliderB.bounds)) {
Expand Down Expand Up @@ -165,15 +164,23 @@ export class ArcadeSolver implements CollisionSolver {
const normal = contact.normal;
const opposite = normal.negate();

// Cancel out velocity opposite direction of collision normal
if (bodyA.collisionType === CollisionType.Active) {
const velAdj = normal.scale(normal.dot(bodyA.vel.negate()));
bodyA.vel = bodyA.vel.add(velAdj);
// only adjust velocity if the contact normal is opposite to the current velocity
// this avoids catching edges on a platform when sliding off
if (bodyA.vel.normalize().dot(opposite) < 0) {
// Cancel out velocity opposite direction of collision normal
const velAdj = normal.scale(normal.dot(bodyA.vel.negate()));
bodyA.vel = bodyA.vel.add(velAdj);
}
}

if (bodyB.collisionType === CollisionType.Active) {
const velAdj = opposite.scale(opposite.dot(bodyB.vel.negate()));
bodyB.vel = bodyB.vel.add(velAdj);
// only adjust velocity if the contact normal is opposite to the current velocity
// this avoids catching edges on a platform
if (bodyB.vel.normalize().dot(normal) < 0) {
const velAdj = opposite.scale(opposite.dot(bodyB.vel.negate()));
bodyB.vel = bodyB.vel.add(velAdj);
}
}
}
}
Expand Down

0 comments on commit b356a83

Please sign in to comment.