Page 1 of 1

Collision detection with more objects

Posted: Wed Jan 07, 2009 8:41 pm
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

Posted: Wed Jan 07, 2009 10:05 pm
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

Posted: Thu Jan 08, 2009 10:13 am
by rogerborg
If you're using 1.5, then examples\15.LoadIrrFile\main.cpp also demonstrates this.

Posted: Thu Jan 08, 2009 3:10 pm
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