home *** CD-ROM | disk | FTP | other *** search
- #ifndef lint
- static char sccsid[] = "@(#)control.c 1.1 92/05/28 SMI" ;
- /* from control.c 1.3 90/05/02 SMI */
- #endif
-
- /*
- * Copyright (c) 1986 by Sun Microsystems, Inc.
- */
-
- /*
- * this file contains generic control stuff:
- *
- * adjust_vector(object)
- * re-calculate objects "Pointing" vector from its "Delta"
- *
- * adjust_vectors(object)
- * updates an object's orientation according to its control inputs
- *
- * control_enemies()
- * goes through object list and updates everything according to
- * their last known control inputs
- *
- */
-
-
-
-
- #include <stdio.h>
- #include <math.h>
- #include "dstar.h"
- #include "object_types.h"
-
-
-
- extern int debug_level ;
-
-
- /****
- *
- * apply a delta to a vector
- *
- ****/
-
-
- adjust_vector(object, delta)
- register Object *object ;
- register Pt3d *delta ;
- {
- float magnitude ;
-
- /* transform Delta from ship coords to universal coords
- * matrix to go from ship coords to universe coords is:
- *
- * | Right |
- * | Up |
- * | Forward |
- *
- * (z-component of Delta is 1.0)
- */
-
- /* apply to forward vector */
-
- object->Pointing.x = delta->x*object->Right.x +
- delta->y*object->Up.x +
- object->Forward.x ;
- object->Pointing.y = delta->x*object->Right.y +
- delta->y*object->Up.y +
- object->Forward.y ;
- object->Pointing.z = delta->x*object->Right.z +
- delta->y*object->Up.z +
- object->Forward.z ;
-
- /* normalize the resulting vector */
-
- magnitude = object->Pointing.x*object->Pointing.x +
- object->Pointing.y*object->Pointing.y +
- object->Pointing.z*object->Pointing.z ;
- if(magnitude != 1.0)
- {
- magnitude = 1.0 / sqrt(magnitude) ;
- object->Pointing.x *= magnitude ;
- object->Pointing.y *= magnitude ;
- object->Pointing.z *= magnitude ;
- }
- }
-
-
-
- /****
- *
- * update object's position/velocity/"up" vectors
- *
- ****/
-
-
- adjust_vectors(object)
- register Object *object ;
- {
- Pt3d delta1, delta2, Forward2, Up2, Right2 ;
- float a ;
- float x,y ;
-
-
- delta1 = object->Delta ;
-
- delta1.x *= Dtime ;
- delta1.y *= Dtime ;
- delta1.z *= Dtime ;
-
-
- /* transform Delta from ship coords to universal coords */
- /* matrix to go from ship coords to universe coords is:
- *
- * | Right |
- * | Up |
- * | Forward |
- */
-
-
- /* (z-component of Delta is zero) */
-
- delta2.x = delta1.x*object->Right.x + delta1.y*object->Up.x ;
- delta2.y = delta1.x*object->Right.y + delta1.y*object->Up.y ;
- delta2.z = delta1.x*object->Right.z + delta1.y*object->Up.z ;
-
-
- /* apply to forward vector */
-
- Forward2.x = object->Forward.x + delta2.x ;
- Forward2.y = object->Forward.y + delta2.y ;
- Forward2.z = object->Forward.z + delta2.z ;
-
- a = Forward2.x*Forward2.x +
- Forward2.y*Forward2.y +
- Forward2.z*Forward2.z ;
-
- if(a != 1.0)
- {
- a = 1.0 / sqrt(a) ;
- Forward2.x *= a ;
- Forward2.y *= a ;
- Forward2.z *= a ;
- }
-
-
- /* transform Roll from ship coords to universal coords */
- /* (y,z components are zero */
-
- delta2.x = delta1.z * object->Right.x ;
- delta2.y = delta1.z * object->Right.y ;
- delta2.z = delta1.z * object->Right.z ;
-
- Up2.x = object->Up.x + delta2.x ;
- Up2.y = object->Up.y + delta2.y ;
- Up2.z = object->Up.z + delta2.z ;
-
-
-
- /* calculate new Right vector and normalize */
-
- Right2.x = Forward2.z*Up2.y - Forward2.y*Up2.z ;
- Right2.y = Forward2.x*Up2.z - Forward2.z*Up2.x ;
- Right2.z = Forward2.y*Up2.x - Forward2.x*Up2.y ;
-
- a = Right2.x*Right2.x + Right2.y*Right2.y + Right2.z*Right2.z ;
-
- if(a != 1.0)
- {
- a = 1.0 / sqrt(a) ;
- Right2.x *= a ;
- Right2.y *= a ;
- Right2.z *= a ;
- }
-
-
- /* go back and recalculate Up vector */
-
- Up2.x = Right2.z*Forward2.y - Right2.y*Forward2.z ;
- Up2.y = Right2.x*Forward2.z - Right2.z*Forward2.x ;
- Up2.z = Right2.y*Forward2.x - Right2.x*Forward2.y ;
-
- a = Up2.x*Up2.x + Up2.y*Up2.y + Up2.z*Up2.z ;
- if(a > 1.00001 || a < .9999)
- printf("whadda fuck? a=%f\n",a) ;
-
-
-
-
- /* update position */
-
- object->Posn.x += object->Speed*Dtime*Forward2.x ;
- object->Posn.y += object->Speed*Dtime*Forward2.y ;
- object->Posn.z += object->Speed*Dtime*Forward2.z ;
-
- object->Forward = Forward2 ;
- object->Up = Up2 ;
- object->Right = Right2 ;
- }
-
-
-
-
-
- control_object(object)
- Object *object ;
- {
- adjust_vector(object,&object->Delta) ;
- adjust_vectors(object) ;
- }
-
-
-
-
-
- control_enemies()
- {
- register int i ;
- register Object *object = &objects[0] ;
-
- /* adjust all active or dead, non-empty objects except
- for myself and my missiles (which are adjusted elsewhere).
- Do not adjust objects for which we've had a recent report. */
-
-
- for(i=MAX_PLAYERS; --i >= 0;)
- {
- if(object->class != OBJ_EMPTY &&
-
- (object->status == OBJ_ACTIVE || object->status == OBJ_DEAD) &&
-
- object != Me &&
- !(object->class == OBJ_MISSILE && object->team == Me->id) &&
-
- (object->last_rep.tv_sec < Last_Time.tv_sec ||
- (object->last_rep.tv_sec == Last_Time.tv_sec &&
- object->last_rep.tv_usec < Last_Time.tv_usec)) )
- {
- (*object->f_vector[F_CONTROL])(object) ;
- }
- ++object ;
- }
- }
-