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.

Ok, so I'm having a problem with setting up AABB Collision detection. I understand how to implement this and everything, and I know this method works, because, with few (functional) changes, I'm pulling it from some old Java code I wrote for a game. Here's what I'm trying to do:

  1. Take two GameObjects (which have their own BoundingBox objects)
    • working
  2. Get a rectangle that encompasses the current and final (unaltered) bounding boxes of the active GameObject.
    • works as well
  3. Create a BoundingBox object that is exactly the same as the active GameObject's, and a moveStep Vector2D that is the normalized value of the active GameObject's moveVector.
    • I think this is working. At least, the functions I'm using have worked in other situations
  4. Move the new BoundingBox along the moveStep vector. If the new BoundingBox intersects with the second GameObjects BoundingBox, edit the active GameObjects moveVector and return.

I know that the wide pass step is working, and I can guess that the creation of testBound and moveStep are working because I use those functions elsewhere in the game and they are fine.

So here's the code:

-(void)handleCollisionWith:(GameObject *)other{
BoundingBox* widePass = [BoundingBox BoxEnclosing:bounds B:[[bounds copy] move:moveVector]];
if ([widePass boundingBoxIntersects:other.bounds]) {
    BoundingBox* testBound = [self.bounds copy];
    Vector2D* moveStep = [Vector2D normalize:moveVector];
    float numSteps = moveVector.x/moveStep.x;

    for (int i=0; i<=numSteps; i++) {
        if (i > numSteps) {
            i = numSteps;
            moveStep = [Vector2D sub:testBound.loc to:[Vector2D add:bounds.loc to:moveVector]];
        }
        [testBound move:moveStep];

        if ([testBound boundingBoxIntersects:other.bounds]) {
            [moveVector mult:numSteps];
            return;
        }
    }
}

}

And just to make sure I've covered everything:

  • Vector2D is just a class with a float x and y. BoundingBox is a class with two Vector2D objects called loc and size.

  • [BoundingBox* boundingBoxIntersects:BoundingBox*] takes the two BoundingBoxes, creates two CGRects out of them, and returns CGRectIntersectsRect();

I just spent a 3 hour car ride looking at this, and I cannot for the life of me figure out why it isn't working. Any help is greatly appreciated,

Thanks, Peter

share|improve this question
1  
(i > numSteps) <-- how could this statement ever be true? –  Ian Markowitz Jun 10 '13 at 6:16
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.