Make a Node Face another Node (updated)

A forum to store posts deemed exceptionally wise and useful
iamtehpwn
Posts: 30
Joined: Sat Mar 31, 2007 6:11 pm

Make a Node Face another Node (updated)

Post by iamtehpwn »

I figured this out myself because I'm currently coding an RPG.
So Lets say you have an NPC. The player talks to that NPC, the NPC needs to turn around so that he is facing the player, and appears to be talking to him.

I stumped with making a node face another node, but eventually I figured it out:

Enemy is a an AnimatedMeshSceneNode, and So is Player. I'm assuming you've declared these as global.

Code: Select all

vector3df eneloc = enemy->getPosition(); // Enemy location
vector3df plyloc = player->getPosition(); // Player location

 	   	enemy->updateAbsolutePosition();
	core::vector3df vect = plyloc - enemy->getAbsolutePosition();
	vect.Y = vect.getHorizontalAngle().Y;
    enemy->setRotation(vector3df(0, vect.Y, 0));

And There You have it!
I hope this will be useful to someone.

EDIT:
Oops, I just realized someone had already posted another way to do this.
But I assure you my method is Very efficient, uses less lines of code, and far more easier to understand than any other method I've seen.

EDIT2:
Updated due to complaints
Last edited by iamtehpwn on Sat Dec 29, 2007 10:29 pm, edited 6 times in total.
shogun
Posts: 162
Joined: Wed Sep 05, 2007 11:02 am
Location: inside

Re: Make a Node Face another Node

Post by shogun »

Code: Select all

	vect = vect.getHorizontalAngle();
	eneloc.Y = vect.Y;
    enemy->setRotation(vector3df(0,eneloc.Y, 0));
Do you really call that "efficient"?
iamtehpwn
Posts: 30
Joined: Sat Mar 31, 2007 6:11 pm

Post by iamtehpwn »

I don't see any flaw it.

If other than the fact that rotation is sudden, and not smooth. If thats what you want, I figured everyone would have the brains to increment Y axis so that they would do a slow transition.


Secondly, if you have a complaint, please point it out instead of trying to be insulting.
shogun
Posts: 162
Joined: Wed Sep 05, 2007 11:02 am
Location: inside

Post by shogun »

iamtehpwn wrote:I don't see any flaw it.
I really wonder why you're giving eneloc.Y the value of vect.Y and then use eneloc.Y, instead of using vect.Y directly.
iamtehpwn
Posts: 30
Joined: Sat Mar 31, 2007 6:11 pm

Post by iamtehpwn »

Oh. No real reason I guess. It doesn't matter, really. It's not really inefficient, considering it just adds 1 extra line of code, and it's not gonna effect memory usage (at all).

Either way, Look at the other person's post...They had many more lines of code. You should at least get commended for doing this in significantly less lines of code. And when I tried their's it didn't even function properly for me. :P
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

iamtehpwn wrote:They had many more lines of code.
Never decide by how many lines of code if its efficient or not..
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
shogun
Posts: 162
Joined: Wed Sep 05, 2007 11:02 am
Location: inside

Post by shogun »

iamtehpwn wrote:Either way, Look at the other person's post...They had many more lines of code. You should at least get commended for doing this in significantly less lines of code. And when I tried their's it didn't even function properly for me. :P
I don't know about the "other person's post" (you should try linking, this is the internet), but you should review your own code first. Especially when you're talking about efficiency.
E.g., "getHorizontalAngle()" also calculates the atan2 of X (just have a look at the docs) - you are only using Y. That's pretty inefficient, if you ask me.
iamtehpwn
Posts: 30
Joined: Sat Mar 31, 2007 6:11 pm

Post by iamtehpwn »

MasterGod wrote:
iamtehpwn wrote:They had many more lines of code.
Never decide by how many lines of code if its efficient or not..
I didn't. I just said I accomplished it in less lines of code. I was implying it was more easy to understand and implement into your applications, and that the other person used way more functions then did. :P
The few lines I did are practically nothing on virtually any computer hardware.

Secondly, I don't see what the big deal is. I'm just trying to help people who are having a problem with this. They can modify the code in whatever manner they want to, to make as good as they need. You act as if it's absolutely horrible. How Horrible can only a few, simplistic lines of code be?

Okay. There. I fixed it. It should still compile and run and the same...Now please stop complaining about Very minor issues.

And the other person's code.

http://irrlicht.sourceforge.net/phpBB2/ ... php?t=1325
shogun
Posts: 162
Joined: Wed Sep 05, 2007 11:02 am
Location: inside

Post by shogun »

I think BableOff's solution (with the improvement by Nick_Japan) is better than yours, because it doesn't calculate something which won't be needed anyway.
You should at least mention in your little code snippet that it does an unnecessary calculation of the X-angle.
iamtehpwn
Posts: 30
Joined: Sat Mar 31, 2007 6:11 pm

