I'm writing a simple physics system for fun, and I've run into a problem that has me stuck.
The basic algorithm now is:
I have one moving body moving toward two, static, massless, bodies.
The moving object is translated in one step to collide with one of the bodies.
I respond by finding the smallest distance I can move so that they are no longer colliding. In this case, that means moving the dynamic body straight down. However, now it is colliding with another box.
I repeat the same thing with that box, trying to move the dynamic box so that it is no longer colliding, but that pushes it back into the first box. This repeats forever. Is my algorithm fundamentally flawed?
Two forms of collision detection: Continuous: very expensive. Simulate solid objects in real life. Discrete: objects will end up with penetrating each other.
Axis-Aligned Bounding Box One of the simpler forms of collision detection is between two rectangles that are axis aligned — meaning no rotation. The algorithm works by ensuring there is no gap between any of the 4 sides of the rectangles. Any gap means a collision does not exist.
code / collision is a new kind of event for coders. coders go head-to-head in a variety of competitions. All you code is the strategy. join an event or host your own. play virtually or in person.
The most basic way to approach this type of 2d collision detection is to utilize a concept known as Axis-aligned bounding box. Axis-aligned bounding box, or AABB, detects if two non-rotational polygons are overlapping each other.
Instead of moving down once a collision has been detected it would be better to move back in the direction you came from. That way you have a guarantee that eventually you must end up in a state where there are no collisions if we assume that the initial state had no collisions.
We need to find out by how much we need to shrink (scale) v
to fit it into the object intersection. The shrunk v
will have the right magnitude, so that if we move backwards in the direction of -v
by that magnitude, then we will no-longer intersect.
Let's assume an intersection consists of a x_intersection
and a y_intersection
component. To find out by how much we need to move backwards to no longer intersect we need to scale the original v = (v_x, v_y)
vector. If the x_intersection
is the smaller intersection then we scale v
by x_intersection / v_x
and move our object back by -v * x_intersection / v_x
. This means we move back by -(x_intersection, x_intersection * v_y/v_x)
. If the y_intersection
is the smaller intersection then we scale v
by y_intersection / v_y
and move our object backwards by -v * y_intersection / v_y = -(y_intersection * v_x/v_y, y_intersection)
.
So I would say the steps in your algorithm could be:
v
If there was a collision
For all collision objects find the minimum scaling of v
by which we we would need to move back. This scaling can be computed as the minimum of two ratios
given v = (v_x, v_y)
min_i = min(x_intersection / v_x, y_intersection / v_y)
Find the minimum scaling ration across all objects.
min_o = min(min_i) for all i
Move object back in direction of vector obtained by scaling the negative move direction with the minimum ratio. That is v2 = (min_o*-v)
where v2
is the vector we use to move back.
For example: first pick w
:
Then pick u2
:
Done :
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With