Doom3world

The world is yours! Doom 3 - Quake 4 - ET:QW - Prey - Rage
It is currently Fri Jul 30, 2010 10:04 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Script to have mover always point at monster....
PostPosted: Sun Feb 05, 2006 9:34 pm 
Offline
Very Senior Forum Poster
User avatar

Joined: Sun May 29, 2005 10:02 pm
Posts: 5959
Location: Here, not there.
it's not perfect.. it can only look at monsters below it right now, in RARE instances the light goes wonkers for a fraction of a second (mostly with lost sould though... i belive because they are projecticles when they attack, not monsters), didn't clean out the code... :)

Here's a video of it in action:
http://www.sterlingshield.net/home/stev ... ht_QT6.mov

Code:
//////////////////////////////////////////////////////////
//                     //
//      ArcSin to Angle Convert         //
//                     //
//////////////////////////////////////////////////////////
/*this is used to take the arcsince you have & convert it to an angle.  use: arcsin(number, accuracy). 
number = the result of a sin or the opposide side of a triangle divided by the hypotonuse (from the SOHCAHTOA forumulas)
accuracy = how acurate you want the arcsin to be.  You can only use up to 4 decimal places.  For short distances (or lots of uses of this algrithum) use a higher number.
*/

float arcsin(float number, float accuracy)
{
   float x = 0;
   float y = 0;
   float temp;
   float temp2 = 0;
   float neg=0;
   if (number < 0)
    {
      neg = 1;
      number = number * (1 - 2);
    }
   
   while (y != 1)
    {
      temp = sys.sin(x);
      if ((number > temp) && (x <= 90))
       {
         x = x + accuracy;
       }
      else
       {
         y = 1;
         temp2 = x;
         if (neg == 1)
          {
            temp2 = temp2 * (1 - 2);
          }
       }
    }
   return temp2;
}

//////////////////////////////////////////////////////////
//                     //
//      Find Quad of Angle         //
//                     //
//////////////////////////////////////////////////////////
/*This is a util function to find the quadrant that an angle will reside in.
orig is the point that will rotate, dest is the target & view is the view you're calcing:
XY = 1 top
YZ = 2 side
XZ = 3 front
*/
   
float find_quad(vector orig, vector dest, float view)
{
   float temp = 0;
   float axis_1_a = 0;
   float axis_1_b = 0;
   float axis_2_a = 0;
   float axis_2_b = 0;

   if (view == 1)
    {
      axis_1_a = orig_y;
      axis_1_b = orig_z;
      axis_2_a = dest_y;
      axis_2_b = dest_z;
      if ((axis_2_a > axis_1_a) && (axis_2_b < axis_1_b))
       {
         temp = 1;
       }
      if ((axis_2_a > axis_1_a) && (axis_2_b > axis_1_b))
       {
         temp = 2;
       }
      if ((axis_2_a < axis_1_a) && (axis_2_b < axis_1_b))
       {
         temp = 3;
       }
      if ((axis_2_a < axis_1_a) && (axis_2_b < axis_1_b))
       {
         temp = 4;
       }
    }      
   if (view == 2)
    {
      axis_1_a = orig_x;
      axis_1_b = orig_z;
      axis_2_a = dest_x;
      axis_2_b = dest_z;
      if ((axis_2_a < axis_1_a) && (axis_2_b < axis_1_b))
       {
         temp = 1;
       }
      if ((axis_2_a < axis_1_a) && (axis_2_b > axis_1_b))
       {
         temp = 2;
       }
      if ((axis_2_a > axis_1_a) && (axis_2_b > axis_1_b))
       {
         temp = 3;
       }
      if ((axis_2_a > axis_1_a) && (axis_2_b < axis_1_b))
       {
         temp = 4;
       }
    }

   if (view == 3)
    {
      axis_1_a = orig_x;
      axis_1_b = orig_y;
      axis_2_a = dest_x;
      axis_2_b = dest_y;
      if ((axis_2_a < axis_1_a) && (axis_2_b < axis_1_b))
       {
         temp = 1;
       }
      if ((axis_2_a < axis_1_a) && (axis_2_b > axis_1_b))
       {
         temp = 2;
       }
      if ((axis_2_a > axis_1_a) && (axis_2_b > axis_1_b))
       {
         temp = 3;
       }
      if ((axis_2_a > axis_1_a) && (axis_2_b < axis_1_b))
       {
         temp = 4;
       }
    }

/*   sys.print("Quadrant dest lies in = ");
   sys.print(temp);
   sys.print(" View = ");
   sys.println(view);
*/
   return temp;
}

