collision help

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.
spearfire
Posts: 11
Joined: Sat Apr 12, 2008 10:26 am

collision help

Post by spearfire »

hello
i am searching 4 day's now for a awnser to how to do some simple collisions
i can get it to work with my terrain but not with some objects i created
i hope someone can help tell me how to get collisions with my model
here is my code from my model

spearfire


Code: Select all

my model(without collisions)
//my model
IAnimatedMesh* indu = smgr->getMesh("c:/irrlicht-1.4/media/indus.X");
IAnimatedMeshSceneNode* s = smgr->addAnimatedMeshSceneNode( indu );

if (s)
{
	s = smgr->addAnimatedMeshSceneNode(indu);
    s->setMaterialFlag(EMF_LIGHTING, true);
    s->setPosition(core::vector3df(2150*10,30*2,3650*2));
    s->setScale(core::vector3df(8,8,8));
    s->setRotation(core::vector3df(-90,0,0));
   	s->addShadowVolumeSceneNode();	
         smgr->setShadowColor(video::SColor(220,0,0,0));
}

my camera only with collisions for my terrain
//my camera
scene::ICameraSceneNode* camera = 
    smgr->addCameraSceneNodeFPS(0,45.0f,650.0f);
camera->setPosition(core::vector3df(1900*10,255*2,3700*2));
camera->setTarget(core::vector3df(2397*2,343*2,2700*2));
camera->setFarValue(12000.0f);
scene::ISceneNodeAnimator* anim =
    smgr->createCollisionResponseAnimator(

		selector, camera, core::vector3df(30,30,30),

		core::vector3df(0,-3,0),

		core::vector3df(0,50,0));


	camera->addAnimator(anim);
B@z
Posts: 876
Joined: Thu Jan 31, 2008 5:05 pm
Location: Hungary

Post by B@z »

you have to use meta triangle selector.
search it on forums, you'll find something ;)
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Specifically...

Code: Select all

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

// Then create your ITriangleSelectors as normal and for each one
// add it to the meta selector, which will take a reference to it
scene::ITriangleSelector * selector = ... create with an appropriate method
meta->addTriangleSelector(selector);
 // And drop your reference to it, so that the meta selector owns it.
selector->drop();


// After adding all the triangle selectors to the meta selector
// create a collision response animator from that meta selector.
// Note that the IMetaTriangleSelector is passed to a method that
// expects an ITriangleSelector.  This is correct; IMetaTriangleSelector
// is derived from ITriangleSelectors, so it IS an ITriangleSelector.
scene::ISceneNodeAnimator* anim = smgr->createCollisionResponseAnimator(meta, camera, core::vector3df(5,5,5), core::vector3df(0,0,0));
meta->drop(); // I'm done with the meta selector now 
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
spearfire
Posts: 11
Joined: Sat Apr 12, 2008 10:26 am

Post by spearfire »

rogerborg wrote:Specifically...

Code: Select all

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

// Then create your ITriangleSelectors as normal and for each one
// add it to the meta selector, which will take a reference to it
scene::ITriangleSelector * selector = ... create with an appropriate method
meta->addTriangleSelector(selector);
 // And drop your reference to it, so that the meta selector owns it.
selector->drop();


// After adding all the triangle selectors to the meta selector
// create a collision response animator from that meta selector.
// Note that the IMetaTriangleSelector is passed to a method that
// expects an ITriangleSelector.  This is correct; IMetaTriangleSelector
// is derived from ITriangleSelectors, so it IS an ITriangleSelector.
scene::ISceneNodeAnimator* anim = smgr->createCollisionResponseAnimator(meta, camera, core::vector3df(5,5,5), core::vector3df(0,0,0));
meta->drop(); // I'm done with the meta selector now 
well i looked at this and searched again for a few hours but i dont seem to get this code to work in my project or find a other code that i could use

could someone put his code in my code(first post)??
i hope someone can help me further

spearfire
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Sure! Can I get you a beer while I'm up? How about a blowj^W a massage?
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
spearfire
Posts: 11
Joined: Sat Apr 12, 2008 10:26 am

Post by spearfire »

rogerborg wrote:Sure! Can I get you a beer while I'm up? How about a blowj^W a massage?
.....................
hey i am just asking a question if you or someone else could help a beginner if you don't want to awnser it then don't but please do not make this kind of post!!
frostysnowman
Posts: 83
Joined: Fri Apr 11, 2008 3:06 am
Location: Beaverton, Oregon, US

