GameLib.D3.ComponentLookAt = function( componentId, targetEntity, targetOffset ) { this.componentId = componentId || GameLib.D3.Tools.RandomId(); this.parentEntity = null; // Todo: this should be executed somewhere in game-lib-z, so that we don't execute it on every construction of an object. GameLib.D3.Utils.Extend(GameLib.D3.ComponentLookAt, GameLib.D3.ComponentInterface); // todo: USE TARGET OFFSET. this.targetEntity = targetEntity; this.targetOffset = targetOffset || new GameLib.D3.Vector3(0, 0, 0); }; ///////////////////////// Methods to override ////////////////////////// GameLib.D3.ComponentLookAt.prototype.onUpdate = function( deltaTime, parentEntity ) { if(this.targetEntity) { var target = this.targetEntity.position; var lookAtMatrix = new THREE.Matrix4().lookAt( new THREE.Vector3( parentEntity.position.x, parentEntity.position.y, parentEntity.position.z ), new THREE.Vector3( target.x, target.y, target.z ), new THREE.Vector3( 0, 1, 0 ) ); var quaternion = new THREE.Quaternion().setFromRotationMatrix(lookAtMatrix); this.parentEntity.quaternion.x = quaternion.x; this.parentEntity.quaternion.y = quaternion.y; this.parentEntity.quaternion.z = quaternion.z; this.parentEntity.quaternion.w = quaternion.w; } };