//////////////////////////////////////////////////////////
//                     //
//         Find Sin         //
//                     //
//////////////////////////////////////////////////////////
/*This is to find the sin # of the oppside side/hypotinuse of right triangle.
orig is the point that will rotate, dest is the target & quadrant is the general angle quadrant the target lies in:
quadrant 1 = 0 to 90
quadrant 2 = 90 to 180
quadrant 3 = 180 to 270
quadrant 4 = 270 to 360 (aka 0)
*/

float find_sin_num(entity orig, vector dest, float quadrant, float view)
{
   float temp = 0;
   vector orig_orig = orig.getWorldOrigin();
   float axis_1_a = 0;
   float axis_1_b = 0;
   float axis_2_a = 0;
   float axis_2_b = 0;

   if (view == 1)
    {
      axis_1_a = orig_orig_y;
      axis_1_b = orig_orig_z;
      axis_2_a = dest_y;
      axis_2_b = dest_z;
      if (quadrant == 1)
       {
         temp = (axis_2_a - axis_1_a) / sys.sqrt(((axis_2_a-axis_1_a)*(axis_2_a-axis_1_a))+((axis_2_b-axis_1_b)*(axis_2_b-axis_1_b)));
/*         sys.print(axis_2_a - axis_1_a);
         sys.print(" ");
         sys.print(sys.sqrt(((axis_2_a-axis_1_a)*(axis_2_a-axis_1_a))+((axis_2_b-axis_1_b)*(axis_2_b-axis_1_b))));
         sys.print(" ");
         sys.println(temp);
*/
       }
      if (quadrant == 2)
       {
         temp = (axis_2_b - axis_1_b) / sys.sqrt(((axis_2_a-axis_1_a)*(axis_2_a-axis_1_a))+((axis_1_b-axis_2_b)*(axis_1_b-axis_2_b)));
/*         sys.print(axis_2_b - axis_1_b);
         sys.print(" ");
         sys.print(sys.sqrt(((axis_2_a-axis_1_a)*(axis_2_a-axis_1_a))+((axis_1_b-axis_2_b)*(axis_1_b-axis_2_b))));
         sys.print(" ");
         sys.println(temp);
*/
       }
      if (quadrant == 3)
       {
         temp = (axis_1_a - axis_2_a) / sys.sqrt(((axis_1_a-axis_2_a)*(axis_1_a-axis_2_a))+((axis_2_b-axis_1_b)*(axis_2_b-axis_1_b)));
/*         sys.print(axis_1_a - axis_2_a);
         sys.print(" ");
         sys.print(sys.sqrt(((axis_1_a-axis_2_a)*(axis_1_a-axis_2_a))+((axis_2_b-axis_1_b)*(axis_2_b-axis_1_b))));
         sys.print(" ");
         sys.println(temp);
*/
       }
      if (quadrant == 4)
       {
         temp = (axis_1_b - axis_2_b) / sys.sqrt(((axis_1_a-axis_2_a)*(axis_1_a-axis_2_a))+((axis_1_b-axis_2_b)*(axis_1_b-axis_2_b)));
/*         sys.print(axis_1_b - axis_2_b);
         sys.print(" ");
         sys.print(sys.sqrt(((axis_1_a-axis_2_a)*(axis_1_a-axis_2_a))+((axis_1_b-axis_2_b)*(axis_1_b-axis_2_b))));
         sys.print(" ");
         sys.println(temp);
*/
       }


    }      
   if (view == 2)
    {
      axis_1_a = orig_orig_x;
      axis_1_b = orig_orig_z;
      axis_2_a = dest_x;
      axis_2_b = dest_z;
      if (quadrant == 1)
       {
         temp = (axis_1_a - axis_2_a) / sys.sqrt(((axis_2_a-axis_1_a)*(axis_2_a-axis_1_a))+((axis_2_b-axis_1_b)*(axis_2_b-axis_1_b)));
/*         sys.print(axis_1_a - axis_2_a);
         sys.print(" ");
         sys.print(sys.sqrt(((axis_2_a-axis_1_a)*(axis_2_a-axis_1_a))+((axis_2_b-axis_1_b)*(axis_2_b-axis_1_b))));
         sys.print(" ");
         sys.println(temp);
*/
       }
      if (quadrant == 2)
       {
         temp = (axis_2_b - axis_1_b) / sys.sqrt(((axis_2_a-axis_1_a)*(axis_2_a-axis_1_a))+((axis_1_b-axis_2_b)*(axis_1_b-axis_2_b)));
/*         sys.print(axis_2_b - axis_1_b);
         sys.print(" ");
         sys.print(sys.sqrt(((axis_2_a-axis_1_a)*(axis_2_a-axis_1_a))+((axis_1_b-axis_2_b)*(axis_1_b-axis_2_b))));
         sys.print(" ");
         sys.println(temp);
*/
       }
      if (quadrant == 3)
       {
         temp = (axis_2_a - axis_1_a) / sys.sqrt(((axis_1_a-axis_2_a)*(axis_1_a-axis_2_a))+((axis_2_b-axis_1_b)*(axis_2_b-axis_1_b)));
/*         sys.print(axis_2_a - axis_1_a);
         sys.print(" ");
         sys.print(sys.sqrt(((axis_1_a-axis_2_a)*(axis_1_a-axis_2_a))+((axis_2_b-axis_1_b)*(axis_2_b-axis_1_b))));
         sys.print(" ");
         sys.println(temp);
*/
       }
      if (quadrant == 4)
       {
         temp = (axis_1_b - axis_2_b) / sys.sqrt(((axis_1_a-axis_2_a)*(axis_1_a-axis_2_a))+((axis_1_b-axis_2_b)*(axis_1_b-axis_2_b)));
/*         sys.print(axis_1_b - axis_2_b);
         sys.print(" ");
         sys.print(sys.sqrt(((axis_1_a-axis_2_a)*(axis_1_a-axis_2_a))+((axis_1_b-axis_2_b)*(axis_1_b-axis_2_b))));
         sys.print(" ");
         sys.println(temp);
*/
       }



    }

   if (view == 3)
    {
      axis_1_a = orig_orig_x;
      axis_1_b = orig_orig_y;
      axis_2_a = dest_x;
      axis_2_b = dest_y;
      if (quadrant == 1)
       {
         temp = (axis_1_a - axis_2_a) / sys.sqrt(((axis_2_a-axis_1_a)*(axis_2_a-axis_1_a))+((axis_2_b-axis_1_b)*(axis_2_b-axis_1_b)));
/*         sys.print(axis_1_a - axis_2_a);
         sys.print(" ");
         sys.print(sys.sqrt(((axis_2_a-axis_1_a)*(axis_2_a-axis_1_a))+((axis_2_b-axis_1_b)*(axis_2_b-axis_1_b))));
         sys.print(" ");
         sys.println(temp);
*/
       }
      if (quadrant == 2)
       {
         temp = (axis_2_b - axis_1_b) / sys.sqrt(((axis_2_a-axis_1_a)*(axis_2_a-axis_1_a))+((axis_1_b-axis_2_b)*(axis_1_b-axis_2_b)));
/*         sys.print(axis_2_b - axis_1_b);
         sys.print(" ");
         sys.print(sys.sqrt(((axis_2_a-axis_1_a)*(axis_2_a-axis_1_a))+((axis_1_b-axis_2_b)*(axis_1_b-axis_2_b))));
         sys.print(" ");
         sys.println(temp);
*/
       }
      if (quadrant == 3)
       {
         temp = (axis_2_a - axis_1_a) / sys.sqrt(((axis_1_a-axis_2_a)*(axis_1_a-axis_2_a))+((axis_2_b-axis_1_b)*(axis_2_b-axis_1_b)));
/*         sys.print(axis_2_a - axis_1_a);
         sys.print(" ");
         sys.print(sys.sqrt(((axis_1_a-axis_2_a)*(axis_1_a-axis_2_a))+((axis_2_b-axis_1_b)*(axis_2_b-axis_1_b))));
         sys.print(" ");
         sys.println(temp);
*/
       }
      if (quadrant == 4)
       {
         temp = (axis_1_b - axis_2_b) / sys.sqrt(((axis_1_a-axis_2_a)*(axis_1_a-axis_2_a))+((axis_1_b-axis_2_b)*(axis_1_b-axis_2_b)));
/*         sys.print(axis_1_b - axis_2_b);
         sys.print(" ");
         sys.print(sys.sqrt(((axis_1_a-axis_2_a)*(axis_1_a-axis_2_a))+((axis_1_b-axis_2_b)*(axis_1_b-axis_2_b))));
         sys.print(" ");
         sys.println(temp);
*/
       }

    }

//   sys.print(" Sin of angle = ");
//   sys.println(temp);
/*   if (temp < 0)
    {
      temp = temp * (1-2);
    }*/
   return temp;

}

   
//////////////////////////////////////////////////////////
//                     //
//      Point at Point            //
//                     //
//////////////////////////////////////////////////////////
void pointatpoint(entity orig, entity dest)
{
   vector rotate_me_to = '0 0 0';
   float quad;
   float angle;
   orig.time(0.05);
//   while (1);
//    {
//      sys.print(orig.getWorldOrigin());
//      sys.print(" ");
//      sys.println(dest.getWorldOrigin());

      quad = find_quad(orig.getWorldOrigin(), dest.getWorldOrigin(), 1);
      angle = find_sin_num(orig, dest.getWorldOrigin(), quad, 1);
//      sys.println(angle);
      angle = arcsin(angle, 0.25);
      if (quad == 2)
       {
         angle = angle + 90;
       }
      if (quad == 3)
       {
         angle = angle + 180;
       }
      if (quad == 4)
       {
         angle = angle + 270;
       }
//      sys.print("Angle to rotate to on X = ");
//      sys.println(angle);
      rotate_me_to_z = angle;

      quad = find_quad(orig.getWorldOrigin(), dest.getWorldOrigin(), 2);
      angle = find_sin_num(orig, dest.getWorldOrigin(), quad, 2);
//      sys.println(angle);
      angle = arcsin(angle, 0.25);
      if (quad == 2)
       {
         angle = angle + 90;
       }
      if (quad == 3)
       {
         angle = angle + 180;
       }
      if (quad == 4)
       {
         angle = angle + 270;
       }
//      sys.print("Angle to rotate to on X = ");
//      sys.println(angle);
      rotate_me_to_x = angle;

/*      
      quad = find_quad(orig.getWorldOrigin(), dest.getWorldOrigin(), 3);
      angle = find_sin_num(orig, dest.getWorldOrigin(), quad, 3);
//      sys.println(angle);
      angle = arcsin(angle, 0.25);
      if (quad == 2)
       {
         angle = angle + 90;
       }
      if (quad == 3)
       {
         angle = angle + 180;
       }
      if (quad == 4)
       {
         angle = angle + 270;
       }
//      sys.print("Angle to rotate to on Y = ");
//      sys.println(angle);
      rotate_me_to_y = angle;
*/
      orig.rotateTo(rotate_me_to);
//      sys.wait(0.25);
//    }
}

_________________
Fuzzy Logic Inc
PAINTBALL DOOM 3!!!!
Check out my Q2 server! q2server.fuzzylogicinc.com :D
Doom 3, Paintball! d3server.fuzzylogicinc.com


Top
 Profile E-mail  
 
 Post subject: Re: Script to have mover always point at monster....
PostPosted: Sun Dec 02, 2007 11:43 am 
Offline
fired 300 rounds

Joined: Thu Jun 16, 2005 4:01 pm
Posts: 308
Hey, friar. I stumbled across this old post of yours, and found a way to shorten it WAY up. alot of redundant code in there. check out the shortened code:

Code:
/*******************************
here is the new, cleaned up code
*******************************/
float find_quad(vector orig, vector dest, float view)
{
   float axis_1_a = 0;
   float axis_1_b = 0;
   float axis_2_a = 0;
   float axis_2_b = 0;
   if (view == 1)
    {
      axis_1_a = orig_y;
      axis_1_b = orig_z;
      axis_2_a = dest_y;
      axis_2_b = dest_z;
    }     
   if (view == 2)
    {
      axis_1_a = orig_x;
      axis_1_b = orig_z;
      axis_2_a = dest_x;
      axis_2_b = dest_z;
    }
   if (view == 3)
    {
      axis_1_a = orig_x;
      axis_1_b = orig_y;
      axis_2_a = dest_x;
      axis_2_b = dest_y;
    }
      if ((axis_2_a < axis_1_a) && (axis_2_b < axis_1_b))
       {
         return 1;
       }
      if ((axis_2_a < axis_1_a) && (axis_2_b > axis_1_b))
       {
         return 2;
       }
      if ((axis_2_a > axis_1_a) && (axis_2_b > axis_1_b))
       {
         return 3;
       }
      if ((axis_2_a > axis_1_a) && (axis_2_b < axis_1_b))
       {
         return 4;
       }
}


much shorter (43 vs 80 lines), and easier to read

Here it is, but for your 3rd function.
originally 79 lines long, now only 56 lines long, excluding comments and spaces.
Code:
/*

      axis_1_a = orig_orig_y; //A
      axis_1_b = orig_orig_z;  //B
      axis_2_a = dest_y; //C
      axis_2_b = dest_z; //D
      /*
     
      Pattern:
            Denominators (per quad calc) are always the same, regardless of view
            views 2 & 3 have identical calculation per quadrant.  only difference is what A thru D get set to.
            view 1: quads 2 & 4 are identical calcs to view 2 & 3. 
            view 1: quads 1 & 3 are opposite calcs to view 2 & 3.
     
         structure:
            if view == x (1 2 or 3)
            {
               assign axis_X_Y
            } //end view assignments
            if quadrant == 1 or 3
               if view == 1
                  { reversed numerator compared to view 2 & 3 }
               else
               { assign temp }
            if quadrant == 2 or 4
            { assign temp }
     
     
      //quad1 (A-C) / sqrt( (C-A)^2 + (D-B)^2 ) //exception: view(1), numerator is flipped
      //quad2 (D-B) / sqrt( (C-A)^2 + (B-D)^2 )
      //quad3 (C-A) / sqrt( (A-C)^2 + (D-B)^2 ) //exception: view(1), numerator is flipped
      //quad4 (B-D) / sqrt( (A-C)^2 + (B-D)^2 )
      */


