Distance from node to node
Distance from node to node
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?
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?
You are in the right direction:
While n1 is the node and n2 is the camera.
Code: Select all
n1->getAbsolutePosition().getDistanceFrom(n2->getAbsolutePosition())
-
- Posts: 299
- Joined: Mon Nov 27, 2006 6:52 pm
- Location: GERMANY
- Contact:
but this wouldn't work like you expected...Masterhawk wrote:distance = sqrt((d.x)^2 + (d.y)^2 + (d.z)^2)
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));
Code: Select all
distance = sqrt((d.x * d.x) + (d.y * d.y) + (d.z * d.z));
while(!asleep) sheep++;
IrrExtensions:
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
IrrExtensions:
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
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.
Correct me if I am wrong, because this has always puzzled me.
TheQuestion = 2B || !2B
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...or the cubic root:
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
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:
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
IrrExtensions:
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
-
- Posts: 299
- Joined: Mon Nov 27, 2006 6:52 pm
- Location: GERMANY
- Contact:
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.Masterhawk wrote:Of course you're right. This peace of "code" should only show the mathematical way and wasn't intended to be "correct c/c++"Acki wrote:but this wouldn't work like you expected...Masterhawk wrote:distance = sqrt((d.x)^2 + (d.y)^2 + (d.z)^2)
in C/C++ the ^ doesn't mean "power of" it means xor !!!
TheQuestion = 2B || !2B
-
- Admin
- Posts: 3590
- Joined: Mon Oct 09, 2006 9:36 am
- Location: Scotland - gonnae no slag aff mah Engleesh
- Contact:
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
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
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.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.
TheQuestion = 2B || !2B