jsgoller1 / sprawl

2D Game Engine
GNU General Public License v3.0
0 stars 0 forks source link

Trajectory-based collision tests #34

Open jsgoller1 opened 1 year ago

jsgoller1 commented 1 year ago

If we do a "check final position and cancel movement if it results in a collision" with a large movement and a thin surface, we can wind up overshooting it and penetrating by accident. To solve this, we can test the entire movement trajectory against the surface as follows. Consider our initial position in the trajectory and our final position; if we draw straight lines from each corner of our bounding box in the starting position to each respect corner of the bounding box in the final position (top left to top left, bottom left to bottom left, etc), it forms a 3D figure. If targetComponent's bounding box is equal size or larger than us, we can test intersection by doing a line-box test against each corner line of the 3D solid with the target's bounding box. If the target box is smaller than ours, an edge case can happen where the 3D solid completely envelopes the target box. To prevent this, we can reinvision our motion along the trajectory as the target box's movement along the inverse trajectory (i.e. they're coming to us instead of us going to them)