Distance from node to node

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
admgamer
Posts: 21
Joined: Tue Feb 12, 2008 10:49 am

Distance from node to node

Post by admgamer »

Hi,

I have a SceneNode that is in a static location, and i basically want to check if the FPS camera that you control is within a certain distance from this SceneNode.

I thought you would need to do something such as get the node position with getPosition, and then check if the player controlled camera is within a certain radial distance but i'm having trouble figuring out how to easily do this.

does anyone know any functionality that can do this? i think i'm right in thinking that just using XYZ of scene nodes isnt enough as this doesn't help one bit with the player being somewhere within the circumference?
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

You are in the right direction:

Code: Select all

n1->getAbsolutePosition().getDistanceFrom(n2->getAbsolutePosition())
While n1 is the node and n2 is the camera.
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
admgamer
Posts: 21
Joined: Tue Feb 12, 2008 10:49 am

Post by admgamer »

thanks for the reply, i JUSt saw this getDistanceFrom and realised how much of a moron i am lol

cheers
Masterhawk
Posts: 299
Joined: Mon Nov 27, 2006 6:52 pm
Location: GERMANY
Contact:

Post by Masterhawk »

do you just want to have a distance between two points?

d = point1 - point2

distance = sqrt((d.x)^2 + (d.y)^2 + (d.z)^2)


i think that should do the trick...

EDIT: too late and too complicated.... ;)
Image
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

Masterhawk wrote:distance = sqrt((d.x)^2 + (d.y)^2 + (d.z)^2)
but this wouldn't work like you expected...
in C/C++ the ^ doesn't mean "power of" it means xor !!! ;)

so it should be:

Code: Select all

distance = sqrt(pow(d.x, 2) + pow(d.y, 2) + pow(d.z, 2));
or:

Code: Select all

distance = sqrt((d.x * d.x) + (d.y * d.y) + (d.z * d.z));
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Halifax
Posts: 1424
Joined: Sun Apr 29, 2007 10:40 pm
Location: $9D95

Post by Halifax »

Yeah, I would go with the latter, Acki. I don't know if I am correct, but the pow() function would present some overhead, especially since I believe it uses a genric algorithm so that you could also calculate square roots with pow() etc. Either way, it is still fast, but I just wanted to put that out there.

Correct me if I am wrong, because this has always puzzled me.
TheQuestion = 2B || !2B
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

I'm not sure about the overhead, too...
I for myself would also prefere the second line I posted... ;)

and of course you can do a square root, or any other root, with pow(), you just need to calculate in the power of the reciprocal value... ;)

Code: Select all

// get the square root 
f32 val = pow(4, 1.0 / 2.0); // val = 2.0
f32 val = pow(9, 1.0 / 2.0); // val = 3.0
or the cubic root:

Code: Select all

// get the cubic root 
f32 val = pow(8, 1.0 / 3.0); // val = 2.0
f32 val = pow(27, 1.0 / 3.0); // val = 3.0
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Masterhawk
Posts: 299
Joined: Mon Nov 27, 2006 6:52 pm
Location: GERMANY
Contact:

Post by Masterhawk »

Acki wrote:
Masterhawk wrote:distance = sqrt((d.x)^2 + (d.y)^2 + (d.z)^2)
but this wouldn't work like you expected...
in C/C++ the ^ doesn't mean "power of" it means xor !!! ;)
Of course you're right. This peace of "code" should only show the mathematical way and wasn't intended to be "correct c/c++" ;)
Image
Halifax
Posts: 1424
Joined: Sun Apr 29, 2007 10:40 pm
Location: $9D95

Post by Halifax »

Masterhawk wrote:
Acki wrote:
Masterhawk wrote:distance = sqrt((d.x)^2 + (d.y)^2 + (d.z)^2)
but this wouldn't work like you expected...
in C/C++ the ^ doesn't mean "power of" it means xor !!! ;)
Of course you're right. This peace of "code" should only show the mathematical way and wasn't intended to be "correct c/c++" ;)
I'm sure he was just correcting you, so that possibly you would show a correct form next time. It is assumed that everyone on this forum uses C/C++ so it is best to put it in that language, for the newbies.
TheQuestion = 2B || !2B
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Just as an aside, getDistanceFromSq() (and comparing it to Distance * Distance) is more efficient, as it avoids the sqrt().
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

pow is equivalent to the direct multiplication once you disable IEEE precision requirements. That's e.g. possible by passing the -ffast-math to the gcc (and therefore made in the Makefile for the optimized compile). Otherwise it's usually a little slower, but more accurate.
Halifax
Posts: 1424
Joined: Sun Apr 29, 2007 10:40 pm
Location: $9D95

Post by Halifax »

hybrid wrote:pow is equivalent to the direct multiplication once you disable IEEE precision requirements. That's e.g. possible by passing the -ffast-math to the gcc (and therefore made in the Makefile for the optimized compile). Otherwise it's usually a little slower, but more accurate.
Ah, okay, thanks for clearing that up hybrid. Now I know, because I used to be a heavy advocate of not using it for something that could be represented with straight multiplication. :lol:
TheQuestion = 2B || !2B
Post Reply