Prozi / detect-collisions

Detect collisions between different shapes such as Points, Lines, Boxes, Polygons (including concave), Ellipses, and Circles. Features include RayCasting and support for offsets, rotation, scaling, bounding box padding, with options for static and trigger bodies (non-colliding).
https://prozi.github.io/detect-collisions/
MIT License
202 stars 21 forks source link

Should resolve multiple simultaneous collision between static and dynamic bodies #81

Closed sbuys closed 1 month ago

sbuys commented 1 month ago

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?

Prozi commented 1 month ago

will look into it thanks @sbuys

Prozi commented 1 month ago

will look into it thanks @sbuys

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

Prozi commented 1 month ago

@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
sbuys commented 1 month ago

@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

Prozi commented 1 month ago

@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

Prozi commented 1 month ago

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

Prozi commented 1 month ago

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

sbuys commented 1 month ago

@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.

Prozi commented 1 month ago

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