Advertisement
snake5

sb3d turret rotation code

Nov 8th, 2014
537
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Turret.rotateTo( dir, dt )
  2. {
  3.     dir = dir.normalized;
  4.    
  5.     // do not let it rotate too quickly
  6.     axis = vec3_cross( dir, this.direction ).normalized;
  7.     angle = acos( min( 1, vec3_dot( dir, this.direction ) ) );
  8.     maxmove = dt;
  9.     if( angle > maxmove )
  10.         dir = mat3().rotate_axis_angle_v3( axis, maxmove ).transform( this.direction ).normalized;
  11.    
  12.     // limit rotation
  13.     axis = vec3_cross( dir, this.orig_direction ).normalized;
  14.     angle = acos( min( 1, vec3_dot( dir, this.orig_direction ) ) );
  15.     if( angle > this.maxangle )
  16.         angle = this.maxangle;
  17.    
  18.     // rotate
  19.     this.rotation = this.orig_rotation.normalized.rotate_axis_angle_v3( axis, angle ).normalized;
  20.     this.direction = this.rotation.transform( vec3( 1, 0, 0 ) );
  21.     this.mesh_inst.matrix = mat4().scale_v3( this.scale ).rotate_quat( this.rotation ).translate_v3( this.position );
  22.     this.body.orientation = this.rotation.invert();
  23.    
  24.     lt = this.light;
  25.     lt.direction = this.direction;
  26.     lt.position = this.position + this.direction;
  27.     lt.updir = this.rotation.transform( vec3(0,0,1) );
  28.     lt.genSpotLightMatrices();
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement