collsion issues with quadtree [on hold]

Posted by QuantumGamer on Game Development See other posts from Game Development or by QuantumGamer
Published on 2014-08-25T14:36:20Z Indexed on 2014/08/25 16:29 UTC
Read the original article Hit count: 1366

Filed under:
|

So i implemented a Quad tree in Java for my 2D game and everything works fine except for when i run my collision detection algorithm, which checks if a object has hit another object and which side it hit.My problem is 80% of the time the collision algorithm works but sometimes the objects just go through each other. Here is my method:

private void checkBulletCollision(ArrayList object) {

    quad.clear(); // quad is the quadtree object
    for(int i=0; i < object.size();i++){
        if(object.get(i).getId() == ObjectId.Bullet) // inserts the object into quadtree
            quad.insert((Bullet)object.get(i));
    }       
    ArrayList<GameObject> returnObjects = new ArrayList<>();  // Uses Quadtree to determine to calculate how many
                                                          // other bullets it can collide with
    for(int i=0; i < object.size(); i++){
        returnObjects.clear();

        if(object.get(i).getId() == ObjectId.Bullet){
            quad.retrieve(returnObjects, object.get(i).getBoundsAll());



            for(int k=0; k < returnObjects.size(); k++){

                Bullet bullet = (Bullet) returnObjects.get(k);


                if(getBoundsTop().intersects(bullet.getBoundsBottom())){
                    vy = speed; 
                    bullet.vy = -speed; 
                }

                if(getBoundsBottom().intersects(bullet.getBoundsTop())){
                    vy = -speed;
                    bullet.vy = speed;
                }

                if(getBoundsLeft().intersects(bullet.getBoundsRight())){
                    vx =speed;
                    bullet.vx = -speed;
                }

                if(getBoundsRight().intersects(bullet.getBoundsLeft())){
                    vx = -speed;
                    bullet.vx = speed;
                }
            }


        }
    }

}

Any help would be appreciated. Thanks in advance.

© Game Development or respective owner

Related posts about java

Related posts about algorithm