Tell me more ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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;
    } // --------------------------------------------------------------- //
share|improve this question
You forgot to ask a question. – Laurent Couvidou Jan 22 at 14:58

closed as too localized by Byte56, Laurent Couvidou, Jimmy, bummzack, ClassicThunder Feb 7 at 15:14

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, see the FAQ.

1 Answer

Are you giving this method to the player or to the wall?

Since you call "otherObject.objId == ObjectId.PLAYER" it seems you are giving this to the wall, but you are calling "this.direction.X", "this.position" and "otherObject.getBounds", which look like they should be given to the player object.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.