2
\$\begingroup\$

I implemented SAT alghorithm that is able to detect collisions and return shortest "pull out" vector. On the screenshot below this vector is determined by yellow color (P and Q). If I add Q vector to position of body B, then will be no collision.

However I realized that I can't use this vector to solve collisions, because the body could arrive from completely different direction. I should rather pull out body B by using direction of vector Z.

I already tried to calculate additional overlap by using axis determined by vector Z. However in such case I'm pulling body B too far away from A.

Is there any ellegant solution for that by keep using SAT algorithm?

enter image description here

\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

I think I found an answer for my question by using SAT algorithm only. Of course I'm completely ignoring tunneling effects.

Let's say we have shapes A and B. Shape B penetrated shape A. We know old position of shape B (before the collision). We already did some collision test (SAT) and we know that the collision between shapes A and B exists. Let's draw an axis P determined by old and new position of shape B.

enter image description here

Now project all vertices of shapes A and B on axis P.

enter image description here

Now we can move shape B by using vector Z calculated by formula: Z = UNIT(P) * (Amax - Bmin)

Where UNIT(P) is unit vector of axis P.

enter image description here

The collision has been removed, because now we have at least one separating axis. However, we are not ready yet. Shape B is too far away from A. We need to snap shape B to A on axis P.

Let's repeat SAT test, but this time we are looking for gaps.

enter image description here

We found a gap on axis Q. The length of the gap can be calculated easily. We also know the angle between P and Q and we can calculate snap vector S.

Now we can add vectors Z (initial pull out) and S (snap).

\$\endgroup\$
1
\$\begingroup\$

Likely not as elegant as you're hoping for, unfortunately.

SAT is primarily for collision detection between two convex shapes, not collision response. So you're already down a bit of a rabbit-hole. You could implement a sweep test (rectangle-rectangle-sweeptest in this case) to help you identify the optimal response to the collision. i.e. point, direction, and duration of the collision.

Something like what's shown here - http://supertux.lethargik.org/wiki/Sweep_collision_algorithms

I wish I had a better answer for you, kudos for using SAT though! :)

\$\endgroup\$
1
  • \$\begingroup\$ Thank you for suggestion. I found a solution of using SAT only, however I ignored tunelling effects. I will need to use some kind of sweeptest anyway :) \$\endgroup\$
    – Rakoo
    Feb 22, 2021 at 13:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .