AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Angle to Origin (https://forums.alliedmods.net/showthread.php?t=22912)

Sandurr 01-06-2006 18:03

Angle to Origin
 
Hi, is it possible to set an NPCs angle so he looks to a given origin?

PM 01-06-2006 18:56

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.

v3x 01-07-2006 02:55

So umm.. Example of usage plz? kthx :)

PM 01-07-2006 03:47

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.

v3x 01-07-2006 03:55

Hmm.. What about an example of making one player look towards the origin of another player? Maybe I misunderstood something.

PM 01-07-2006 04:37

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.

Sandurr 01-09-2006 03:35

Well it doesn't work should the compute_look_angle return the OutAngle? And how?


All times are GMT -4. The time now is 15:45.

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