Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have been havaing a problem doing 3-d collision in a game i have been trying to make for several weeks now. What i am trying to do is prevent a PlayerModel object from colliding with Obstacle(cube) objects. The code i have right now works perfectly fine but it only works correctly with the first Obstacle in the list of Obstacle objects i have. I have tried using a foreach and for statement to try and go through each object in the list but neither method worked, also putting breaks and continue statements did not work either. The first code block that i want to post is the code in the player model object that checks the collision.

I decided to leave in the comments i had from when i was debugging the code. Also this code block is all happening in the PlayerModel class.

//update future bounding sphere based on input
        this.futureBoundingSphere = new BoundingSphere(this.futurePosition.Translation, 10.5f);

        //collision checking last
        //What i think is going on - At first only one cube was being checked, so i just break out of the statement if we hit soemthing.,
        //i think that was the only way i could get the collision to get checked on multiple objects.
        //Also, i must take note of the bounding spehers, It is likely that the spacemans collision sphere is being drawn waaaay to large, suggest scaling spacemodel to test
        foreach (BasicModel obstacleModel in game.obstacles.models)
        {

            if (this.wholeCollidesWith(futureBoundingSphere, obstacleModel, obstacleModel.GetWorld()))
            {
                //note - First object in the list does second statement, last object does first statement
                game.aColor = Color.AliceBlue;
                futurePosition.Translation = this.world.Translation;
                //break; ////- adding in break will cause all but the first object to do the first statement
            }
            else
            {
                game.aColor = Color.Orange;
                this.world.Translation = futurePosition.Translation;
            }
        }

       //update actual bounding sphere last
        this.myBoundingSphere = new BoundingSphere(this.GetWorld().Translation, 10f);

The second code block i have is the wholeCollidesWith method that i use to check collision between single objects. Please keep in mind that i am not currently using the otherWorld parameter, i had used it once before but stopped using it because i am updating each objects bouding sphere whenever it's update method is called.

//Whole model collision, not checking individual meshes
    public bool wholeCollidesWith(BoundingSphere futureBoundingSphere, BasicModel otherModel, Matrix otherWorld)
    {
        //Check if one object collides with another
        if(futureBoundingSphere.Intersects(
            otherModel.myBoundingSphere))
        {
            return true;
        }
        return false;
    }

I am not sure what is going on but i am hoping that it is a simple syntax error that i am overlooking with my foreach loop. In any case i hope there will be somone who might be able to find the error that i could not find, please request for any more code/information if needed. Thanks in advance.

share|improve this question

closed as too localized by Byte56, Sean Middleditch, bummzack, Laurent Couvidou, Trevor Powell Feb 28 '13 at 21:41

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, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

1  
I think debugging your code is too localized a question for the site. Check out the FAQ to learn what types of questions to ask here, and find some sites that are more open to discussion based questions like this one. Thanks. –  Byte56 Feb 27 '13 at 19:52
    
Sorry about that, I guess I assumed that it would be ok, I won't post any specific questions again. –  Habeeb Adisa Feb 28 '13 at 13:07

1 Answer 1

up vote 0 down vote accepted

You are modifying the world.Translation before you have a chance to check for collisions with the rest of the objects.

In the case where the first object is not colliding you set the world.Translation to be the futurePosition.Translation, then no matter what happens after that you are stuck with the futurePosition.Translation because you are essentially saying futurePosition.Translation = futurePosition.Translation (this.world.Translation) in the first part of the if statement.

Just set a flag _collisionHappened to false before entering the loop, then set it to true if you hit something. At the end of the loop check the flag and only update the translate if there was no collision.

share|improve this answer
    
Thanks for the edits. I didn't realize you could hightlight code elements inline like that. Definitely makes it more readable. –  RobCurr Feb 27 '13 at 20:11
    
Thank you so much, it worked finally. I did not think that updating the world translation inside of the loop would affect the rest of the collision checking for the rest of the objects. Thanks again. –  Habeeb Adisa Feb 28 '13 at 13:26

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