float find_sin_num(entity orig, vector dest, float quadrant, float view)
{
   vector orig_orig = orig.getWorldOrigin();
   float A, B, C, D;
   float numerator, denominator;
   if (view == 1)
    {
      A = orig_orig_y; //A
      B = orig_orig_z;  //B
      C = dest_y; //C
      D = dest_z; //D
    }
   if (view == 2)
    {
      A = orig_orig_x;
      B = orig_orig_z;
      C = dest_x;
      D = dest_z;
    }
   if (view == 3)
    {
      A = orig_orig_x;
      B = orig_orig_y;
      C = dest_x;
      D = dest_y;
    }
   if ( quadrant == 1)
    {
       if ( view == 1)
       {
          return (C - A) / sys.sqrt (((C - A)*(C - A)) + ((D - B)*(D - B)) ) ;
       }
       else
       {
          return (A - C) / sys.sqrt (((C - A)*(C - A)) + ((D - B)*(D - B)) ) ;
       }
    }
   if (quadrant == 2)
    {
       return (D - B) / sys.sqrt (((C - A)*(C - A)) + ((B - D)* (B - D)) );
    }
   if (quadrant == 3)
    {
       if ( view == 1)
       {
          return (A - C) / sys.sqrt ( ((A-C)*(A-C)) + ((B-D)*(B-D)) );
       }
       else
       {
          return (C - A) / sys.sqrt ( ((A-C)*(A-C)) + ((B-D)*(B-D)) );
       }
    }
   if (quadrant == 4)
    {
       return (B - D) / sys.sqrt (((A-C)*(A-C)) + ((B-D)*(B-D)) );
    }
}

