Page 1 of 1

How to find the distance between 2 animated meshes?

Posted: Tue Apr 17, 2012 7:32 am
by chaiein
I want to know the distance between two meshes(node) so that the value is less that is the meshes come close or intersect any one mesh will be destroyed Please help me how to do this?

Re: How to find the distance between 2 animated meshes?

Posted: Tue Apr 17, 2012 8:45 am
by CuteAlien
This would use circles arund your meshes that go around the boundingbox and the nodes collide as soon as they are closer together than that (most typical case for collisions, although sometimes people also use the inner-radius which touches only the sides of the boundingbox and not the corners):

Code: Select all

 
irr::scene::IMeshSceneNode * node1 =  .... ; // replace ... by whatever method you use to get your node
irr::scene::IMeshSceneNode * node2 =  .... ; // replace ... by whatever method you use to get your node
irr::f32 minRadius1 = node1->getMesh()->getBoundingBox().getExtent().getLength() * 0.5f; // maximal radius the mesh will have
irr::f32 minRadius2 = node1->getMesh()->getBoundingBox().getExtent().getLength() * 0.5f; // maximal radius the mesh will have
irr::f32 distance = node1->getAbsolutePosition().getDistanceFrom(node2->getAbsolutePosition());
if ( distance < minRadius1+minRadius2 ) 
{
 node2->remove(); // or sometimes you just hide it with node2->setVisible(false), depends on the situation
}
 

edit: fixed missing .getLength()

Re: How to find the distance between 2 animated meshes?

Posted: Tue Apr 17, 2012 11:14 am
by chaiein

Code: Select all

scene::IAnimatedMeshSceneNode* node2 ;
scene::IAnimatedMeshSceneNode* node3;
node2=smgr->addAnimatedMeshSceneNode(smgr->getMesh("ninja.b3d"),0,-1);
 node3=smgr->addAnimatedMeshSceneNode(smgr->getMesh("ninja.b3d"),camera,-1);
 
my nodes are of IAnimatedMeshSceneNode type

Code: Select all

 
f32 minRadius1 = node3->getMesh()->getBoundingBox().getExtent() * 0.5f; 
f32 minRadius2 = node3->getMesh()->getBoundingBox().getExtent() * 0.5f; 
f32 distance = node1->getAbsolutePosition().getDistanceFrom(node2->getAbsolutePosition());
if ( distance < minRadius1+minRadius2 ) 
 {
     node2->setVisible(false); 
}
I get these errors
 
Error   3       error C2440: 'initializing' : cannot convert from 'irr::core::vector3d<T>' to 'irr::f32'       
Error   4       error C2440: 'initializing' : cannot convert from 'irr::core::vector3d<T>' to 'irr::f32'       
 
 
when i changed the code to below I got one more error
 

Code: Select all

 
core::vector3df minRadius1 = node3->getMesh()->getBoundingBox().getExtent() * 0.5f;
core::vector3df minRadius2 = node2->getMesh()->getBoundingBox().getExtent() * 0.5f;
f32 distance = node1->getAbsolutePosition().getDistanceFrom(node2->getAbsolutePosition());
if ( distance<(minRadius1+minRadius2))
{
node2->setVisible(false);
 }
 
Error 3 error C2677: binary '<' : no global operator found which takes type 'irr::core::vector3d<T>' (or there is no acceptable conversion)

Re: How to find the distance between 2 animated meshes?

Posted: Tue Apr 17, 2012 11:32 am
by REDDemon
you can't compare a vector with a float. You need to get lenght from vector.

Code: Select all

if(distance < (minRad1+minRad2).getLenght() )
or better:

Code: Select all

if( (distance*distance) < (minRad1+minRad2).getLenghtSQ() )
edit: look at time between 2 post XD just few seconds

Re: How to find the distance between 2 animated meshes?

Posted: Tue Apr 17, 2012 11:33 am
by teto
The documentation is not correct : "Get extent of the box (maximal distance of two points in the box). " while getExtent returns MaxEdge-MinEdge.
In pseudo code : (minRadius1+minRadius2).getLength ()

Btw for performance reasons I advise you to compare squared distances.

Edit: Reddemon beat me there ^^

Re: How to find the distance between 2 animated meshes?

Posted: Tue Apr 17, 2012 11:36 am
by CuteAlien
Ah yes sorry - I forgot the .getLength(). I've edited my code above.
And yeah, there is a possibility for optimization - but I want to keep it maximal readable for now so it's obvious why it works.

