i'm having a bit of a problem getting my collision handler to work correctly, my collision detection works fine but handling the movement after the collision is where things have gone a bit buggy. the idea is that if the object is colliding horizontally it will be moved back before dealing with the virtical collision that also happens, when the object has been moved back there is no longer a vertical collision to deal with so it goes ignored, and vice versa for colliding virtically.
do accomplish this i am stepping through the horizontal collision before the virtical collision, first i'm checking of the object is within the same vertical space as the object it is colliding with, if they are in the same virtical space then there is a possible collision to handle, if there is a collision then move the object back based on the overlapping distance. then i check to see if the object is on the same horizontal space as the object it is colliding with, if they are in the same vertical space then there is a collision to handle. if the object is colliding vertically then moving horizontally will be safe so no collision will be detected so the vertical collision will be handled. if the object is colliding horizontally then moving vertically should be safe because after handling the horizontal collision the objects won't be using the same horizontal space so the vertical collision will be ignored.
this is working, sort of, it just seems to be very buggy. if i move horizontally only then the collision is handled normally, while currently colliding if i move vertically while still colliding horizontally then the object just slides up and down the side of the other object. and the opposite is true when first only moving vertically then trying to move horizontally while colliding. the problem comes when the object is colliding while moving both horizontally and vertically at the same time, when the collision is detected it jumps to the corner of the object its colliding with and still remains overlapped. it seems to be a bit tempermental, if the object is moving slow then it works fine, if its moving fast then it goes all screwy, to fast and i can't get the object away from the one it has collided with, instead it will just jump around the corners.
here's the function:
void SpringBack(Movement& Object, Movement& Obstruct)
{
ModAaBb& ObjectMods = Object.GetPolygonMods();
AaBb& ObjectPolygon = Object.GetPolygonReference();
const AaBb& ObstructPolygon = Obstruct.GetPolygonReference();
//step horizontal
//if object and obstruct are in the same vertical space
if(ObjectPolygon.MinY < ObstructPolygon.MaxY && ObjectPolygon.MaxY > ObstructPolygon.MinY){
if(ObjectMods.HorizDir == Movement::LEFT && ObjectPolygon.MinX + ObjectMods.Xvel < ObstructPolygon.MaxX){
ObjectMods.Xvel += ObstructPolygon.MaxX - ObjectPolygon.MinX + 1.0f;
ObjectPolygon.MinX += ObjectMods.Xvel;
ObjectPolygon.MaxX += ObjectMods.Xvel;
ObjectMods.Xvel = 0.0f;
ObjectMods.HorizDir = Movement::NOT_MOVING;
}else if(ObjectMods.HorizDir == Movement::RIGHT && ObjectPolygon.MaxX + ObjectMods.Xvel > ObstructPolygon.MinX){
ObjectMods.Xvel += ObstructPolygon.MinX - ObjectPolygon.MaxX - 1.0f;
ObjectPolygon.MaxX += ObjectMods.Xvel;
ObjectPolygon.MinX += ObjectMods.Xvel;
ObjectMods.Xvel = 0.0f;
ObjectMods.HorizDir = Movement::NOT_MOVING;
}
}
//step vertically
//if object and obstruct are in the same horizontal space
if(ObjectPolygon.MinX < ObstructPolygon.MaxX && ObjectPolygon.MaxX > ObstructPolygon.MinX){
if(ObjectMods.VerticDir == Movement::UP && ObjectPolygon.MinY + ObjectMods.Yvel < ObstructPolygon.MaxY){
ObjectMods.Yvel += ObstructPolygon.MaxY - ObjectPolygon.MinY + 1.0f;
ObjectPolygon.MinY += ObjectMods.Yvel;
ObjectPolygon.MaxY += ObjectMods.Yvel;
ObjectMods.Yvel = 0.0f;
ObjectMods.VerticDir = Movement::NOT_MOVING;
}
if(ObjectMods.VerticDir == Movement::DOWN && ObjectPolygon.MaxY + ObjectMods.Yvel > ObstructPolygon.MinY){
ObjectMods.Yvel += ObstructPolygon.MinY - ObjectPolygon.MaxY - 1.0f;
ObjectPolygon.MaxY += ObjectMods.Yvel;
ObjectPolygon.MinY += ObjectMods.Yvel;
ObjectMods.Yvel = 0.0f;
ObjectMods.VerticDir = Movement::NOT_MOVING;
}
}
}
also, i think i'm getting obssesed on void functions.. they're all i ever seem to make ..