Post by frostysnowman »

I was looking over this thread, and have been trying to do the same thing. Here is my code:

Code: Select all

void setCollisionWithObject(ISceneNode* a_obj, const c8 * a_objMeshFilePath, ISceneNode* a_obj2, const c8 * a_obj2MeshFilePath)
{
	ISceneNode* obj = a_obj;
	ISceneNode* obj2 = a_obj2;

	IMesh* objMesh = smgr->getMesh(a_objMeshFilePath);
	IMesh* obj2Mesh = smgr->getMesh(a_obj2MeshFilePath);

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

	// Then create your ITriangleSelectors as normal and for each one
	// add it to the meta selector, which will take a reference to it
	scene::ITriangleSelector * selector = smgr->createTriangleSelector(objMesh, obj);
	meta->addTriangleSelector(selector);
	
 // And drop your reference to it, so that the meta selector owns it.
	selector->drop();


	// After adding all the triangle selectors to the meta selector
	// create a collision response animator from that meta selector.
	// Note that the IMetaTriangleSelector is passed to a method that
	// expects an ITriangleSelector.  This is correct; IMetaTriangleSelector
	// is derived from ITriangleSelectors, so it IS an ITriangleSelector.
	scene::ISceneNodeAnimator* anim = smgr->createCollisionResponseAnimator(meta, obj2, core::vector3df(5,5,5), core::vector3df(0,-1,0));

	obj2->addAnimator(anim);

	meta->drop(); // I'm done with the meta selector now

}

As you can see, I copied almost entirely all of the code posted above to create this function. Problem is, obj2 just falls through obj's geometry and it does not work. I am confused, can anyone spot my mistake?
B@z
Posts: 876
Joined: Thu Jan 31, 2008 5:05 pm
Location: Hungary

Post by B@z »

well i looked at this and searched again for a few hours but i dont seem to get this code to work in my project or find a other code that i could use

could someone put his code in my code(first post)??
i hope someone can help me further

spearfire
i think this should work

Code: Select all

scene::IMetaTriangleSelector * meta = smgr->createMetaTriangleSelector(); 
scene::ITriangleSelector * selector;
AnimatedMesh* indu = smgr->getMesh("c:/irrlicht-1.4/media/indus.X");
IAnimatedMeshSceneNode* s = smgr->addAnimatedMeshSceneNode( indu );

if (s)
{
   s = smgr->addAnimatedMeshSceneNode(indu); // you should delete it. it creates the same model twice..
   s->setMaterialFlag(EMF_LIGHTING, true);
   s->setPosition(core::vector3df(2150*10,30*2,3650*2));
   s->setScale(core::vector3df(8,8,8));
   s->setRotation(core::vector3df(-90,0,0));
   s->addShadowVolumeSceneNode();   
   smgr->setShadowColor(video::SColor(220,0,0,0));
   selector = smgr->createTriangleSelector(s->getMesh(),s);
   meta->addTriangleSelector(selector);
   selector->drop();
}

my camera only with collisions for my terrain
//my camera
scene::ICameraSceneNode* camera =
smgr->addCameraSceneNodeFPS(0,45.0f,650.0f);
camera->setPosition(core::vector3df(1900*10,255*2,3700*2));
camera->setTarget(core::vector3df(2397*2,343*2,2700*2));
camera->setFarValue(12000.0f);
scene::ISceneNodeAnimator* anim =
smgr->createCollisionResponseAnimator(meta, camera, core::vector3df(30,30,30),core::vector3df(0,-3,0),core::vector3df(0,50,0));
meta->drop();
camera->addAnimator(anim);
anim->drop();
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Oh balls. This is down to two problems:

1) IAnimatedMesh is now derived from IMesh, so IAnimatedMesh * can be assigned to IMesh * variables and passed to methods that take an IMesh *. However, this generally does not do what you'd expect. You should instead manually do a getMesh(0) on the IAnimatedMesh *.

2) Except that won't work anyway, because IAnimatedMesh::getMesh() doesn't work for skinned meshes in 1.4.

Both of these are known problems. As far as I can tell, there's no sense of urgency to fix them.

In short: accurate per-triangle collision detection with animated meshes is currently borked in Irrlicht 1.4 and the SVN trunk.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
spearfire
Posts: 11
Joined: Sat Apr 12, 2008 10:26 am

Post by spearfire »

B@z wrote:
well i looked at this and searched again for a few hours but i dont seem to get this code to work in my project or find a other code that i could use

