Raised This Month: $51 Target: $400
 12% 

Angle to Origin


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Sandurr
Senior Member
Join Date: Aug 2005
Old 01-06-2006 , 18:03   Angle to Origin
Reply With Quote #1

Hi, is it possible to set an NPCs angle so he looks to a given origin?
Sandurr is offline
PM
hello, i am pm
Join Date: Jan 2004
Location: Canalization
Old 01-06-2006 , 18:56  
Reply With Quote #2

Well, you have two origins:

1) the source_origin, ie. the current position of the NPC. If you don't have this, you can easily get it using entity_get_vector with EV_VEC_origin
2) the destination_origin, ie. where the NPC should be looking at

You can compute the vector which needs to be added to source_origin in order to obtain destination_origin:
Code:
dif_vector = source_origin - destination_origin;
This vector has the same direction as the NPC's wanted look vector. So you only need to normalise it then.

We can write a function called compute_look_vector, which computes the normalised look vector. It has three parameters: inSource, inDest, and outVec.
Code:
compute_look_vector(const Float:inSource[3], const Float:inDest[3], Float:outVec[3]) {    // First, set outVec to inSource - inDest    outVec[0] = inSource[0] - inDest[0];    outVec[1] = inSource[1] - inDest[1];    outVec[2] = inSource[2] - inDest[2];    // Now, normalise the vector    new Float:invLength = 1.0 / floatsqroot(outVec[0]*outVec[0] + outVec[1]*outVec[1] + outVec[2]*outVec[2]);    outVec[0] *= invLength;    outVec[1] *= invLength;    outVec[2] *= invLength; }

But I assume you want the three angles, as HL uses them: pitch/yaw/roll. I wanted to write up how to compute them but it's too late so you can simply use the vector_to_angle (from the engine module) function for today.

So here is our second version of the function, now with a different name, which computes the angles:

Code:
compute_look_angles(const Float:inSource[3], const Float:inDest[3], Float:outAngles[3]) {    // Note that we use outAngles as a temporary variable which stores the computed vector    // First, set outVec to inSource - inDest    outAngles[0] = inSource[0] - inDest[0];    outAngles[1] = inSource[1] - inDest[1];    outAngles[2] = inSource[2] - inDest[2];    // Now, normalise the vector    new Float:invLength = 1.0 / floatsqroot(outAngles[0]*outAngles[0] + outAngles[1]*outAngles[1] + outAngles[2]*outAngles[2]);    outAngles[0] *= invLength;    outAngles[1] *= invLength;    outAngles[2] *= invLength;    // Now, convert to angles    vec_to_angles(outAngles, outAngles); }

I hope I haven't made a mistake.
__________________
hello, i am pm
PM is offline
v3x
Veteran Member
Join Date: Oct 2004
Location: US
Old 01-07-2006 , 02:55  
Reply With Quote #3

So umm.. Example of usage plz? kthx
__________________
What am I doing these days? Well, I run my own Rust server. It's heavily modded. If you'd like to join, the ip is 167.114.101.67:28116

I also created a website called Rust Tools. It will calculate and tell you the raw amounts of resources needed to craft items.
v3x is offline
PM
hello, i am pm
Join Date: Jan 2004
Location: Canalization
Old 01-07-2006 , 03:47  
Reply With Quote #4

Well, that's a tough one because I misunderstood the question.

However I think that's possible as well.

We can use the function I posted to compute the angles. Then, we can set the NPC's yaw to the computed yaw value. We can safely ignore roll (because roll usually isn't associated with a looking-to computation and vec_to_angle should return 0 for it anyway). The problem is pitch - how can set the pitch of the head of the npc? I've looked at models from HL1 (barney, gman) and from cs 1.5 (hostage) and both only have controllers for setting the left-right rotation (yaw) of the head. So I'm afraid we can't set the pitch.

Anyway, so we can set the rotation of the head. If you want to use this instead of setting the model's yaw angle directly, play around with the EV_BYTE_controller1 entvar.
__________________
hello, i am pm
PM is offline
v3x
Veteran Member
Join Date: Oct 2004
Location: US
Old 01-07-2006 , 03:55  
Reply With Quote #5

Hmm.. What about an example of making one player look towards the origin of another player? Maybe I misunderstood something.
__________________
What am I doing these days? Well, I run my own Rust server. It's heavily modded. If you'd like to join, the ip is 167.114.101.67:28116

I also created a website called Rust Tools. It will calculate and tell you the raw amounts of resources needed to craft items.
v3x is offline
PM
hello, i am pm
Join Date: Jan 2004
Location: Canalization
Old 01-07-2006 , 04:37  
Reply With Quote #6

Yup, I indeed think you misunderstood it. I think he wants a player model to look there - created by something like create_entity.

Anyway, your example:
Code:
new Float:destVec[3]; // Set this accordingly new Float:srcVec[3]; new Float:angles[3]; entity_get_vector(id, EV_VEC_origin, srcVec); compute_look_angles(srcVec, destVec, angles); entity_set_vector(id, EV_VEC_angles, angles); entity_set_vector(id, EV_VEC_v_angle, angles); entity_set_int(id, EV_INT_fixangle, 1);

Or, uhh, that might work, I haven't tested it though. Also, I don't know whether v_angle is relative to the world coordinates or to the already set model angles. No idea XD test it.
__________________
hello, i am pm
PM is offline
Sandurr
Senior Member
Join Date: Aug 2005
Old 01-09-2006 , 03:35  
Reply With Quote #7

Well it doesn't work should the compute_look_angle return the OutAngle? And how?
Sandurr is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 01:35.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode