2

I'm learning how to do some very basic physics stuff for my own amusement, but I'm running into an odd issue.

I'm using a time-corrected verlet integration method as described on http://www.lonesock.net/article/verlet.html , and so far, it's worked pretty well. The movements and stuff all look pretty good.

My problem occurs with regards to collision resolution. I can resolve (it seems, fairly well) against terrain, since the terrain doesn't move. I just set the current position of the player to a safe spot, and things seem to be good. However, when I attempt to resolve against another sprite (physical actor, etc), things seem to shoot all over the place.

My resolution code looks as follows:

    void ResolveCollision(Sprite* entity1, Sprite* entity2)
    {
        Vec2D depth = GetCollisionDepth(entity1, entity2);
        if (GetSpritePhysical(entity1) && GetSpritePhysical(entity2))
        {
            /* Two physical objects. Move them apart both by half the collision depth. */
            SetPosition(entity1, PointFromVector(AddVectors(
                VectorFromPoint(GetPosition(entity1)), ScalarMultiply(0.5f, depth))));
            SetPosition(entity2, PointFromVector(AddVectors(
                VectorFromPoint(GetPosition(entity2)), ScalarMultiply(-0.5f, depth))));
        }
        else if (GetSpritePhysical(entity1) && !GetSpritePhysical(entity2))
        {
            SetPosition(entity1, PointFromVector(AddVectors(
                VectorFromPoint(GetPosition(entity1)), ScalarMultiply(-1.0f, depth))));
        }
        else if (!GetSpritePhysical(entity1) && GetSpritePhysical(entity2))
        {
            SetPosition(entity2, PointFromVector(AddVectors(
                VectorFromPoint(GetPosition(entity2)), depth)));
        }
        else
        {
            /* Do nothing, why do we have two collidable but nonphysical objects... */
        }
    }

As one can see, there are three cases, determined by whether the collided sprite is physical or not.

It is quite probably the first case that has the issue; and I'm using the same function to get penetration depth here that I am in my (seemingly working) terrain collisions.

Why would the collided sprites be flying off all over the place? I'm quite stumped on this problem. Any help would be great.

c69
  • 19,951
  • 7
  • 52
  • 82
Kozaki
  • 615
  • 2
  • 7
  • 13

1 Answers1

1

Once you separate them, you then resolve their velocities/momentums. How are you doing that?

Most likely, to determine the direction to separate, you use a vector that goes from the center of each. The deeper the penetration, the less accurate this vector (collision normal) may be. That can cause unexpected results.

What works better is not to have to separate them, but rather to calculate where the the collision point will be before moving them, and then only move them that far... then resolve velocities... then move them in the new direction for the rest of the time slice. Granted, it is a little more complex but the results are more accurate.

Steve H
  • 5,479
  • 4
  • 20
  • 26