Pregunta

I am trying to create a physically plausible 2d physics engine. I have read many documents about detection of collisions, contact resolving, interpenetrations, projection, separating axis theorem (SAT) methods, etc.

Projection via SAT appears to be one physically plausible method for dealing with overlapping ("penetrating") objects. This works fine for objects with no rotation, but I can't figure out how to deal with rotations.

Imagine two polygons in rotation that will collide:

Before collision

I need to understand how to project the point of contact and the time when this happens.

Point of collision

any help will be appreciated!

¿Fue útil?

Solución

The problem is that you are assuming both bodies are moving at the same time, which will be a huge pain to calculate the exact collision point.

You can update the position of each body sequentially, and apply the collisions accordingly:

1

(I know the bodies are not rotated in this example, but I have no means right now to draw rotated forms, but the same idea applies, sorry)

First, the rectangle is moved, and checks for collision. Then, the pentagon moves, and collides with the rectangle, updating its position. And last, the triangle position is also checked for collition and updated.

About how to calculate the collision point, you can apply this:

Position calculateValidPosition(Position start, Position end)
    Position middlePoint = (start + end) /2

    if (middlePoint == start || middlePoint == end)
        return start 

    if( isColliding(middlePont) )
        return calculateValidPosition(start, middlePoint)
    else
        return calculate(middlePoint, end)

Note that

Position middlePoint = (start + end) /2

not only calculates the body's middle position, but also should calculate it's middle rotation: If rotation is X at the begining of the movement, and Y at the end, the middle point rotatiln is just (X+Y)/2

Note this algorithm leaves a lot of room for optimization (like making it non-recursive)

This solution might seem not really accurate, but it will only work incorrectly when the speed of the bodies is way greater than the body's size, which will only happen if a really small body moves really fast. In the rest of scenarios the result is "good enough" for a game.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top