I'm trying to implement collisions between two solid objects. I want the objects to stop moving in their current direction when they hit another solid object (hitting a wall). So far, they always get stuck, even though I'm sure that the math works such that they shouldn't, I've actually taken the code from another question on this site.
Here is the code:
public override void collide(GameObject otherObject)
{
Vector2 newPos = this.position;
if (otherObject.objId == ObjectId.PLAYER)
{
if (this.direction.X == 1) // moving right
{
newPos.X = otherObject.getBounds.Left - boundingBox.Width;
}
else if (this.direction.X == -1) // moving left
{
newPos.X = (int)otherObject.getBounds.Right;
}
}
this.position = newPos;
} // --------------------------------------------------------------- //