46 lines
1.4 KiB
JavaScript
46 lines
1.4 KiB
JavaScript
|
GameLib.D3.ComponentLookAt = function(
|
||
|
componentId,
|
||
|
targetEntity
|
||
|
) {
|
||
|
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);
|
||
|
|
||
|
//
|
||
|
this.targetEntity = targetEntity;
|
||
|
};
|
||
|
|
||
|
///////////////////////// 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;
|
||
|
}
|
||
|
};
|