follow component target offset.
todo: - look at: use target offset. - camera component sphere-rigidbody.beta.r3js.org
parent
274ab21d3e
commit
9208ea9257
File diff suppressed because one or more lines are too long
|
@ -166,6 +166,28 @@ GameLib.D3.Color = function(r, g, b, a) {
|
||||||
this.b = b;
|
this.b = b;
|
||||||
this.a = a;
|
this.a = a;
|
||||||
};
|
};
|
||||||
|
GameLib.D3.ComponentCamera = function(
|
||||||
|
componentId,
|
||||||
|
threeCamera
|
||||||
|
) {
|
||||||
|
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.ComponentCamera, GameLib.D3.ComponentInterface);
|
||||||
|
|
||||||
|
// Component fields
|
||||||
|
this.threeCamera = threeCamera;
|
||||||
|
};
|
||||||
|
|
||||||
|
///////////////////////// Methods to override //////////////////////////
|
||||||
|
GameLib.D3.ComponentCamera.prototype.onLateUpdate = function(
|
||||||
|
deltaTime,
|
||||||
|
parentEntity
|
||||||
|
) {
|
||||||
|
this.threeCamera.quaternion.copy(parentEntity.quaternion);
|
||||||
|
this.threeCamera.position.copy(parentEntity.position);
|
||||||
|
};
|
||||||
GameLib.D3.ComponentColorFlash = function(
|
GameLib.D3.ComponentColorFlash = function(
|
||||||
componentId
|
componentId
|
||||||
) {
|
) {
|
||||||
|
@ -190,6 +212,248 @@ GameLib.D3.ComponentColorFlash.prototype.onSetParentEntity = function(
|
||||||
) {
|
) {
|
||||||
parentEntity.mesh.material = new THREE.MeshBasicMaterial();
|
parentEntity.mesh.material = new THREE.MeshBasicMaterial();
|
||||||
};
|
};
|
||||||
|
GameLib.D3.ComponentFlyControls = function(
|
||||||
|
componentId
|
||||||
|
) {
|
||||||
|
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.ComponentFlyControls, GameLib.D3.ComponentInterface);
|
||||||
|
|
||||||
|
// Component fields
|
||||||
|
this.pitch = 0;
|
||||||
|
this.yaw = 0;
|
||||||
|
this.canRotate = false;
|
||||||
|
this.moveSpeed = 22.2;
|
||||||
|
this.moveForward = false;
|
||||||
|
this.moveBackward = false;
|
||||||
|
this.moveLeft = false;
|
||||||
|
this.moveRight = false;
|
||||||
|
this.moveUp = false;
|
||||||
|
this.moveDown = false;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
///////////////////////// Methods to override //////////////////////////
|
||||||
|
GameLib.D3.ComponentFlyControls.prototype.onUpdate = function(
|
||||||
|
deltaTime,
|
||||||
|
parentEntity
|
||||||
|
) {
|
||||||
|
// Apply rotation
|
||||||
|
var rotation = new THREE.Euler(
|
||||||
|
this.pitch,
|
||||||
|
this.yaw,
|
||||||
|
0,
|
||||||
|
"YXZ"
|
||||||
|
);
|
||||||
|
|
||||||
|
var quat = new THREE.Quaternion().setFromEuler(
|
||||||
|
rotation
|
||||||
|
);
|
||||||
|
|
||||||
|
parentEntity.quaternion.x = quat.x;
|
||||||
|
parentEntity.quaternion.y = quat.y;
|
||||||
|
parentEntity.quaternion.z = quat.z;
|
||||||
|
parentEntity.quaternion.w = quat.w;
|
||||||
|
|
||||||
|
// Apply translation
|
||||||
|
var direction = new THREE.Vector3(0, 0, -1);
|
||||||
|
direction = direction.applyEuler(rotation).normalize();
|
||||||
|
|
||||||
|
if(this.moveForward) {
|
||||||
|
|
||||||
|
parentEntity.position.x += direction.x * (deltaTime * this.moveSpeed);
|
||||||
|
parentEntity.position.y += direction.y * (deltaTime * this.moveSpeed);
|
||||||
|
parentEntity.position.z += direction.z * (deltaTime * this.moveSpeed);
|
||||||
|
|
||||||
|
} else if(this.moveBackward) {
|
||||||
|
|
||||||
|
parentEntity.position.x -= direction.x * (deltaTime * this.moveSpeed);
|
||||||
|
parentEntity.position.y -= direction.y * (deltaTime * this.moveSpeed);
|
||||||
|
parentEntity.position.z -= direction.z * (deltaTime * this.moveSpeed);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if(this.moveLeft) {
|
||||||
|
|
||||||
|
var right = direction.cross(new THREE.Vector3(0, 1, 0));
|
||||||
|
parentEntity.position.x -= right.x * (deltaTime * this.moveSpeed);
|
||||||
|
parentEntity.position.y -= right.y * (deltaTime * this.moveSpeed);
|
||||||
|
parentEntity.position.z -= right.z * (deltaTime * this.moveSpeed);
|
||||||
|
|
||||||
|
} else if(this.moveRight) {
|
||||||
|
|
||||||
|
var right = direction.cross(new THREE.Vector3(0, 1, 0));
|
||||||
|
parentEntity.position.x += right.x * (deltaTime * this.moveSpeed);
|
||||||
|
parentEntity.position.y += right.y * (deltaTime * this.moveSpeed);
|
||||||
|
parentEntity.position.z += right.z * (deltaTime * this.moveSpeed);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Absolute Y-Axis
|
||||||
|
if(this.moveUp) {
|
||||||
|
|
||||||
|
parentEntity.position.y += (deltaTime * this.moveSpeed);
|
||||||
|
|
||||||
|
} else if(this.moveDown) {
|
||||||
|
|
||||||
|
parentEntity.position.y -= (deltaTime * this.moveSpeed);
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
GameLib.D3.ComponentFlyControls.prototype.onSetParentEntity = function(
|
||||||
|
parentScene,
|
||||||
|
parentEntity
|
||||||
|
) {
|
||||||
|
var component = this;
|
||||||
|
|
||||||
|
// Lock cursor
|
||||||
|
var havePointerLock = 'pointerLockElement' in document || 'mozPointerLockElement' in document || 'webkitPointerLockElement' in document;
|
||||||
|
var element = document.body;
|
||||||
|
|
||||||
|
document.addEventListener('click', function(event) {
|
||||||
|
if(havePointerLock) {
|
||||||
|
if(event.button == 0) {
|
||||||
|
component.canRotate = true;
|
||||||
|
element.requestPointerLock();
|
||||||
|
} else if(event.button == 2) {
|
||||||
|
component.canRotate = false;
|
||||||
|
document.exitPointerLock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if(havePointerLock) {
|
||||||
|
element.requestPointerLock = element.requestPointerLock || element.mozRequestPointerLock || element.webkitRequestPointerLock;
|
||||||
|
document.exitPointerLock = document.exitPointerLock || document.mozExitPointerLock || document.webkitExitPointerLock;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Mouse move
|
||||||
|
document.addEventListener('mousemove', function (event) {
|
||||||
|
if(component.canRotate) {
|
||||||
|
var movementX = event.movementX || event.mozMovementX || event.webkitMovementX || 0;
|
||||||
|
var movementY = event.movementY || event.mozMovementY || event.webkitMovementY || 0;
|
||||||
|
|
||||||
|
component.yaw -= movementX * 0.002;
|
||||||
|
component.pitch -= movementY * 0.002;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save mouseCoords
|
||||||
|
sys.mouseCoords.set(
|
||||||
|
(event.clientX / window.innerWidth) * 2 - 1,
|
||||||
|
-(event.clientY / window.innerHeight) * 2 + 1
|
||||||
|
);
|
||||||
|
|
||||||
|
}, false);
|
||||||
|
|
||||||
|
|
||||||
|
// Hook up keyboard controls
|
||||||
|
document.addEventListener('keydown', function (event) {
|
||||||
|
|
||||||
|
switch (event.keyCode) {
|
||||||
|
case 87: // w
|
||||||
|
component.moveForward = true;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 65: // a
|
||||||
|
component.moveLeft = true;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 83: // s
|
||||||
|
component.moveBackward = true;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 68: // d
|
||||||
|
component.moveRight = true;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 32: // space
|
||||||
|
component.moveUp = true;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 16:
|
||||||
|
component.moveDown = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}, false);
|
||||||
|
|
||||||
|
document.addEventListener('keyup', function (event) {
|
||||||
|
switch (event.keyCode) {
|
||||||
|
case 38: // up
|
||||||
|
case 87: // w
|
||||||
|
component.moveForward = false;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 37: // left
|
||||||
|
case 65: // a
|
||||||
|
component.moveLeft = false;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 40: // down
|
||||||
|
case 83: // s
|
||||||
|
component.moveBackward = false;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 39: // right
|
||||||
|
case 68: // d
|
||||||
|
component.moveRight = false;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 32: // space
|
||||||
|
component.moveUp = false;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 16:
|
||||||
|
component.moveDown = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}, false);
|
||||||
|
|
||||||
|
};
|
||||||
|
GameLib.D3.ComponentFollow = function(
|
||||||
|
componentId,
|
||||||
|
targetEntity,
|
||||||
|
targetOffset,
|
||||||
|
minDistance
|
||||||
|
) {
|
||||||
|
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.ComponentFollow, GameLib.D3.ComponentInterface);
|
||||||
|
|
||||||
|
//
|
||||||
|
this.targetEntity = targetEntity;
|
||||||
|
this.moveSpeed = 2.5;
|
||||||
|
|
||||||
|
|
||||||
|
if(GameLib.D3.Utils.UndefinedOrNull(targetOffset)) {
|
||||||
|
targetOffset = new GameLib.D3.Vector3(0, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.targetOffset = targetOffset;
|
||||||
|
this.minDistance = minDistance || 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
///////////////////////// Methods to override //////////////////////////
|
||||||
|
GameLib.D3.ComponentFollow.prototype.onUpdate = function(
|
||||||
|
deltaTime,
|
||||||
|
parentEntity
|
||||||
|
) {
|
||||||
|
if(this.targetEntity) {
|
||||||
|
var target = new THREE.Vector3().copy(this.targetEntity.position);
|
||||||
|
target.x += this.targetOffset.x;
|
||||||
|
target.y += this.targetOffset.y;
|
||||||
|
target.z += this.targetOffset.z;
|
||||||
|
|
||||||
|
if(target.distanceTo(parentEntity.position) > this.minDistance) {
|
||||||
|
parentEntity.position = parentEntity.position.lerp(target, this.moveSpeed * deltaTime);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
GameLib.D3.ComponentInterface = function(
|
GameLib.D3.ComponentInterface = function(
|
||||||
componentId
|
componentId
|
||||||
) {
|
) {
|
||||||
|
@ -257,6 +521,196 @@ GameLib.D3.ComponentInterface.prototype.onSetParentEntity = function(
|
||||||
parentEntity
|
parentEntity
|
||||||
) {
|
) {
|
||||||
|
|
||||||
|
};
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
GameLib.D3.ComponentMeshPermutation = function(
|
||||||
|
componentId,
|
||||||
|
positionOffset,
|
||||||
|
quaternionOffset,
|
||||||
|
scaleOffset
|
||||||
|
) {
|
||||||
|
this.componentId = componentId || GameLib.D3.Tools.RandomId();
|
||||||
|
this.parentEntity = null;
|
||||||
|
|
||||||
|
this.positionOffset = positionOffset || new GameLib.D3.Vector3(0, 0, 0);
|
||||||
|
this.quaternionOffset = quaternionOffset || new GameLib.D3.Quaternion(0, 0, 0, 1);
|
||||||
|
this.scaleOffset = scaleOffset || new GameLib.D3.Vector3(0, 0, 0);
|
||||||
|
|
||||||
|
// 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.ComponentMeshPermutation, GameLib.D3.ComponentInterface);
|
||||||
|
};
|
||||||
|
|
||||||
|
///////////////////////// Methods to override //////////////////////////
|
||||||
|
GameLib.D3.ComponentMeshPermutation.prototype.onLateUpdate = function(
|
||||||
|
deltaTime,
|
||||||
|
parentEntity
|
||||||
|
) {
|
||||||
|
|
||||||
|
if(parentEntity && parentEntity.mesh) {
|
||||||
|
var quaternion = new THREE.Quaternion();
|
||||||
|
quaternion.copy(parentEntity.mesh.quaternion);
|
||||||
|
|
||||||
|
var quaternionCopy = quaternion.clone();
|
||||||
|
|
||||||
|
var position = new THREE.Vector3();
|
||||||
|
position.copy(parentEntity.mesh.position);
|
||||||
|
|
||||||
|
var offsetQuaternion = new THREE.Quaternion();
|
||||||
|
offsetQuaternion.copy(this.quaternionOffset);
|
||||||
|
quaternion = quaternion.multiply(offsetQuaternion).normalize();
|
||||||
|
|
||||||
|
var offsetPosition = new THREE.Vector3();
|
||||||
|
offsetPosition.copy(this.positionOffset);
|
||||||
|
position = position.add(offsetPosition.applyQuaternion(quaternionCopy));
|
||||||
|
|
||||||
|
var scale = new THREE.Vector3();
|
||||||
|
scale.copy(parentEntity.mesh.scale);
|
||||||
|
|
||||||
|
var scaleOffset = new THREE.Vector3();
|
||||||
|
scaleOffset.copy(this.scaleOffset);
|
||||||
|
scale = scale.add(scaleOffset);
|
||||||
|
|
||||||
|
parentEntity.mesh.position.copy(position);
|
||||||
|
parentEntity.mesh.quaternion.copy(quaternion);
|
||||||
|
parentEntity.mesh.scale.copy(scale);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
GameLib.D3.ComponentRaycastVehicleControls = function(
|
||||||
|
componentId,
|
||||||
|
frontLWheelIndex,
|
||||||
|
frontRWheelIndex,
|
||||||
|
backLWheelIndex,
|
||||||
|
backRWheelIndex,
|
||||||
|
maxForce,
|
||||||
|
steering
|
||||||
|
) {
|
||||||
|
this.componentId = componentId || GameLib.D3.Tools.RandomId();
|
||||||
|
this.parentEntity = null;
|
||||||
|
|
||||||
|
// maybe we pass this in the constructor
|
||||||
|
this.raycastVehicleComponent = null;
|
||||||
|
this.frontLWheelIndex = frontLWheelIndex || 0;
|
||||||
|
this.frontRWheelIndex = frontRWheelIndex || 1;
|
||||||
|
this.backLWheelIndex = backLWheelIndex || 2;
|
||||||
|
this.backRWheelIndex = backRWheelIndex || 3;
|
||||||
|
|
||||||
|
this.maxForce = maxForce || 400;
|
||||||
|
this.steering = steering || 0.5;
|
||||||
|
|
||||||
|
// 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.ComponentRaycastVehicleControls, GameLib.D3.ComponentInterface);
|
||||||
|
};
|
||||||
|
|
||||||
|
///////////////////////// Methods to override //////////////////////////
|
||||||
|
GameLib.D3.ComponentRaycastVehicleControls.prototype.onSetParentEntity = function(
|
||||||
|
parentScene,
|
||||||
|
parentEntity
|
||||||
|
) {
|
||||||
|
|
||||||
|
console.log("Set parent!");
|
||||||
|
|
||||||
|
this.raycastVehicleComponent = parentEntity.getComponent(GameLib.D3.RaycastVehicle);
|
||||||
|
if(!this.raycastVehicleComponent) {
|
||||||
|
console.warn("NO RAYCAST VEHICLE FOUND!");
|
||||||
|
} else {
|
||||||
|
|
||||||
|
var component = this;
|
||||||
|
document.addEventListener('keydown', function(event) {
|
||||||
|
|
||||||
|
if (event.keyCode == 73) { // Forward [i]
|
||||||
|
|
||||||
|
component.raycastVehicleComponent.instance.applyEngineForce(-component.maxForce, component.backLWheelIndex);
|
||||||
|
component.raycastVehicleComponent.instance.applyEngineForce(-component.maxForce, component.backRWheelIndex);
|
||||||
|
|
||||||
|
} else if (event.keyCode == 74) { // Left [j]
|
||||||
|
|
||||||
|
component.raycastVehicleComponent.instance.setSteeringValue(component.steering, component.frontLWheelIndex);
|
||||||
|
component.raycastVehicleComponent.instance.setSteeringValue(component.steering, component.frontRWheelIndex);
|
||||||
|
|
||||||
|
} else if (event.keyCode == 75) { // Back [k]
|
||||||
|
|
||||||
|
component.raycastVehicleComponent.instance.applyEngineForce(component.maxForce, component.backLWheelIndex);
|
||||||
|
component.raycastVehicleComponent.instance.applyEngineForce(component.maxForce, component.backRWheelIndex);
|
||||||
|
|
||||||
|
} else if (event.keyCode == 76) { // Right [l]
|
||||||
|
component.raycastVehicleComponent.instance.setSteeringValue(-component.steering, component.frontLWheelIndex);
|
||||||
|
component.raycastVehicleComponent.instance.setSteeringValue(-component.steering, component.frontRWheelIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
}, false);
|
||||||
|
|
||||||
|
document.addEventListener('keyup', function(event) {
|
||||||
|
|
||||||
|
if (event.keyCode == 73) { // Forward [i]
|
||||||
|
|
||||||
|
component.raycastVehicleComponent.instance.applyEngineForce(0, component.backLWheelIndex);
|
||||||
|
component.raycastVehicleComponent.instance.applyEngineForce(0, component.backRWheelIndex);
|
||||||
|
|
||||||
|
} else if (event.keyCode == 74) { // Left [j]
|
||||||
|
|
||||||
|
component.raycastVehicleComponent.instance.setSteeringValue(0, component.frontLWheelIndex);
|
||||||
|
component.raycastVehicleComponent.instance.setSteeringValue(0, component.frontRWheelIndex);
|
||||||
|
|
||||||
|
} else if (event.keyCode == 75) { // Back [k]
|
||||||
|
|
||||||
|
component.raycastVehicleComponent.instance.applyEngineForce(0, component.backLWheelIndex);
|
||||||
|
component.raycastVehicleComponent.instance.applyEngineForce(0, component.backRWheelIndex);
|
||||||
|
|
||||||
|
} else if (event.keyCode == 76) { // Right [l]
|
||||||
|
component.raycastVehicleComponent.instance.setSteeringValue(0, component.frontLWheelIndex);
|
||||||
|
component.raycastVehicleComponent.instance.setSteeringValue(0, component.frontRWheelIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
}, false);
|
||||||
|
|
||||||
|
}
|
||||||
};
|
};
|
||||||
/**
|
/**
|
||||||
* Engine Superset
|
* Engine Superset
|
||||||
|
@ -315,7 +769,10 @@ GameLib.D3.Engine.ENGINE_TYPE_AMMO = 0x2;
|
||||||
GameLib.D3.Engine.ENGINE_TYPE_GOBLIN = 0x3;
|
GameLib.D3.Engine.ENGINE_TYPE_GOBLIN = 0x3;
|
||||||
GameLib.D3.Entity = function(
|
GameLib.D3.Entity = function(
|
||||||
meshId,
|
meshId,
|
||||||
componentIds
|
componentIds,
|
||||||
|
position,
|
||||||
|
quaternion,
|
||||||
|
scale
|
||||||
) {
|
) {
|
||||||
this.meshId = meshId;
|
this.meshId = meshId;
|
||||||
|
|
||||||
|
@ -325,8 +782,24 @@ GameLib.D3.Entity = function(
|
||||||
|
|
||||||
this.componentIds = componentIds;
|
this.componentIds = componentIds;
|
||||||
|
|
||||||
|
// constructed at runtime
|
||||||
this.parentScene = null;
|
this.parentScene = null;
|
||||||
this.mesh = null;
|
this.mesh = null;
|
||||||
|
|
||||||
|
if(GameLib.D3.Utils.UndefinedOrNull(position)) {
|
||||||
|
position = new GameLib.D3.Vector3(0, 0, 0);
|
||||||
|
}
|
||||||
|
this.position = position;
|
||||||
|
|
||||||
|
if(GameLib.D3.Utils.UndefinedOrNull(quaternion)) {
|
||||||
|
quaternion = new GameLib.D3.Vector4(0, 0, 0, 1);
|
||||||
|
}
|
||||||
|
this.quaternion = quaternion;
|
||||||
|
|
||||||
|
if(GameLib.D3.Utils.UndefinedOrNull(scale)) {
|
||||||
|
scale = new GameLib.D3.Vector3(1, 1, 1);
|
||||||
|
}
|
||||||
|
this.scale = scale;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -344,6 +817,12 @@ GameLib.D3.Entity.prototype.update = function(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(this.mesh) {
|
||||||
|
this.mesh.position.set(this.position.x, this.position.y, this.position.z);
|
||||||
|
this.mesh.quaternion.set(this.quaternion.x, this.quaternion.y, this.quaternion.z, this.quaternion.w);
|
||||||
|
this.mesh.scale.set(this.scale.x, this.scale.y, this.scale.z);
|
||||||
|
}
|
||||||
|
|
||||||
this.onUpdate(deltaTime);
|
this.onUpdate(deltaTime);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -375,7 +854,7 @@ GameLib.D3.Entity.prototype.register = function(
|
||||||
this.parentScene = parentScene;
|
this.parentScene = parentScene;
|
||||||
|
|
||||||
if(this.meshId != null && parentScene.meshIdToMesh[this.meshId]) {
|
if(this.meshId != null && parentScene.meshIdToMesh[this.meshId]) {
|
||||||
parentScene.threeScene.add(parentScene.meshIdToMesh[this.meshId]);
|
parentScene.instance.add(parentScene.meshIdToMesh[this.meshId]);
|
||||||
this.mesh = parentScene.meshIdToMesh[this.meshId];
|
this.mesh = parentScene.meshIdToMesh[this.meshId];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -409,6 +888,20 @@ GameLib.D3.Entity.prototype.addComponent = function(
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
GameLib.D3.Entity.prototype.getComponent = function(
|
||||||
|
componentType
|
||||||
|
) {
|
||||||
|
for(var c in this.componentIds) {
|
||||||
|
var componentId = this.componentIds[c];
|
||||||
|
var component = this.parentScene.componentIdToComponent[componentId];
|
||||||
|
if (component instanceof componentType) {
|
||||||
|
return component;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
|
||||||
///////////////////////// Methods to override //////////////////////////
|
///////////////////////// Methods to override //////////////////////////
|
||||||
|
|
||||||
GameLib.D3.Entity.prototype.onUpdate = function(
|
GameLib.D3.Entity.prototype.onUpdate = function(
|
||||||
|
@ -679,14 +1172,14 @@ GameLib.D3.Game.prototype.addScene = function(
|
||||||
};
|
};
|
||||||
|
|
||||||
GameLib.D3.Game.prototype.processPhysics = function (
|
GameLib.D3.Game.prototype.processPhysics = function (
|
||||||
timestep
|
dt
|
||||||
) {
|
) {
|
||||||
for(var s in this.scenes) {
|
for(var s in this.scenes) {
|
||||||
var scene = this.scenes[s];
|
var scene = this.scenes[s];
|
||||||
|
|
||||||
for(var w in scene.worlds) {
|
for(var w in scene.worlds) {
|
||||||
var world = scene.worlds[w];
|
var world = scene.worlds[w];
|
||||||
world.step(timestep);
|
world.step(dt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -703,14 +1196,15 @@ GameLib.D3.Game.prototype.render = function(
|
||||||
};
|
};
|
||||||
|
|
||||||
GameLib.D3.Game.prototype.update = function(
|
GameLib.D3.Game.prototype.update = function(
|
||||||
dt
|
dt,
|
||||||
|
fixedDt
|
||||||
) {
|
) {
|
||||||
for(var s in this.scenes) {
|
for(var s in this.scenes) {
|
||||||
var scene = this.scenes[s];
|
var scene = this.scenes[s];
|
||||||
|
|
||||||
for(var w in scene.worlds) {
|
for(var w in scene.worlds) {
|
||||||
var world = scene.worlds[w];
|
var world = scene.worlds[w];
|
||||||
world.step(dt);
|
world.step(fixedDt);
|
||||||
}
|
}
|
||||||
|
|
||||||
scene.update(dt);
|
scene.update(dt);
|
||||||
|
@ -2565,7 +3059,8 @@ GameLib.D3.PolyVertex.prototype.clone = function() {
|
||||||
GameLib.D3.RaycastVehicle = function(
|
GameLib.D3.RaycastVehicle = function(
|
||||||
engine,
|
engine,
|
||||||
chassisBody,
|
chassisBody,
|
||||||
wheels
|
wheels,
|
||||||
|
wheelBodies
|
||||||
) {
|
) {
|
||||||
this.engine = engine;
|
this.engine = engine;
|
||||||
this.engine.isNotCannonThrow();
|
this.engine.isNotCannonThrow();
|
||||||
|
@ -2579,7 +3074,14 @@ GameLib.D3.RaycastVehicle = function(
|
||||||
}
|
}
|
||||||
this.wheels = wheels;
|
this.wheels = wheels;
|
||||||
|
|
||||||
|
if(GameLib.D3.Utils.UndefinedOrNull(wheelBodies)) {
|
||||||
|
wheelBodies = [];
|
||||||
|
}
|
||||||
|
this.wheelBodies = wheelBodies;
|
||||||
|
|
||||||
this.instance = this.createInstance();
|
this.instance = this.createInstance();
|
||||||
|
|
||||||
|
GameLib.D3.Utils.Extend(GameLib.D3.RaycastVehicle, GameLib.D3.ComponentInterface);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -2595,12 +3097,15 @@ GameLib.D3.RaycastVehicle.prototype.createInstance = function() {
|
||||||
/**
|
/**
|
||||||
* Adds a raycast wheel to this vehicle
|
* Adds a raycast wheel to this vehicle
|
||||||
* @param wheel GameLib.D3.RaycastWheel
|
* @param wheel GameLib.D3.RaycastWheel
|
||||||
|
* @param wheelRigidBody GameLib.D3.RigidBody
|
||||||
*/
|
*/
|
||||||
GameLib.D3.RaycastVehicle.prototype.addWheel = function (
|
GameLib.D3.RaycastVehicle.prototype.addWheel = function (
|
||||||
wheel
|
wheel,
|
||||||
|
wheelRigidBody
|
||||||
) {
|
) {
|
||||||
this.wheels.push(wheel);
|
this.wheels.push(wheel);
|
||||||
this.instance.addWheel(wheel.instance);
|
this.wheelBodies.push(wheelRigidBody);
|
||||||
|
wheel.wheelIndex = this.instance.addWheel(wheel.instance);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -2611,6 +3116,28 @@ GameLib.D3.RaycastVehicle.prototype.addWheel = function (
|
||||||
GameLib.D3.RaycastVehicle.prototype.getWheelInfo = function() {
|
GameLib.D3.RaycastVehicle.prototype.getWheelInfo = function() {
|
||||||
return this.instance.wheelInfos;
|
return this.instance.wheelInfos;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Override component methods //
|
||||||
|
GameLib.D3.RaycastVehicle.prototype.onUpdate = function(
|
||||||
|
deltaTime,
|
||||||
|
parentEntity
|
||||||
|
) {
|
||||||
|
for (var i = 0; i < this.getWheelInfo().length; i++) {
|
||||||
|
this.instance.updateWheelTransform(i);
|
||||||
|
var t = this.getWheelInfo()[i].worldTransform;
|
||||||
|
var wheelBody = this.wheelBodies[i].instance;
|
||||||
|
wheelBody.position.copy(t.position);
|
||||||
|
wheelBody.quaternion.copy(t.quaternion);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
GameLib.D3.RaycastVehicle.prototype.onRegistered = function(
|
||||||
|
parentScene
|
||||||
|
) {
|
||||||
|
|
||||||
|
};
|
||||||
GameLib.D3.RaycastWheel = function(
|
GameLib.D3.RaycastWheel = function(
|
||||||
engine,
|
engine,
|
||||||
chassisConnectionPointLocal,
|
chassisConnectionPointLocal,
|
||||||
|
@ -2631,7 +3158,6 @@ GameLib.D3.RaycastWheel = function(
|
||||||
deltaRotation,
|
deltaRotation,
|
||||||
rollInfluence,
|
rollInfluence,
|
||||||
maxSuspensionForce,
|
maxSuspensionForce,
|
||||||
isFrontWheel,
|
|
||||||
clippedInvContactDotSuspension,
|
clippedInvContactDotSuspension,
|
||||||
suspensionRelativeVelocity,
|
suspensionRelativeVelocity,
|
||||||
suspensionForce,
|
suspensionForce,
|
||||||
|
@ -2646,147 +3172,144 @@ GameLib.D3.RaycastWheel = function(
|
||||||
|
|
||||||
this.componentId = GameLib.D3.Tools.RandomId();
|
this.componentId = GameLib.D3.Tools.RandomId();
|
||||||
|
|
||||||
if(typeof chassisConnectionPointLocal == 'undefined') {
|
if(typeof chassisConnectionPointLocal == 'undefined' || chassisConnectionPointLocal == null) {
|
||||||
chassisConnectionPointLocal = new this.engine.instance.Vec3();
|
chassisConnectionPointLocal = new this.engine.instance.Vec3();
|
||||||
}
|
}
|
||||||
this.chassisConnectionPointLocal = chassisConnectionPointLocal;
|
this.chassisConnectionPointLocal = chassisConnectionPointLocal;
|
||||||
|
|
||||||
if(typeof chassisConnectionPointWorld == 'undefined') {
|
if(typeof chassisConnectionPointWorld == 'undefined' || chassisConnectionPointWorld == null) {
|
||||||
chassisConnectionPointWorld = new this.engine.instance.Vec3();
|
chassisConnectionPointWorld = new this.engine.instance.Vec3();
|
||||||
}
|
}
|
||||||
this.chassisConnectionPointWorld = chassisConnectionPointWorld;
|
this.chassisConnectionPointWorld = chassisConnectionPointWorld;
|
||||||
|
|
||||||
if(typeof directionLocal == 'undefined') {
|
if(typeof directionLocal == 'undefined' || directionLocal == null) {
|
||||||
directionLocal = new this.engine.instance.Vec3();
|
directionLocal = new this.engine.instance.Vec3();
|
||||||
}
|
}
|
||||||
this.directionLocal = directionLocal;
|
this.directionLocal = directionLocal;
|
||||||
|
|
||||||
if(typeof directionWorld == 'undefined') {
|
if(typeof directionWorld == 'undefined' || directionWorld == null) {
|
||||||
directionWorld = new this.engine.instance.Vec3();
|
directionWorld = new this.engine.instance.Vec3();
|
||||||
}
|
}
|
||||||
this.directionWorld = directionWorld;
|
this.directionWorld = directionWorld;
|
||||||
|
|
||||||
if(typeof axleLocal == 'undefined') {
|
if(typeof axleLocal == 'undefined' || axleLocal == null) {
|
||||||
axleLocal = new this.engine.instance.Vec3();
|
axleLocal = new this.engine.instance.Vec3();
|
||||||
}
|
}
|
||||||
this.axleLocal = axleLocal;
|
this.axleLocal = axleLocal;
|
||||||
|
|
||||||
if(typeof axleWorld == 'undefined') {
|
if(typeof axleWorld == 'undefined' || axleWorld == null) {
|
||||||
axleWorld = new this.engine.instance.Vec3();
|
axleWorld = new this.engine.instance.Vec3();
|
||||||
}
|
}
|
||||||
this.axleWorld = axleWorld;
|
this.axleWorld = axleWorld;
|
||||||
|
|
||||||
if(typeof suspensionRestLength == 'undefined') {
|
if(typeof suspensionRestLength == 'undefined' || suspensionRestLength == null) {
|
||||||
suspensionRestLength = 1;
|
suspensionRestLength = 1;
|
||||||
}
|
}
|
||||||
this.suspensionRestLength = suspensionRestLength;
|
this.suspensionRestLength = suspensionRestLength;
|
||||||
|
|
||||||
if(typeof suspensionMaxLength == 'undefined') {
|
if(typeof suspensionMaxLength == 'undefined' || suspensionMaxLength == null) {
|
||||||
suspensionMaxLength = 2;
|
suspensionMaxLength = 2;
|
||||||
}
|
}
|
||||||
this.suspensionMaxLength = suspensionMaxLength;
|
this.suspensionMaxLength = suspensionMaxLength;
|
||||||
|
|
||||||
if(typeof radius == 'undefined') {
|
if(typeof radius == 'undefined' || radius == null) {
|
||||||
radius = 1;
|
radius = 1;
|
||||||
}
|
}
|
||||||
this.radius = radius;
|
this.radius = radius;
|
||||||
|
|
||||||
if(typeof suspensionStiffness == 'undefined') {
|
if(typeof suspensionStiffness == 'undefined' || suspensionStiffness == null) {
|
||||||
suspensionStiffness = 100;
|
suspensionStiffness = 100;
|
||||||
}
|
}
|
||||||
this.suspensionStiffness = suspensionStiffness;
|
this.suspensionStiffness = suspensionStiffness;
|
||||||
|
|
||||||
if(typeof dampingCompression == 'undefined') {
|
if(typeof dampingCompression == 'undefined' || dampingCompression == null) {
|
||||||
dampingCompression = 10;
|
dampingCompression = 10;
|
||||||
}
|
}
|
||||||
this.dampingCompression = dampingCompression;
|
this.dampingCompression = dampingCompression;
|
||||||
|
|
||||||
if(typeof dampingRelaxation == 'undefined') {
|
if(typeof dampingRelaxation == 'undefined' || dampingRelaxation == null) {
|
||||||
dampingRelaxation = 10;
|
dampingRelaxation = 10;
|
||||||
}
|
}
|
||||||
this.dampingRelaxation = dampingRelaxation;
|
this.dampingRelaxation = dampingRelaxation;
|
||||||
|
|
||||||
if(typeof frictionSlip == 'undefined') {
|
if(typeof frictionSlip == 'undefined' || frictionSlip == null) {
|
||||||
frictionSlip = 10000;
|
frictionSlip = 10000;
|
||||||
}
|
}
|
||||||
this.frictionSlip = frictionSlip;
|
this.frictionSlip = frictionSlip;
|
||||||
|
|
||||||
if(typeof steering == 'undefined') {
|
if(typeof steering == 'undefined' || steering == null) {
|
||||||
steering = 0;
|
steering = 0;
|
||||||
}
|
}
|
||||||
this.steering = steering;
|
this.steering = steering;
|
||||||
|
|
||||||
if(typeof rotation == 'undefined') {
|
if(typeof rotation == 'undefined' || rotation == null) {
|
||||||
rotation = 0;
|
rotation = 0;
|
||||||
}
|
}
|
||||||
this.rotation = rotation;
|
this.rotation = rotation;
|
||||||
|
|
||||||
if(typeof deltaRotation == 'undefined') {
|
if(typeof deltaRotation == 'undefined' || deltaRotation == null) {
|
||||||
deltaRotation = 0;
|
deltaRotation = 0;
|
||||||
}
|
}
|
||||||
this.deltaRotation = deltaRotation;
|
this.deltaRotation = deltaRotation;
|
||||||
|
|
||||||
if(typeof rollInfluence == 'undefined') {
|
if(typeof rollInfluence == 'undefined' || rollInfluence == null) {
|
||||||
rollInfluence = 0.01;
|
rollInfluence = 0.01;
|
||||||
}
|
}
|
||||||
this.rollInfluence = rollInfluence;
|
this.rollInfluence = rollInfluence;
|
||||||
|
|
||||||
if(typeof maxSuspensionForce == 'undefined') {
|
if(typeof maxSuspensionForce == 'undefined' || maxSuspensionForce == null) {
|
||||||
maxSuspensionForce = Number.MAX_VALUE;
|
maxSuspensionForce = Number.MAX_VALUE;
|
||||||
}
|
}
|
||||||
this.maxSuspensionForce = maxSuspensionForce;
|
this.maxSuspensionForce = maxSuspensionForce;
|
||||||
|
|
||||||
if(typeof isFrontWheel == 'undefined') {
|
if(typeof clippedInvContactDotSuspension == 'undefined' || clippedInvContactDotSuspension == null) {
|
||||||
isFrontWheel = true;
|
|
||||||
}
|
|
||||||
this.isFrontWheel = isFrontWheel;
|
|
||||||
|
|
||||||
if(typeof clippedInvContactDotSuspension == 'undefined') {
|
|
||||||
clippedInvContactDotSuspension = 1;
|
clippedInvContactDotSuspension = 1;
|
||||||
}
|
}
|
||||||
this.clippedInvContactDotSuspension = clippedInvContactDotSuspension;
|
this.clippedInvContactDotSuspension = clippedInvContactDotSuspension;
|
||||||
|
|
||||||
if(typeof suspensionRelativeVelocity == 'undefined') {
|
if(typeof suspensionRelativeVelocity == 'undefined' || suspensionRelativeVelocity == null) {
|
||||||
suspensionRelativeVelocity = 0;
|
suspensionRelativeVelocity = 0;
|
||||||
}
|
}
|
||||||
this.suspensionRelativeVelocity = suspensionRelativeVelocity;
|
this.suspensionRelativeVelocity = suspensionRelativeVelocity;
|
||||||
|
|
||||||
if(typeof suspensionForce == 'undefined') {
|
if(typeof suspensionForce == 'undefined' || suspensionForce == null) {
|
||||||
suspensionForce = 0;
|
suspensionForce = 0;
|
||||||
}
|
}
|
||||||
this.suspensionForce = suspensionForce;
|
this.suspensionForce = suspensionForce;
|
||||||
|
|
||||||
if(typeof skidInfo == 'undefined') {
|
if(typeof skidInfo == 'undefined' || skidInfo == null) {
|
||||||
skidInfo = 0;
|
skidInfo = 0;
|
||||||
}
|
}
|
||||||
this.skidInfo = skidInfo;
|
this.skidInfo = skidInfo;
|
||||||
|
|
||||||
if(typeof suspensionLength == 'undefined') {
|
if(typeof suspensionLength == 'undefined' || suspensionLength == null) {
|
||||||
suspensionLength = 0;
|
suspensionLength = 0;
|
||||||
}
|
}
|
||||||
this.suspensionLength = suspensionLength;
|
this.suspensionLength = suspensionLength;
|
||||||
|
|
||||||
if(typeof maxSuspensionTravel == 'undefined') {
|
if(typeof maxSuspensionTravel == 'undefined' || maxSuspensionTravel == null) {
|
||||||
maxSuspensionTravel = 1;
|
maxSuspensionTravel = 1;
|
||||||
}
|
}
|
||||||
this.maxSuspensionTravel = maxSuspensionTravel;
|
this.maxSuspensionTravel = maxSuspensionTravel;
|
||||||
|
|
||||||
if(typeof useCustomSlidingRotationalSpeed == 'undefined') {
|
if(typeof useCustomSlidingRotationalSpeed == 'undefined' || useCustomSlidingRotationalSpeed == null) {
|
||||||
useCustomSlidingRotationalSpeed = false;
|
useCustomSlidingRotationalSpeed = false;
|
||||||
}
|
}
|
||||||
this.useCustomSlidingRotationalSpeed = useCustomSlidingRotationalSpeed;
|
this.useCustomSlidingRotationalSpeed = useCustomSlidingRotationalSpeed;
|
||||||
|
|
||||||
if(typeof customSlidingRotationalSpeed == 'undefined') {
|
if(typeof customSlidingRotationalSpeed == 'undefined' || customSlidingRotationalSpeed == null) {
|
||||||
customSlidingRotationalSpeed = -0.1;
|
customSlidingRotationalSpeed = -0.1;
|
||||||
}
|
}
|
||||||
this.customSlidingRotationalSpeed = customSlidingRotationalSpeed;
|
this.customSlidingRotationalSpeed = customSlidingRotationalSpeed;
|
||||||
|
|
||||||
this.instance = this.createInstance();
|
this.instance = this.createInstance();
|
||||||
|
|
||||||
|
// this gets assigned at runtime, when the wheel gets added to a vehicle
|
||||||
|
this.wheelIndex = -1;
|
||||||
};
|
};
|
||||||
|
|
||||||
GameLib.D3.RaycastWheel.prototype.createInstance = function() {
|
GameLib.D3.RaycastWheel.prototype.createInstance = function() {
|
||||||
|
return {
|
||||||
return new this.engine.instance.WheelInfo({
|
|
||||||
chassisConnectionPointLocal : this.chassisConnectionPointLocal,
|
chassisConnectionPointLocal : this.chassisConnectionPointLocal,
|
||||||
chassisConnectionPointWorld : this.chassisConnectionPointWorld,
|
chassisConnectionPointWorld : this.chassisConnectionPointWorld,
|
||||||
directionLocal : this.directionLocal,
|
directionLocal : this.directionLocal,
|
||||||
|
@ -2805,7 +3328,6 @@ GameLib.D3.RaycastWheel.prototype.createInstance = function() {
|
||||||
deltaRotation : this.deltaRotation,
|
deltaRotation : this.deltaRotation,
|
||||||
rollInfluence : this.rollInfluence,
|
rollInfluence : this.rollInfluence,
|
||||||
maxSuspensionForce : this.maxSuspensionForce,
|
maxSuspensionForce : this.maxSuspensionForce,
|
||||||
isFrontWheel : this.isFrontWheel,
|
|
||||||
clippedInvContactDotSuspension : this.clippedInvContactDotSuspension,
|
clippedInvContactDotSuspension : this.clippedInvContactDotSuspension,
|
||||||
suspensionRelativeVelocity : this.suspensionRelativeVelocity,
|
suspensionRelativeVelocity : this.suspensionRelativeVelocity,
|
||||||
suspensionForce : this.suspensionForce,
|
suspensionForce : this.suspensionForce,
|
||||||
|
@ -2814,7 +3336,7 @@ GameLib.D3.RaycastWheel.prototype.createInstance = function() {
|
||||||
maxSuspensionTravel : this.maxSuspensionTravel,
|
maxSuspensionTravel : this.maxSuspensionTravel,
|
||||||
useCustomSlidingRotationalSpeed : this.useCustomSlidingRotationalSpeed,
|
useCustomSlidingRotationalSpeed : this.useCustomSlidingRotationalSpeed,
|
||||||
customSlidingRotationalSpeed : this.customSlidingRotationalSpeed
|
customSlidingRotationalSpeed : this.customSlidingRotationalSpeed
|
||||||
});
|
};
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -3013,11 +3535,11 @@ GameLib.D3.RigidBody.prototype.addShape = function(
|
||||||
offset,
|
offset,
|
||||||
orientation
|
orientation
|
||||||
) {
|
) {
|
||||||
if (!offset) {
|
if (!offset || typeof offset == 'undefined') {
|
||||||
offset = new GameLib.D3.Vector3(0,0,0);
|
offset = new GameLib.D3.Vector3(0,0,0);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!orientation) {
|
if (!orientation || typeof orientation == 'undefined') {
|
||||||
orientation = new GameLib.D3.Vector4(0,0,0,1);
|
orientation = new GameLib.D3.Vector4(0,0,0,1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3038,50 +3560,26 @@ GameLib.D3.RigidBody.prototype.addShape = function(
|
||||||
};
|
};
|
||||||
|
|
||||||
///////////////////////// Methods to override //////////////////////////
|
///////////////////////// Methods to override //////////////////////////
|
||||||
GameLib.D3.RigidBody.prototype.onLateUpdate = function(
|
GameLib.D3.RigidBody.prototype.onUpdate = function(
|
||||||
deltaTime,
|
deltaTime,
|
||||||
parentEntity
|
parentEntity
|
||||||
) {
|
) {
|
||||||
if(parentEntity.mesh) {
|
if(parentEntity) {
|
||||||
var quaternion = new THREE.Quaternion();
|
var quaternion = new THREE.Quaternion();
|
||||||
quaternion.copy(this.instance.quaternion);
|
quaternion.copy(this.instance.quaternion);
|
||||||
|
|
||||||
var quaternionCopy = quaternion.clone();
|
|
||||||
|
|
||||||
var position = new THREE.Vector3();
|
var position = new THREE.Vector3();
|
||||||
position.copy(this.instance.position);
|
position.copy(this.instance.position);
|
||||||
|
|
||||||
// todo: create mesh superset with permutate field
|
parentEntity.position.x = position.x;
|
||||||
// permutate : {
|
parentEntity.position.y = position.y;
|
||||||
// offset : {
|
parentEntity.position.z = position.z;
|
||||||
// quaternion : new Quaternion(),
|
|
||||||
// position : new Vec3()
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
/*if(mesh.permutate) {
|
parentEntity.quaternion.x = quaternion.x;
|
||||||
if(mesh.permutate.offset) {
|
parentEntity.quaternion.y = quaternion.y;
|
||||||
if(mesh.permutate.offset.quaternion) {
|
parentEntity.quaternion.z = quaternion.z;
|
||||||
var offsetQuaternion = new THREE.Quaternion();
|
parentEntity.quaternion.w = quaternion.w;
|
||||||
offsetQuaternion.copy(mesh.permutate.offset.quaternion);
|
|
||||||
quaternion = quaternion.multiply(offsetQuaternion).normalize();
|
|
||||||
}
|
|
||||||
|
|
||||||
if(mesh.permutate.offset.position) {
|
|
||||||
var offsetPosition = new THREE.Vector3();
|
|
||||||
offsetPosition.copy(mesh.permutate.offset.position);
|
|
||||||
//position = position.add(offsetPosition);
|
|
||||||
position = position.add(offsetPosition.applyQuaternion(quaternionCopy));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}*/
|
|
||||||
|
|
||||||
parentEntity.mesh.position.copy(position);
|
|
||||||
parentEntity.mesh.quaternion.copy(quaternion);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
/**
|
/**
|
||||||
* Rigid Wheel superset
|
* Rigid Wheel superset
|
||||||
|
@ -3187,16 +3685,22 @@ GameLib.D3.Scene = function(
|
||||||
}
|
}
|
||||||
this.entities = entities;
|
this.entities = entities;
|
||||||
|
|
||||||
// todo: remove this from the scene class
|
this.instance = this.createInstance();
|
||||||
// changes
|
|
||||||
//this.threeScene = new THREE.Scene();
|
|
||||||
//this.threeScene.render = true;
|
|
||||||
|
|
||||||
// assoc array
|
// assoc array
|
||||||
//this.meshIdToMesh = {};
|
this.meshIdToMesh = {};
|
||||||
|
|
||||||
// assoc array
|
// assoc array
|
||||||
//this.componentIdToComponent = {};
|
this.componentIdToComponent = {};
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
GameLib.D3.Scene.prototype.createInstance = function (
|
||||||
|
|
||||||
|
) {
|
||||||
|
var scene = new THREE.Scene();
|
||||||
|
scene.render = true;
|
||||||
|
return scene;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -3705,7 +4209,7 @@ GameLib.D3.Scene.prototype.render = function(
|
||||||
renderer,
|
renderer,
|
||||||
camera
|
camera
|
||||||
) {
|
) {
|
||||||
renderer.render(this.threeScene, camera);
|
renderer.render(this.instance, camera);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -3753,7 +4257,7 @@ GameLib.D3.Scene.prototype.registerComponent = function(
|
||||||
GameLib.D3.Scene.prototype.registerLight = function(
|
GameLib.D3.Scene.prototype.registerLight = function(
|
||||||
light
|
light
|
||||||
) {
|
) {
|
||||||
this.threeScene.add(light);
|
this.instance.add(light);
|
||||||
};
|
};
|
||||||
/**
|
/**
|
||||||
* Physics Shape Superset
|
* Physics Shape Superset
|
||||||
|
@ -4715,6 +5219,12 @@ GameLib.D3.Utils.Extend = function(
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
GameLib.D3.Utils.UndefinedOrNull = function (
|
||||||
|
variable
|
||||||
|
) {
|
||||||
|
return typeof variable == 'undefined' || variable == null;
|
||||||
|
};
|
||||||
|
|
||||||
GameLib.D3.Utils.Raycast = function (
|
GameLib.D3.Utils.Raycast = function (
|
||||||
from,
|
from,
|
||||||
to,
|
to,
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
GameLib.D3.ComponentFollow = function(
|
GameLib.D3.ComponentFollow = function(
|
||||||
componentId,
|
componentId,
|
||||||
targetEntity
|
targetEntity,
|
||||||
|
targetOffset,
|
||||||
|
minDistance
|
||||||
) {
|
) {
|
||||||
this.componentId = componentId || GameLib.D3.Tools.RandomId();
|
this.componentId = componentId || GameLib.D3.Tools.RandomId();
|
||||||
this.parentEntity = null;
|
this.parentEntity = null;
|
||||||
|
@ -11,6 +13,14 @@ GameLib.D3.ComponentFollow = function(
|
||||||
//
|
//
|
||||||
this.targetEntity = targetEntity;
|
this.targetEntity = targetEntity;
|
||||||
this.moveSpeed = 2.5;
|
this.moveSpeed = 2.5;
|
||||||
|
|
||||||
|
|
||||||
|
if(GameLib.D3.Utils.UndefinedOrNull(targetOffset)) {
|
||||||
|
targetOffset = new GameLib.D3.Vector3(0, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.targetOffset = targetOffset;
|
||||||
|
this.minDistance = minDistance || 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
///////////////////////// Methods to override //////////////////////////
|
///////////////////////// Methods to override //////////////////////////
|
||||||
|
@ -20,7 +30,12 @@ GameLib.D3.ComponentFollow.prototype.onUpdate = function(
|
||||||
) {
|
) {
|
||||||
if(this.targetEntity) {
|
if(this.targetEntity) {
|
||||||
var target = new THREE.Vector3().copy(this.targetEntity.position);
|
var target = new THREE.Vector3().copy(this.targetEntity.position);
|
||||||
|
target.x += this.targetOffset.x;
|
||||||
|
target.y += this.targetOffset.y;
|
||||||
|
target.z += this.targetOffset.z;
|
||||||
|
|
||||||
parentEntity.position = parentEntity.position.lerp(target, this.moveSpeed * deltaTime);
|
if(target.distanceTo(parentEntity.position) > this.minDistance) {
|
||||||
|
parentEntity.position = parentEntity.position.lerp(target, this.moveSpeed * deltaTime);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
|
@ -1,6 +1,7 @@
|
||||||
GameLib.D3.ComponentLookAt = function(
|
GameLib.D3.ComponentLookAt = function(
|
||||||
componentId,
|
componentId,
|
||||||
targetEntity
|
targetEntity,
|
||||||
|
targetOffset
|
||||||
) {
|
) {
|
||||||
this.componentId = componentId || GameLib.D3.Tools.RandomId();
|
this.componentId = componentId || GameLib.D3.Tools.RandomId();
|
||||||
this.parentEntity = null;
|
this.parentEntity = null;
|
||||||
|
@ -8,8 +9,9 @@ GameLib.D3.ComponentLookAt = function(
|
||||||
// Todo: this should be executed somewhere in game-lib-z, so that we don't execute it on every construction of an object.
|
// 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);
|
GameLib.D3.Utils.Extend(GameLib.D3.ComponentLookAt, GameLib.D3.ComponentInterface);
|
||||||
|
|
||||||
//
|
// todo: USE TARGET OFFSET.
|
||||||
this.targetEntity = targetEntity;
|
this.targetEntity = targetEntity;
|
||||||
|
this.targetOffset = targetOffset || new GameLib.D3.Vector3(0, 0, 0);
|
||||||
};
|
};
|
||||||
|
|
||||||
///////////////////////// Methods to override //////////////////////////
|
///////////////////////// Methods to override //////////////////////////
|
||||||
|
|
Loading…
Reference in New Issue