Post by iamtehpwn »

Uhh. Thats not true. If actually read my post above, you would be able to note that I modified the code above so that it only calculates the Y value, fixed at your request.
His method is not better than mines. I've tested all the methods I found available, including his. Have you?

Presumably, No. Perhaps you should actually see who's is better first than just assuming who's better. I have no idea what vendetta in which you have with me for posting simple code. Unless you want to save every single byte of memory (local variables are usually not that bad in memory usage, and such a small calculation is nothing on any modern day 2ghz+ CPU), Then you should really consider Reading my newly updated and post and thinking first.

Really, stop making a big deal out of nothing.
shogun
Posts: 162
Joined: Wed Sep 05, 2007 11:02 am
Location: inside

Post by shogun »

iamtehpwn wrote:Uhh. Thats not true. If actually read my post above, you would be able to note that I modified the code above so that it only calculates the Y value, fixed at your request.
No, you didn't. This is by no means a vendetta (sorry if you have the feeling), I just want to help you understand what exactly you are doing. Please look up what "getHorizontalAngle()" in "vect.Y = vect.getHorizontalAngle().Y;" does. You can do that by doing a search in the docs.

You will see the following code:

Code: Select all

00235                 vector3d<T> getHorizontalAngle()
00236                 {
00237                         vector3d<T> angle;
00238 
00239                         angle.Y = (T)atan2(X, Z);
00240                         angle.Y *= (f32)RADTODEG64;
00241 
00242                         if (angle.Y < 0.0f) angle.Y += 360.0f;
00243                         if (angle.Y >= 360.0f) angle.Y -= 360.0f;
00244 
00245                         f32 z1 = sqrtf(X*X + Z*Z);
00246 
00247                         angle.X = (T)atan2(z1, Y);
00248                         angle.X *= (f32)RADTODEG64;
00249                         angle.X -= 90.0f;
00250 
00251                         if (angle.X < 0.0f) angle.X += 360.0f;
00252                         if (angle.X >= 360.0f) angle.X -= 360.0f;
00253 
00254                         return angle;
00255                 }
As you can see, angle.X gets calculated, even though you're only using Y after that. BableOff does nearly the same as getHorizontalAngle(), but without the costly second atan2-calculation of X. (And an unnecessary sqrtf is there, too.)

Actually, I wouldn't mind this that much, but you were the one who insisted that your code is "Very efficient".
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

lol that's funny..

Well, we don't have anything against you, it's just nuances..

Oh and don't assume most people have 2.0GHz cpus :lol:

If I want to make my app on some little 300MHz computer?

And btw, untill last month I had a 6 years old computer as my main and you can guess what does that mean hardware-wise...

P.S
Good work on trying to help others ^^
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

Post by sudi »

Dunno if this is more efficent but it works:

Code: Select all

irr::scene::ISceneNode* node;      //u have to
irr::scene::ISceneNode* target;    //to init these
irr::core::vector3df vec = target->getAbsolutePosition()-node->getAbsolutePosition();
irr::core::vector3df dir(0,0,1);
vec.normalize();
irr::f32 r = acos(dir.dotProduct(vec))*180/3.14;
if(target->getAbsolutePosition().X < node->getAbsolutePosition().X)
    node->setRotation(irr::core::vector3df(0,-r,0));
else
    node->setRotation(irr::core::vector3df(0,r,0));
We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by renovation:tinkering,improving,planting flower beds.
iamtehpwn
Posts: 30
Joined: Sat Mar 31, 2007 6:11 pm

Post by iamtehpwn »

Well. Sorry. I just didn't want a big flame war to come out of me just trying to do something good :P

At any rate, The game I'm developing is targeted toward more powerful PC's, so excuse me if I'm not be "Extremely efficient". Point being, use it if you want to...It's simple, and it works. :)
Dorth
Posts: 931
Joined: Sat May 26, 2007 11:03 pm

Post by Dorth »

vector3df eneloc = enemy->getPosition(); // Enemy location

this line is completely useless, you do not use eneloc anywhere anymore. Also:

vector3df plyloc = player->getPosition(); // Player location
core::vector3df vect = plyloc - enemy->getAbsolutePosition();

you compare a relative position to an absolute position. Choose based on the logic of your World which should be used (although absolute make more sense) and then follow through. Also,
enemy->setRotation(vector3df(0, vect.Y, 0));
you are setting a relative position from an absolute one. You're better off calculating the difference of the 2 angles and then adding that to your current relative angle (so calculate with the 2 absolute, add diff to relative)

finally, what about the player? he could be oriented slightly off too. Of course you can use a second time the function to rotate the player but instead of doing twice the calculation, you're better off piling it in one func.
Post Reply