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

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Raden Mu'az
Posts: 12
Joined: Sat Dec 27, 2008 1:26 pm
Contact:

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

Post 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.
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post 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)
Image Image Image
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post 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.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
Raden Mu'az
Posts: 12
Joined: Sat Dec 27, 2008 1:26 pm
Contact:

Post 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;
    }
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post 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...
Image Image Image
Raden Mu'az
Posts: 12
Joined: Sat Dec 27, 2008 1:26 pm
Contact:

Post by Raden Mu'az »

How about the spaceship colliding with a rock? Does this also need a ray for coll. detection?
porcus
Posts: 149
Joined: Sun May 27, 2007 6:24 pm
Location: Germany

Post 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.
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post 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
Post Reply