could someone put his code in my code(first post)??
i hope someone can help me further

spearfire
i think this should work

Code: Select all

scene::IMetaTriangleSelector * meta = smgr->createMetaTriangleSelector(); 
scene::ITriangleSelector * selector;
AnimatedMesh* indu = smgr->getMesh("c:/irrlicht-1.4/media/indus.X");
IAnimatedMeshSceneNode* s = smgr->addAnimatedMeshSceneNode( indu );

if (s)
{
   s = smgr->addAnimatedMeshSceneNode(indu); // you should delete it. it creates the same model twice..
   s->setMaterialFlag(EMF_LIGHTING, true);
   s->setPosition(core::vector3df(2150*10,30*2,3650*2));
   s->setScale(core::vector3df(8,8,8));
   s->setRotation(core::vector3df(-90,0,0));
   s->addShadowVolumeSceneNode();   
   smgr->setShadowColor(video::SColor(220,0,0,0));
   selector = smgr->createTriangleSelector(s->getMesh(),s);
   meta->addTriangleSelector(selector);
   selector->drop();
}

my camera only with collisions for my terrain
//my camera
scene::ICameraSceneNode* camera =
smgr->addCameraSceneNodeFPS(0,45.0f,650.0f);
camera->setPosition(core::vector3df(1900*10,255*2,3700*2));
camera->setTarget(core::vector3df(2397*2,343*2,2700*2));
camera->setFarValue(12000.0f);
scene::ISceneNodeAnimator* anim =
smgr->createCollisionResponseAnimator(meta, camera, core::vector3df(30,30,30),core::vector3df(0,-3,0),core::vector3df(0,50,0));
meta->drop();
camera->addAnimator(anim);
anim->drop();
Thank you verry much i changed your code a bit becaus at first it did not work
but now it finaly WORKS!!! (if you enter the object i gets a bit buggy) i got collisions with my terrain and my model
now i can finaly continue my project
again thank you very much and every one else who helped me

here is my working code for if someone else doesnt get his collisions right

Code: Select all

 IAnimatedMesh* indu = smgr->getMesh("c:/irrlicht-1.4/media/waren.X");
IAnimatedMeshSceneNode* s = smgr->addAnimatedMeshSceneNode( indu );
scene::IMetaTriangleSelector * meta = smgr->createMetaTriangleSelector();
scene::ITriangleSelector * selector1;

if (s)
{
   s->setMaterialFlag(EMF_LIGHTING, true);
   s->setPosition(core::vector3df(2000*10,30*2,3650*2));
   s->setScale(core::vector3df(8,8,8));
   s->setRotation(core::vector3df(-90,-90,0));
   s->addShadowVolumeSceneNode();   
   smgr->setShadowColor(video::SColor(220,0,0,0));
   selector1 = smgr->createTriangleSelector(s->getMesh(),s);
   meta->addTriangleSelector(selector1);
   selector1->drop(); 	
}

//my camera
scene::ICameraSceneNode* camera =
smgr->addCameraSceneNodeFPS(0,45.0f,650.0f);
{
if(camera)
camera->setPosition(core::vector3df(1900*10,255*2,3700*2));
camera->setTarget(core::vector3df(2397*2,343*2,2700*2));
camera->setFarValue(12000.0f);
scene::ISceneNodeAnimator* anim = 
smgr->createCollisionResponseAnimator(meta, camera, core::vector3df(30,30,30),core::vector3df(0,-3,0),core::vector3df(0,50,0));
meta->drop();
camera->addAnimator(anim);
anim->drop();

scene::ISceneNodeAnimator* an = 
smgr->createCollisionResponseAnimator(selector, camera, core::vector3df(30,30,30),core::vector3df(0,0,0),core::vector3df(0,50,0));
selector->drop();
camera->addAnimator(an);
an->drop();
} 
B@z
Posts: 876
Joined: Thu Jan 31, 2008 5:05 pm
Location: Hungary

Post by B@z »

spearfire wrote: Thank you verry much i changed your code a bit becaus at first it did not work
but now it finaly WORKS!!! (if you enter the object i gets a bit buggy) i got collisions with my terrain and my model
now i can finaly continue my project
again thank you very much and every one else who helped me

here is my working code for if someone else doesnt get his collisions right

Code: Select all

 IAnimatedMesh* indu = smgr->getMesh("c:/irrlicht-1.4/media/waren.X");
