Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/engine/collision/solver/realistic-solver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,8 @@ export class RealisticSolver implements CollisionSolver {
*/
solvePosition(contacts: CollisionContact[]) {
for (let i = 0; i < this.config.positionIterations; i++) {
for (let i = 0; i < contacts.length; i++) {
const contact = contacts[i];
for (let j = 0; j < contacts.length; j++) {
const contact = contacts[j];
const bodyA = contact.bodyA;
const bodyB = contact.bodyB;

Expand Down Expand Up @@ -350,8 +350,8 @@ export class RealisticSolver implements CollisionSolver {
solveVelocity(contacts: CollisionContact[]) {
// velocityIterations:
for (let i = 0; i < this.config.velocityIterations; i++) {
for (let i = 0; i < contacts.length; i++) {
const contact = contacts[i];
for (let j = 0; j < contacts.length; j++) {
const contact = contacts[j];
const bodyA = contact.bodyA;
const bodyB = contact.bodyB;

Expand Down
26 changes: 26 additions & 0 deletions src/spec/vitest/realistic-solver-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,30 @@ describe('A RealisticSolver', () => {
// Considers infinitesimally overlapping to no longer be overlapping and thus cancels the contact
expect(contact.isCanceled()).toBe(true);
});

it('should run the configured number of position and velocity iterations without variable shadowing issues', () => {
const config = { ...getDefaultPhysicsConfig().realistic, positionIterations: 3, velocityIterations: 4 };
const realisticSolver = new ex.RealisticSolver(config);

const actor1 = new ex.Actor({ x: 0, y: 0, width: 40, height: 40, collisionType: ex.CollisionType.Active });
const actor2 = new ex.Actor({ x: 30, y: 0, width: 40, height: 40, collisionType: ex.CollisionType.Active });

const contact = new ex.CollisionContact(
actor1.collider.get(),
actor2.collider.get(),
ex.Vector.Right,
ex.Vector.Right,
ex.Vector.Up,
[ex.vec(20, 0)],
[ex.vec(20, 0)],
null
);
contact.mtv = ex.vec(10, 0);

// Should not throw and should complete successfully with multiple iterations
realisticSolver.preSolve([contact]);
realisticSolver.solvePosition([contact]);
realisticSolver.solveVelocity([contact]);
realisticSolver.postSolve([contact]);
});
});
Loading