home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / alt / sources / 2608 / control.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-23  |  4.8 KB  |  243 lines

  1. #ifndef lint
  2. static    char    sccsid[] = "@(#)control.c 1.1 92/05/28 SMI" ;
  3.     /* from control.c 1.3 90/05/02 SMI */
  4. #endif
  5.  
  6. /*
  7.  * Copyright (c) 1986 by Sun Microsystems, Inc.
  8.  */
  9.  
  10. /*
  11.  * this file contains generic control stuff:
  12.  *
  13.  * adjust_vector(object)
  14.  *    re-calculate objects "Pointing" vector from its "Delta"
  15.  *
  16.  * adjust_vectors(object)
  17.  *    updates an object's orientation according to its control inputs
  18.  *
  19.  * control_enemies()
  20.  *    goes through object list and updates everything according to
  21.  *    their last known control inputs
  22.  *
  23.  */
  24.  
  25.  
  26.  
  27.  
  28. #include <stdio.h>
  29. #include <math.h>
  30. #include "dstar.h"
  31. #include "object_types.h"
  32.  
  33.  
  34.  
  35. extern    int    debug_level ;
  36.  
  37.  
  38. /****
  39.  *
  40.  * apply a delta to a vector
  41.  *
  42.  ****/
  43.  
  44.  
  45. adjust_vector(object, delta)
  46. register Object    *object ;
  47. register Pt3d    *delta ;
  48. {
  49.     float   magnitude ;
  50.  
  51. /* transform Delta from ship coords to universal coords
  52.  * matrix to go from ship coords to universe coords is:
  53.  *
  54.  * |  Right  |
  55.  * |   Up    |
  56.  * | Forward |
  57.  *
  58.  * (z-component of Delta is 1.0)
  59.  */
  60.  
  61.     /* apply to forward vector */
  62.  
  63.     object->Pointing.x = delta->x*object->Right.x +
  64.                  delta->y*object->Up.x +
  65.                  object->Forward.x ;
  66.     object->Pointing.y = delta->x*object->Right.y +
  67.                  delta->y*object->Up.y +
  68.                  object->Forward.y ;
  69.     object->Pointing.z = delta->x*object->Right.z +
  70.                  delta->y*object->Up.z +
  71.                  object->Forward.z ;
  72.  
  73.     /* normalize the resulting vector */
  74.  
  75.     magnitude = object->Pointing.x*object->Pointing.x +
  76.             object->Pointing.y*object->Pointing.y +
  77.             object->Pointing.z*object->Pointing.z ;
  78.     if(magnitude != 1.0)
  79.     {
  80.       magnitude = 1.0 / sqrt(magnitude) ;
  81.       object->Pointing.x *= magnitude ;
  82.       object->Pointing.y *= magnitude ;
  83.       object->Pointing.z *= magnitude ;
  84.     }
  85. }
  86.  
  87.  
  88.  
  89. /****
  90.  *
  91.  * update object's position/velocity/"up" vectors
  92.  *
  93.  ****/
  94.  
  95.  
  96. adjust_vectors(object)
  97. register Object    *object ;
  98. {
  99.     Pt3d    delta1, delta2, Forward2, Up2, Right2 ;
  100.     float    a ;
  101.     float    x,y ;
  102.  
  103.  
  104.     delta1 = object->Delta ;
  105.  
  106.     delta1.x *= Dtime ;
  107.     delta1.y *= Dtime ;
  108.     delta1.z *= Dtime ;
  109.  
  110.  
  111.     /* transform Delta from ship coords to universal coords */
  112.     /* matrix to go from ship coords to universe coords is:
  113.      *
  114.      * |  Right  |
  115.      * |   Up    |
  116.      * | Forward |
  117.      */
  118.  
  119.  
  120.     /* (z-component of Delta is zero) */
  121.  
  122.     delta2.x = delta1.x*object->Right.x + delta1.y*object->Up.x ;
  123.     delta2.y = delta1.x*object->Right.y + delta1.y*object->Up.y ;
  124.     delta2.z = delta1.x*object->Right.z + delta1.y*object->Up.z ;
  125.  
  126.  
  127.     /* apply to forward vector */
  128.  
  129.     Forward2.x = object->Forward.x + delta2.x ;
  130.     Forward2.y = object->Forward.y + delta2.y ;
  131.     Forward2.z = object->Forward.z + delta2.z ;
  132.  
  133.     a = Forward2.x*Forward2.x +
  134.         Forward2.y*Forward2.y + 
  135.         Forward2.z*Forward2.z ; 
  136.  
  137.     if(a != 1.0)
  138.     {
  139.       a = 1.0 / sqrt(a) ;
  140.       Forward2.x *= a ;
  141.       Forward2.y *= a ;
  142.       Forward2.z *= a ;
  143.     }
  144.  
  145.  
  146.     /* transform Roll from ship coords to universal coords */
  147.     /* (y,z components are zero */
  148.  
  149.     delta2.x = delta1.z * object->Right.x ;
  150.     delta2.y = delta1.z * object->Right.y ;
  151.     delta2.z = delta1.z * object->Right.z ;
  152.  
  153.     Up2.x = object->Up.x + delta2.x ;
  154.     Up2.y = object->Up.y + delta2.y ;
  155.     Up2.z = object->Up.z + delta2.z ;
  156.  
  157.  
  158.  
  159.     /* calculate new Right vector and normalize */
  160.  
  161.     Right2.x = Forward2.z*Up2.y - Forward2.y*Up2.z ; 
  162.     Right2.y = Forward2.x*Up2.z - Forward2.z*Up2.x ; 
  163.     Right2.z = Forward2.y*Up2.x - Forward2.x*Up2.y ; 
  164.  
  165.     a = Right2.x*Right2.x + Right2.y*Right2.y + Right2.z*Right2.z ; 
  166.  
  167.     if(a != 1.0)
  168.     {
  169.       a = 1.0 / sqrt(a) ;
  170.       Right2.x *= a ;
  171.       Right2.y *= a ;
  172.       Right2.z *= a ;
  173.     }
  174.  
  175.  
  176.     /* go back and recalculate Up vector */
  177.  
  178.     Up2.x = Right2.z*Forward2.y - Right2.y*Forward2.z ;
  179.     Up2.y = Right2.x*Forward2.z - Right2.z*Forward2.x ;
  180.     Up2.z = Right2.y*Forward2.x - Right2.x*Forward2.y ;
  181.  
  182.     a = Up2.x*Up2.x + Up2.y*Up2.y + Up2.z*Up2.z ; 
  183.     if(a > 1.00001  ||  a < .9999)
  184.       printf("whadda fuck? a=%f\n",a) ;
  185.  
  186.  
  187.  
  188.  
  189.     /* update position */
  190.  
  191.     object->Posn.x += object->Speed*Dtime*Forward2.x ;
  192.     object->Posn.y += object->Speed*Dtime*Forward2.y ;
  193.     object->Posn.z += object->Speed*Dtime*Forward2.z ;
  194.  
  195.     object->Forward = Forward2 ;
  196.     object->Up = Up2 ;
  197.     object->Right = Right2 ;
  198. }
  199.  
  200.  
  201.  
  202.  
  203.  
  204. control_object(object)
  205.     Object    *object ;
  206. {
  207.     adjust_vector(object,&object->Delta) ;
  208.     adjust_vectors(object) ;
  209. }
  210.  
  211.  
  212.  
  213.  
  214.  
  215. control_enemies()
  216. {
  217. register int    i ;
  218. register Object    *object = &objects[0] ;
  219.  
  220.     /* adjust all active or dead, non-empty objects except
  221.        for myself and my missiles (which are adjusted elsewhere).
  222.        Do not adjust objects for which we've had a recent report. */
  223.  
  224.  
  225.     for(i=MAX_PLAYERS; --i >= 0;)
  226.     {
  227.       if(object->class != OBJ_EMPTY  &&
  228.  
  229.         (object->status == OBJ_ACTIVE || object->status == OBJ_DEAD)  &&
  230.  
  231.          object != Me  &&
  232.          !(object->class == OBJ_MISSILE  &&  object->team == Me->id)  &&
  233.  
  234.         (object->last_rep.tv_sec < Last_Time.tv_sec  ||
  235.          (object->last_rep.tv_sec == Last_Time.tv_sec  &&
  236.           object->last_rep.tv_usec < Last_Time.tv_usec)) )
  237.       {
  238.         (*object->f_vector[F_CONTROL])(object) ;
  239.       }
  240.       ++object ;
  241.     }
  242. }
  243.