edit: IAnimatedMeshSceneNode also has a getMesh, you just have to pass your frame additionally. Although for collision it might be better using a fixed size, for example getMesh(0). Or often you use an additional (invisible) mesh for the collision geometry, or just figure out the radius in another way (try&error, or measuring your sizes in a modeling tool) and set it directly. The only reason I get the mesh there is to figure out the size and not knowing anything else about your game that is a general method that works more less well.

Re: How to find the distance between 2 animated meshes?

Posted: Wed Apr 18, 2012 3:09 am
by chaiein
I tried the following code which should make the node2 to be invisible/remove when node3 reaches it. but its not getting invisible/remove but the errors are cleared.

Code: Select all

 
        smgr->drawAll();
                core::vector3df minRadius1 = node3->getMesh()->getBoundingBox().getExtent() * 0.5f;
                core::vector3df minRadius2 = node2->getMesh()->getBoundingBox().getExtent() * 0.5f;
                f32 distance = node3->getAbsolutePosition().getDistanceFrom(node2->getAbsolutePosition());
                if ((distance*distance)<(minRadius1+minRadius2).getLengthSQ())
                {
                        node2->remove();
                }
                        guienv->drawAll();
 
Is the position of the code between drawall correct? if yes then why am I not able to remove.

And I have a clarification,
Is it possible to do this work using any code in Irrlicht or will the physics engineshelp us to do if yes please help me to name the best with Irrlicht?

Re: How to find the distance between 2 animated meshes?

Posted: Wed Apr 18, 2012 8:20 am
by CuteAlien
chaiein wrote: ... but its not getting invisible/remove but the errors are cleared.
Sorry, that sentence makes no sense. What does "the errors are cleared" mean? Try to formulate it different.

My guess would be that you might get a crash soon afterward because you don't set node2 to 0 afterward (you just removed the object so now the pointer points to random memory as usual in c++) and you also don't check if node2 is valid before using it.

Also please start with my code, I have edited it yesterday. Aways get things working before you start optimizing for speed - especially when you don't understand yet what you are doing.
(minRadius1+minRadius2).getLengthSQ() is _not_ the same as minRadius1.getLengthSQ() + minRadius2.getLengthSQ() unless your boundingboxes are quadratic as the vectors are otherwise not collinear (you can always calculate the stuff through by hand with examples by the way - the calculations used in there are all visible in the source and not hard).

Re: How to find the distance between 2 animated meshes?

Posted: Wed Apr 18, 2012 8:45 am
by chaiein

Code: Select all

   
core::vector3df minRadius1 = node3->getMesh()->getBoundingBox().getExtent()*2.5f;
                core::vector3df minRadius2 = node2->getMesh()->getBoundingBox().getExtent()*2.5f;
                f32 distance = node3->getAbsolutePosition().getDistanceFrom(node2->getAbsolutePosition());
                                f32 temp;
                                temp=(minRadius1+minRadius2).getLength();
                                if ( distance<temp)
                {
                                        node3->setFrameLoop(134,145);
                                        node2->setFrameLoop(166,173);
                }
As CuteAlien said I got break while removing so instead I gave setFrameLoop to avoid the problem.And I updated *5.0f to *2.5f in the above code while calculatin minRadius.

Thank you very much :)
Thank you:)

Re: How to find the distance between 2 animated meshes?

Posted: Wed Apr 18, 2012 9:26 am
by CuteAlien
You know how to use a debugger I guess? Just set a breakpoint there and check your values of minRadius1 and minRadius2 (if you use VS the debugger will give show you all those values in "locals". Maybe your meshes have a messed up boundingbox.

And no - it is still not correct - (minRadius1+minRadius2).getLength() is certainly also not the same as minRadius1.getLenght() + minRadius2.getLenght(). Just draw some vector on paper to check it. Ohterwise just use my code as I posted it instead of modifying it always without understanding what you do.

Anyway - use a debugger and check what radius you get - whatever solution you use. Maybe it's messed up. If you use my solution above you can also replace the boundingbox stuff easy by some fixed values. And also maybe just print-out distance (you can use a staticText for text-output for example).

Please don't expect we do the coding for you - this is all still _very_ trivial stuff. If you don't even try to understand what's going on there you won't make it far in your game. You need at least a rudimentary understanding of vector-math to do a 3D application. Otherwise I would recommend to start with a 2D game and go back to 3D once you got the math and have a better understanding of c++ as well.

Re: How to find the distance between 2 animated meshes?

Posted: Wed Apr 18, 2012 10:00 am
by chaiein
Ya I got already before I read ur previous reply similar to ur explaination .its working!! I used FrameLoop that's enough :D
Thank yiou :)