Get distance to object Irrlicht.

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
Justei
Posts: 47
Joined: Fri Aug 20, 2010 11:20 am

Get distance to object Irrlicht.

Post by Justei »

Well, I know this is a very simple task, however I thought someone may find it usefull xD...

Code: Select all

 
         float x,z, dx, dz, distance;
         x = selectedSceneNode->getPosition().X;
         z = selectedSceneNode->getPosition().Z;
 
         dx = (camera->getPosition().X > x) ? camera->getPosition().X - x : x - camera->getPosition().X;
         dz = (camera->getPosition().Z > z) ? camera->getPosition().Z - z : z - camera->getPosition().Z;
 
         cout << "Distance: " << sqrt (dx + dz) << endl;
 
Obviously this is only for distance with x and z, didnt need Y in my case so yeah... xD
Image
RdR
Competition winner
Posts: 273
Joined: Tue Mar 29, 2011 2:58 pm
Contact:

Re: Get distance to object Irrlicht.

Post by RdR »

Code: Select all

 
f32 distance = node->getAbsolutePosition().getDistanceFrom(camera->getAbsolutePosition());
 
http://irrlicht.sourceforge.net/docu/cl ... 127c3005e2

Should use getAbsolutePosition() else you might get problems when the nodes dont have the same parent.
Justei
Posts: 47
Joined: Fri Aug 20, 2010 11:20 am

Re: Get distance to object Irrlicht.

Post by Justei »

Huh, googled fast for a function like it but I needed my own anyway so I just made it ^^ but that's really neat lol. Oh well at least I got some more practice xD.

Edit: About getabsoluteposition, I actually don't have the same parent on my nodes and it seems to work fine ^^. But you are most likely right so I'll go for that :P thx for the tip :)
Image
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Get distance to object Irrlicht.

Post by mongoose7 »

No point in taking the square root as you didn't square the components (Pythagoras). Just depends what you need.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: Get distance to object Irrlicht.

Post by hybrid »

I move this away from code snippets, just to avoid people skipping over the rest and implementing these functions always on their own...
Post Reply