IAnimatedMeshSceneNode* s = smgr->addAnimatedMeshSceneNode( indu );
scene::IMetaTriangleSelector * meta = smgr->createMetaTriangleSelector();
scene::ITriangleSelector * selector1;

if (s)
{
   s->setMaterialFlag(EMF_LIGHTING, true);
   s->setPosition(core::vector3df(2000*10,30*2,3650*2));
   s->setScale(core::vector3df(8,8,8));
   s->setRotation(core::vector3df(-90,-90,0));
   s->addShadowVolumeSceneNode();   
   smgr->setShadowColor(video::SColor(220,0,0,0));
   selector1 = smgr->createTriangleSelector(s->getMesh(),s);
   meta->addTriangleSelector(selector1);
   selector1->drop(); 	
}

//my camera
scene::ICameraSceneNode* camera =
smgr->addCameraSceneNodeFPS(0,45.0f,650.0f);
{
if(camera)
camera->setPosition(core::vector3df(1900*10,255*2,3700*2));
camera->setTarget(core::vector3df(2397*2,343*2,2700*2));
camera->setFarValue(12000.0f);
scene::ISceneNodeAnimator* anim = 
smgr->createCollisionResponseAnimator(meta, camera, core::vector3df(30,30,30),core::vector3df(0,-3,0),core::vector3df(0,50,0));
meta->drop();
camera->addAnimator(anim);
anim->drop();

scene::ISceneNodeAnimator* an = 
smgr->createCollisionResponseAnimator(selector, camera, core::vector3df(30,30,30),core::vector3df(0,0,0),core::vector3df(0,50,0));
selector->drop();
camera->addAnimator(an);
an->drop();
} 
uhm does it really work? :D

Code: Select all

smgr->addCameraSceneNodeFPS(0,45.0f,650.0f);
{
if(camera)
camera->setPosition(core::vector3df(1900*10,255*2,3700*2));
camera->setTarget(core::vector3df(2397*2,343*2,2700*2));
camera->setFarValue(12000.0f);
it means that if the camera isn't initialized, then it won't set the position, but set the target and the far value..

Code: Select all

if(camera)
{
camera->setPosition(core::vector3df(1900*10,255*2,3700*2));
change to this

Code: Select all


scene::ISceneNodeAnimator* an = 
smgr->createCollisionResponseAnimator(selector, camera, core::vector3df(30,30,30),core::vector3df(0,0,0),core::vector3df(0,50,0));
selector->drop();
camera->addAnimator(an);
an->drop();
you don't have selector, does you? :D
i think this isn't necessary.. maybe a mistake? :P
spearfire
Posts: 11
Joined: Sat Apr 12, 2008 10:26 am

Post by spearfire »

i do have selector for my terrain
it works only when i add gravity it becomes a bit buggy somehow and it wants to go trough the floor of my building
B@z
Posts: 876
Joined: Thu Jan 31, 2008 5:05 pm
Location: Hungary

Post by B@z »

i see..
it wasn't in the code, so didn't know...

i don't know the selector very well, but i think you shouldn't add 2 collision animators to one thing.
that's why there is the meta selector.

just add the "selector" to the "meta" and it will be all right i think. (just set the gravitation 0)

Code: Select all

meta->addTriangleSelector(selector); 
selector->drop();
scene::ISceneNodeAnimator* anim =
smgr->createCollisionResponseAnimator(meta, camera, core::vector3df(30,30,30),core::vector3df(0,0,0),core::vector3df(0,50,0));
meta->drop();
camera->addAnimator(anim);
anim->drop(); 
i think this should work ^^v
spearfire
Posts: 11
Joined: Sat Apr 12, 2008 10:26 am

Post by spearfire »

i have got it fully working now.
first, the camera wanted to go trough the floor of my object
in my camera part i had the object collisions first and then the terrain collisions

i changed that so now i have the terrain collisions first and then the object collisions and it works without a problem now!

wel thank you all who helped me
B@z
Posts: 876
Joined: Thu Jan 31, 2008 5:05 pm
Location: Hungary

Post by B@z »

i'm glad :D

i have a question too.
can i display the meta selector?

i have a problem with my collision. it goes throught the objects, but collide with air and so on :D
so i want to know where is my collision, and where isn't. (maybe it's just moved, not bad)
is there any method to display it?

EDIT:
it seems like i have some problems with my code, it isn't moved, but really bad :D
i'm using scaled models, maybe it's wrong?
do triangle selector supports scaled models?
Post Reply