Collision detection with more objects

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
infx
Posts: 3
Joined: Wed Jan 07, 2009 8:28 pm

Collision detection with more objects

Post by infx »

hello,
I have a problem with collision detection ... I have two (or more in the future) "ISceneNode". I have two objects, and for both i want same collision.

My code:

Code: Select all

	scene::ISceneNode* bedna = smgr->addCubeSceneNode(20);

		bedna = smgr->addCubeSceneNode(); // first cube
		bedna->setMaterialTexture(0, driver->getTexture("data/images/bedna.jpg"));
		bedna->setMaterialFlag(video::EMF_LIGHTING, false);
		bedna->setPosition(core::vector3df(300,20,150));
		bedna->setScale(core::vector3df(3,3,3));

		bedna = smgr->addCubeSceneNode(); // second cube
		bedna->setMaterialTexture(0, driver->getTexture("data/images/bedna.jpg"));
		bedna->setMaterialFlag(video::EMF_LIGHTING, false);
		bedna->setPosition(core::vector3df(350,20,150));
		bedna->setScale(core::vector3df(3,3,3));

// for both want to have the same collision detection
This is code with collision detection:

Code: Select all

	scene::ITriangleSelector* select_bedna
		= smgr->createTriangleSelectorFromBoundingBox(bedna);

	scene::ISceneNodeAnimatorCollisionResponse *bedna_g = smgr->createCollisionResponseAnimator(
		select_bedna,
			node,
        vector3df(20, 26, 20),
        vector3df(0, 0, 0),
        vector3df(0, 0, 0),
        0.00080f);
	node->addAnimator(bedna_g);
	bedna_g->drop();
Sorry for my english... Thanks for answer
ayboangelus
Posts: 9
Joined: Thu Dec 11, 2008 12:02 am

Post by ayboangelus »

Hardcpp for french irrlicht website have create a script for collision for all.

Code: Select all

    scene::IMetaTriangleSelector * meta = smgr->createMetaTriangleSelector();

    core::array<scene::ISceneNode *> nodes;
    smgr->getSceneNodesFromType(scene::ESNT_ANY, nodes);

    for (u32 i=0; i < nodes.size(); ++i)
    {
        scene::ISceneNode * node = nodes[i];
        scene::ITriangleSelector * selector = 0;

        switch(node->getType())
        {
            case scene::ESNT_CUBE:
            case scene::ESNT_ANIMATED_MESH:
                selector = smgr->createTriangleSelectorFromBoundingBox(node);
            break;

            case scene::ESNT_MESH:
            case scene::ESNT_SPHERE:
                selector = smgr->createTriangleSelector(((scene::IMeshSceneNode*)node)->getMesh(), node);
                break;

            case scene::ESNT_TERRAIN:
                selector = smgr->createTerrainTriangleSelector((scene::ITerrainSceneNode*)node);
                break;

            case scene::ESNT_OCT_TREE:
                selector = smgr->createOctTreeTriangleSelector(((scene::IMeshSceneNode*)node)->getMesh(), node);
                break;

            default:
                break;
        }

        if(selector)
        {
            meta->addTriangleSelector(selector);
            selector->drop();
        }
    }

    scene::ISceneNodeAnimator* anim = smgr->createCollisionResponseAnimator(
        meta, camera, core::vector3df(5,5,5),
        core::vector3df(0,0,0));
    meta->drop();

    camera->addAnimator(anim);
    anim->drop();
Try with this
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

If you're using 1.5, then examples\15.LoadIrrFile\main.cpp also demonstrates this.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

Since you are trying to do colliison between multiple different nodes, you will need to augment the above code slightly, and you should keep a few things in mind.

First, the collision response animator uses an ellipse for collisions. This means that if you have a bunch of boxes that are setup to collide with each other, it will look like they are not touching when they are in collision.

Second, for any given node, you do not want that nodes triangle selector to be in the meta selector that is used for its own collision animator. i.e., if you have three cubes that can collide with each other (say A, B and C), the meta selector used for A's collision response animator will only include selectors for B and C. B would only have selectors for A and C... Get it?

It should be noted that you would want to create selectors for any given scene node. It might also be useful to tell the node about its triangle selector with a call to ISceneNode::setTriangleSelector().

Once every node that you want to collide has a selector, you need to go back and create meta selectors for use with the collision response animators of the nodes that will be colliding with them.

Travis
Post Reply