Page 1 of 1

[n00b] How to do collision detection with a bounding box??

Posted: Fri Jan 16, 2009 9:41 am
by Raden Mu'az
I need help with collision bounding box - Irrlicht tut only shows me how to use rays to detect bumped scene nodes.
The collision section in this post doesn't solve my problem: http://irrlicht.sourceforge.net/phpBB2/ ... 481#155481


Say,
If a have a spaceship's bullet flying around with lots of rocks, how do I check if the spaceship's bullet collide with ANY rocks, thus doing some actions to the rocks (destroy it and the bullet, etc) - with a bounding box.

Thanks.

Posted: Fri Jan 16, 2009 9:51 am
by JP
Iterate through each bullet and then each rock and check whether their bounding boxes intersect using the functions of the bounding box class:

Code: Select all

for each bullet
    for each rock
        if bullet bounding box intersects with rock bounding box
            destroy rock (and bullet? if so break out of rock loop and move onto next bullet)

Posted: Fri Jan 16, 2009 9:53 am
by rogerborg
I'd use rays for that. :)

Consider that projectiles move relatively quickly. If you only checked the projectile against each box each frame (either as a point, or as a box-box test) then you run the risk of having it passing right through targets between one frame and the next.

So instead, store the projectile's previous position, and each frame create a ray from its previous position to its current position. Use that ray in your collision check, as per the collision example.

Posted: Fri Jan 16, 2009 1:27 pm
by Raden Mu'az
I've experimented something

I tried doing this but since Bullet is derived from Actor I always got "P5Actor" when doing "std::cout << world->actors; << "\n" ".

Code: Select all

core::array<Actor*> collide(f32 frameDeltaTime)
    {
        core::array<Actor*>collided;

        for(s32 i=0;i<(world->actors.size());i++)
        {
            Actor* c = world->actors[i];

            if (typeid(c) != typeid(this))
            if (this->node->getTransformedBoundingBox().intersectsWithBox(
            world->actors[i]->node->getTransformedBoundingBox()))
            {
                collided.push_back(c);
                //std::cout << world->actors[i]; << "\n";
            }
        }

        return collided;
    }

Posted: Fri Jan 16, 2009 1:57 pm
by JP
yeah rogerborg is right, you probably would be better off considering your bullet as a point rather than a box and then using a ray of its position last frame and position this frame...

Posted: Fri Jan 16, 2009 9:40 pm
by Raden Mu'az
How about the spaceship colliding with a rock? Does this also need a ray for coll. detection?

Posted: Fri Jan 16, 2009 10:34 pm
by porcus
No, for that I would check if the boundingboxes are intersecting
each other. Of course If you want to have a realistic behaviour
after the collision you could use a physics engine like newton.

Posted: Sat Jan 17, 2009 2:00 am
by vitek
Raden Mu'az wrote:How about the spaceship colliding with a rock? Does this also need a ray for coll. detection?
It depends on the size and speed of both objects. If an object is very small and fast (i.e., it can travel further in one 'tick' than length of its bounding box in that direction), then you should not use a simple bounding box test.

Travis