snake5 icon

sb3d turret rotation code

snake5 | PRO | 11/08/14 06:02:38 PM UTC | 0 ⭐ | 879 👁️ | Never ⏰ | []
JavaScript |

1.06 KB

|

None

|

0 👍

/

0 👎

function Turret.rotateTo( dir, dt )
{
    dir = dir.normalized;
    
    // do not let it rotate too quickly
    axis = vec3_cross( dir, this.direction ).normalized;
    angle = acos( min( 1, vec3_dot( dir, this.direction ) ) );
    maxmove = dt;
    if( angle > maxmove )
        dir = mat3().rotate_axis_angle_v3( axis, maxmove ).transform( this.direction ).normalized;
    
    // limit rotation
    axis = vec3_cross( dir, this.orig_direction ).normalized;
    angle = acos( min( 1, vec3_dot( dir, this.orig_direction ) ) );
    if( angle > this.maxangle )
        angle = this.maxangle;
    
    // rotate
    this.rotation = this.orig_rotation.normalized.rotate_axis_angle_v3( axis, angle ).normalized;
    this.direction = this.rotation.transform( vec3( 1, 0, 0 ) );
    this.mesh_inst.matrix = mat4().scale_v3( this.scale ).rotate_quat( this.rotation ).translate_v3( this.position );
    this.body.orientation = this.rotation.invert();
    
    lt = this.light;
    lt.direction = this.direction;
    lt.position = this.position + this.direction;
    lt.updir = this.rotation.transform( vec3(0,0,1) );
    lt.genSpotLightMatrices();
}

Comments