detect-collisions icon indicating copy to clipboard operation
detect-collisions copied to clipboard

Should resolve multiple simultaneous collision between static and dynamic bodies

Open sbuys opened this issue 1 year ago • 4 comments

When multiple collisions are detect simultaneously, resolutions offset each other because they are handled sequentially.

collisions Given the above situation, I would expect system.separate() to move the blue dynamic body out of both static body areas. But the logic for calculating offsets will resolve based on the final item in the calculation.

Further, when calling system.checkAll() or system.checkOne(...) after calling system.separate(), the library does not detect any remaining collisions.

collision-harrisburg

In this example, the blue circles are static bodies, the red dashed boxes are the initial placement of dynamic bodies and the blue solid boxes are the positions resolved by the library. You can see in the magenta box that "Harrisburg" correctly resolves the collisions in its area, moving below the static circle body.

collision-portland

But "Portland" does not resolve correctly. The dynamic rect still intersects with the static circle. The resolution from the "Seattle" collision is overriding the "Portland" resolution.

It seems like system.separate() should account for multiple simultaneous intersections and resolve them in whole. Is there a recommended approach for tackling situations like this?

sbuys avatar Aug 13 '24 21:08 sbuys

will look into it thanks @sbuys

nenjack avatar Aug 19 '24 18:08 nenjack

will look into it thanks @sbuys

don know how to approach this yet but I've thought about it

nenjack avatar Aug 22 '24 20:08 nenjack

@sbuys

can you point me to part of code which does as you wrote

the logic for calculating offsets will resolve based on the final item in the calculation

nenjack avatar Aug 22 '24 20:08 nenjack

@sbuys

can you point me to part of code which does as you wrote

the logic for calculating offsets will resolve based on the final item in the calculation

Sure:

https://github.com/Prozi/detect-collisions/blob/master/src/system.ts#L59

forEach(this.all(), (body: TBody) => {
  this.separateBody(body, callback, response);
});

Each body calculates sequentially as it is called in forEach

sbuys avatar Aug 22 '24 20:08 sbuys

@sbuys can you point me to part of code which does as you wrote

the logic for calculating offsets will resolve based on the final item in the calculation

Sure:

https://github.com/Prozi/detect-collisions/blob/master/src/system.ts#L59

forEach(this.all(), (body: TBody) => {
  this.separateBody(body, callback, response);
});

Each body calculates sequentially as it is called in forEach

I feel what you mean but, this is like the "dynamic body" in your picture

inside separateBody() there is checkOne() call and inside it the offsets are actually added

but in the case where circle is exactly between 2 quads the collisions vector to nullify collision will actually not nullify collision if you feel me

I will try to add at least a test for this in a couple of days

but still no solution on my side, especially since it was even written long time ago in the docs that: https://www.npmjs.com/package/detect-collisions/v/2.1.0#sometimes-bodies-can-squeeze-between-two-other-bodies-whats-going-on

nenjack avatar Aug 26 '24 17:08 nenjack

inside separateBody() there is checkOne() call and inside it the offsets are actually added

https://github.com/Prozi/detect-collisions/blob/master/src/system.ts#L80-L95

nenjack avatar Aug 26 '24 17:08 nenjack

only thing for now I can suggest is to do something like:

class MySystem extends System {
  separate(...) {
    // your implementation that does separate even if offset.x === 0 and offset.y === 0
  }
}

and handle it how you feel you should, sorry

nenjack avatar Aug 26 '24 17:08 nenjack

@Prozi Thanks for looking into it. I understand the complexity. I ended up solving this particular problem with a physics library. I do really like what you've done here and will use for other situations.

sbuys avatar Aug 31 '24 04:08 sbuys

@sbuys thank you I think I will close this for now

nenjack avatar Sep 05 '24 00:09 nenjack