This is how I would handle this:
for(i=0;i<collisionobjectscount;i++)
{
xcollision=false;
ycollision=false;
// I have four sets to check for positive and negative speed values.
//this checks if the object is past something before the speed is added and if it's past something after. If it isn't past it before, but is after the speed is added, there's an X axis collision. For there to be a real collision there needs to be an X and Y axis collision.
if(Xposition>=collisionobject[ i ].X+collisionobject[ i ].spaceoffset
&&
Xposition+Xspeed<=collisionobject[ i ].X-collisionobject[ i ].spaceoffset)
{
xcollision=true;
}
//this checks if the object is past something before the speed is added
and if it's past something after. If it isn't past it before, but is
after the speed is added, there's an X axis collision. For there to be
a real collision there needs to be an X and Y axis collision.
if(Xposition<=collisionobject[ i ].X-collisionobject[ i ].spaceoffset
&&
Xposition+Xspeed>=collisionobject[ i ].X+collisionobject[ i ].spaceoffset)
{
xcollision=true;
}
//this checks if the object is past something before the speed is added
and if it's past something after. If it isn't past it before, but is
after the speed is added, there's a Y axis collision. For there to be
a real collision there needs to be an X and Y axis collision.
if(Yposition>=collisionobject[ i ].Y+collisionobject[ i ].spaceoffset
&& Yposition+Yspeed<=collisionobject[ i ].Y-collisionobject[ i ].spaceoffset)
{
ycollision=true;
}
//this checks if the object is past something before the speed is added
and if it's past something after. If it isn't past it before, but is
after the speed is added, there's a Y axis collision. For there to be
a real collision there needs to be an X and Y axis collision.
if(Yposition<=collisionobject[ i ].Y-collisionobject[ i ].spaceoffset
&& Yposition+Yspeed>=collisionobject[ i ].Y+collisionobject[ i ].spaceoffset)
{
ycollision=true;
}
if(ycollision==true && xcollision==true)
{
//these adjust the speed to hit the object rather than to go through it.
if(xspeed>0)
{
Xspeed=Xspeed-((Xposition+Xspeed)-(collisionobject[ i ].X-collisionobject[ i ].spaceoffset));
}
if(xspeed<0)
{
Xspeed=Xspeed+((collisionobject[ i ].X+collisionobject[ i ].spaceoffset)-(Xposition-Xspeed));
}
if(Yspeed>0)
{
Yspeed=Yspeed-((Yposition+Yspeed)-(collisionobject[ i ].Y-collisionobject[ i ].spaceoffset));
}
if(Yspeed<0)
{
Xspeed=Xspeed+((collisionobject[ i ].Y+collisionobject[ i ].spaceoffset)-(Yposition-Yspeed));
}
}
}
Xposition+=Xspeed;
Yposition+=Yspeed;
This is a simple brute force collision detection algorithm. It will check if there's a collision with every object and lower the X and Y speeds so that it will stop at the object rather than go through the object. If it detects a collision with one object, it will adjust the speed for that item, and then with the new speed adjustments it will continue until it finds another collision with the updated speeds and it will do it again. Finally it will move your object after it's checked every item that can have a collision.
This algorithm also assumes your objects are squares, but you can change that by giving every object its own X and Y spaceoffset variables for the algorithm to consider.