IMHO, much easier to read, because you can follow the algorithm process visually much easier.

Last but not least, is the point at point function: it's a short cleanup, but an efficient one:

the following:
Code:
      angle = arcsin(angle, 0.25);
      if (quad == 2)
       {
         angle = angle + 90;
       }
      if (quad == 3)
       {
         angle = angle + 180;
       }
      if (quad == 4)
       {
         angle = angle + 270;
       }

can become:
Code:
      angle = arcsin(angle, 0.25);
   angle = angle + (( quad - 1 ) * 90);  // angle = angle + 90*0 or 1, 2 3, essentially: angle + 90, 180, 270.

effective and cuts ~36 lines out because you use those ~12 lines thrice in your function.

easier to read, too.

Anyway. I know it's ~2 years after the fact, but maybe some new coder will find it interesting to see how to optimize their code a bit, and make it easier to read.


Top
 Profile  
 
 Post subject: Re: Script to have mover always point at monster....
PostPosted: Sun Dec 02, 2007 1:13 pm 
Offline
Site Admin
User avatar

Joined: Tue May 28, 2002 6:17 pm
Posts: 10185
Location: Munich / Germany
chuckdood wrote:
Anyway. I know it's ~2 years after the fact, but maybe some new coder will find it interesting to see how to optimize their code a bit, and make it easier to read.


Interesting posts and optimizations never expire!

_________________
Image Staff - The world is yours, soon in 6 degrees of freedom!
Visit ModWiki


Top
 Profile  
 
 Post subject: Re: Script to have mover always point at monster....
PostPosted: Sun Dec 02, 2007 8:12 pm 
Offline
fired 300 rounds

Joined: Thu Jun 16, 2005 4:01 pm
Posts: 308
roger that, BNA!.


Top
 Profile  
 
 Post subject: Re: Script to have mover always point at monster....
PostPosted: Sun Dec 02, 2007 8:26 pm 
Offline
Very Senior Forum Poster
User avatar

Joined: Sun May 29, 2005 10:02 pm
Posts: 5959
Location: Here, not there.
what i did was a result of a lot of "oh you stupid son of a....." for many days. Please add to it, fix it, etc. It's a cool, useful little script. would love to see it used. :)

_________________
Fuzzy Logic Inc
PAINTBALL DOOM 3!!!!
Check out my Q2 server! q2server.fuzzylogicinc.com :D
Doom 3, Paintball! d3server.fuzzylogicinc.com


Top
 Profile E-mail  
 
 Post subject: Re: Script to have mover always point at monster....
PostPosted: Sun Dec 16, 2007 11:40 pm 
Offline
Orginal Programmer Wannabe
User avatar

Joined: Thu Jul 07, 2005 11:57 pm
Posts: 1229
Location: U.S & A
This is pretty cool stuff. This would be awesome for a background tree effect. Just put a transparent texture of a tree on a flat patch mesh and trun it into a mover to point at the player, like the halo game maps. :D

_________________
My Programming Blog


Top
 Profile E-mail  
 
 Post subject: Re: Script to have mover always point at monster....
PostPosted: Mon Dec 17, 2007 4:57 pm 
Offline
Very Senior Forum Poster
User avatar

Joined: Sun May 29, 2005 10:02 pm
Posts: 5959
Location: Here, not there.
use the keyword "sprite". does that. :D (but it has sorting issues with other textures, so this way MAY be better).

_________________
Fuzzy Logic Inc
PAINTBALL DOOM 3!!!!
Check out my Q2 server! q2server.fuzzylogicinc.com :D
Doom 3, Paintball! d3server.fuzzylogicinc.com


Top
 Profile E-mail  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Google [Bot] and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group