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.