follow component target offset.

todo:
- look at: use target offset.
- camera component sphere-rigidbody.
beta.r3js.org
polygonboutique 2016-11-04 16:31:56 +01:00
parent 274ab21d3e
commit 9208ea9257
4 changed files with 622 additions and 95 deletions

File diff suppressed because one or more lines are too long

View File

@ -166,6 +166,28 @@ GameLib.D3.Color = function(r, g, b, a) {
this.b = b;
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(
componentId
) {
@ -190,6 +212,248 @@ GameLib.D3.ComponentColorFlash.prototype.onSetParentEntity = function(
) {
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(
componentId
) {
@ -257,6 +521,196 @@ GameLib.D3.ComponentInterface.prototype.onSetParentEntity = function(
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
@ -315,7 +769,10 @@ GameLib.D3.Engine.ENGINE_TYPE_AMMO = 0x2;
GameLib.D3.Engine.ENGINE_TYPE_GOBLIN = 0x3;
GameLib.D3.Entity = function(
meshId,
componentIds
componentIds,
position,
quaternion,
scale
) {
this.meshId = meshId;
@ -325,8 +782,24 @@ GameLib.D3.Entity = function(
this.componentIds = componentIds;
// constructed at runtime
this.parentScene = 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);
};
@ -375,7 +854,7 @@ GameLib.D3.Entity.prototype.register = function(
this.parentScene = parentScene;
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];
}
@ -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 //////////////////////////
GameLib.D3.Entity.prototype.onUpdate = function(
@ -679,14 +1172,14 @@ GameLib.D3.Game.prototype.addScene = function(
};
GameLib.D3.Game.prototype.processPhysics = function (
timestep
dt
) {
for(var s in this.scenes) {
var scene = this.scenes[s];
for(var w in scene.worlds) {
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(
dt
dt,
fixedDt
) {
for(var s in this.scenes) {
var scene = this.scenes[s];
for(var w in scene.worlds) {
var world = scene.worlds[w];
world.step(dt);
world.step(fixedDt);
}
scene.update(dt);
@ -2565,7 +3059,8 @@ GameLib.D3.PolyVertex.prototype.clone = function() {
GameLib.D3.RaycastVehicle = function(
engine,
chassisBody,
wheels
wheels,
wheelBodies
) {
this.engine = engine;
this.engine.isNotCannonThrow();
@ -2579,7 +3074,14 @@ GameLib.D3.RaycastVehicle = function(
}
this.wheels = wheels;
if(GameLib.D3.Utils.UndefinedOrNull(wheelBodies)) {
wheelBodies = [];
}
this.wheelBodies = wheelBodies;
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
* @param wheel GameLib.D3.RaycastWheel
* @param wheelRigidBody GameLib.D3.RigidBody
*/
GameLib.D3.RaycastVehicle.prototype.addWheel = function (
wheel
wheel,
wheelRigidBody
) {
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() {
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(
engine,
chassisConnectionPointLocal,
@ -2631,7 +3158,6 @@ GameLib.D3.RaycastWheel = function(
deltaRotation,
rollInfluence,
maxSuspensionForce,
isFrontWheel,
clippedInvContactDotSuspension,
suspensionRelativeVelocity,
suspensionForce,
@ -2646,147 +3172,144 @@ GameLib.D3.RaycastWheel = function(
this.componentId = GameLib.D3.Tools.RandomId();
if(typeof chassisConnectionPointLocal == 'undefined') {
if(typeof chassisConnectionPointLocal == 'undefined' || chassisConnectionPointLocal == null) {
chassisConnectionPointLocal = new this.engine.instance.Vec3();
}
this.chassisConnectionPointLocal = chassisConnectionPointLocal;
if(typeof chassisConnectionPointWorld == 'undefined') {
if(typeof chassisConnectionPointWorld == 'undefined' || chassisConnectionPointWorld == null) {
chassisConnectionPointWorld = new this.engine.instance.Vec3();
}
this.chassisConnectionPointWorld = chassisConnectionPointWorld;
if(typeof directionLocal == 'undefined') {
if(typeof directionLocal == 'undefined' || directionLocal == null) {
directionLocal = new this.engine.instance.Vec3();
}
this.directionLocal = directionLocal;
if(typeof directionWorld == 'undefined') {
if(typeof directionWorld == 'undefined' || directionWorld == null) {
directionWorld = new this.engine.instance.Vec3();
}
this.directionWorld = directionWorld;
if(typeof axleLocal == 'undefined') {
if(typeof axleLocal == 'undefined' || axleLocal == null) {
axleLocal = new this.engine.instance.Vec3();
}
this.axleLocal = axleLocal;
if(typeof axleWorld == 'undefined') {
if(typeof axleWorld == 'undefined' || axleWorld == null) {
axleWorld = new this.engine.instance.Vec3();
}
this.axleWorld = axleWorld;
if(typeof suspensionRestLength == 'undefined') {
if(typeof suspensionRestLength == 'undefined' || suspensionRestLength == null) {
suspensionRestLength = 1;
}
this.suspensionRestLength = suspensionRestLength;
if(typeof suspensionMaxLength == 'undefined') {
if(typeof suspensionMaxLength == 'undefined' || suspensionMaxLength == null) {
suspensionMaxLength = 2;
}
this.suspensionMaxLength = suspensionMaxLength;
if(typeof radius == 'undefined') {
if(typeof radius == 'undefined' || radius == null) {
radius = 1;
}
this.radius = radius;
if(typeof suspensionStiffness == 'undefined') {
if(typeof suspensionStiffness == 'undefined' || suspensionStiffness == null) {
suspensionStiffness = 100;
}
this.suspensionStiffness = suspensionStiffness;
if(typeof dampingCompression == 'undefined') {
if(typeof dampingCompression == 'undefined' || dampingCompression == null) {
dampingCompression = 10;
}
this.dampingCompression = dampingCompression;
if(typeof dampingRelaxation == 'undefined') {
if(typeof dampingRelaxation == 'undefined' || dampingRelaxation == null) {
dampingRelaxation = 10;
}
this.dampingRelaxation = dampingRelaxation;
if(typeof frictionSlip == 'undefined') {
if(typeof frictionSlip == 'undefined' || frictionSlip == null) {
frictionSlip = 10000;
}
this.frictionSlip = frictionSlip;
if(typeof steering == 'undefined') {
if(typeof steering == 'undefined' || steering == null) {
steering = 0;
}
this.steering = steering;
if(typeof rotation == 'undefined') {
if(typeof rotation == 'undefined' || rotation == null) {
rotation = 0;
}
this.rotation = rotation;
if(typeof deltaRotation == 'undefined') {
if(typeof deltaRotation == 'undefined' || deltaRotation == null) {
deltaRotation = 0;
}
this.deltaRotation = deltaRotation;
if(typeof rollInfluence == 'undefined') {
if(typeof rollInfluence == 'undefined' || rollInfluence == null) {
rollInfluence = 0.01;
}
this.rollInfluence = rollInfluence;
if(typeof maxSuspensionForce == 'undefined') {
if(typeof maxSuspensionForce == 'undefined' || maxSuspensionForce == null) {
maxSuspensionForce = Number.MAX_VALUE;
}
this.maxSuspensionForce = maxSuspensionForce;
if(typeof isFrontWheel == 'undefined') {
isFrontWheel = true;
}
this.isFrontWheel = isFrontWheel;
if(typeof clippedInvContactDotSuspension == 'undefined') {
if(typeof clippedInvContactDotSuspension == 'undefined' || clippedInvContactDotSuspension == null) {
clippedInvContactDotSuspension = 1;
}
this.clippedInvContactDotSuspension = clippedInvContactDotSuspension;
if(typeof suspensionRelativeVelocity == 'undefined') {
if(typeof suspensionRelativeVelocity == 'undefined' || suspensionRelativeVelocity == null) {
suspensionRelativeVelocity = 0;
}
this.suspensionRelativeVelocity = suspensionRelativeVelocity;
if(typeof suspensionForce == 'undefined') {
if(typeof suspensionForce == 'undefined' || suspensionForce == null) {
suspensionForce = 0;
}
this.suspensionForce = suspensionForce;
if(typeof skidInfo == 'undefined') {
if(typeof skidInfo == 'undefined' || skidInfo == null) {
skidInfo = 0;
}
this.skidInfo = skidInfo;
if(typeof suspensionLength == 'undefined') {
if(typeof suspensionLength == 'undefined' || suspensionLength == null) {
suspensionLength = 0;
}
this.suspensionLength = suspensionLength;
if(typeof maxSuspensionTravel == 'undefined') {
if(typeof maxSuspensionTravel == 'undefined' || maxSuspensionTravel == null) {
maxSuspensionTravel = 1;
}
this.maxSuspensionTravel = maxSuspensionTravel;
if(typeof useCustomSlidingRotationalSpeed == 'undefined') {
if(typeof useCustomSlidingRotationalSpeed == 'undefined' || useCustomSlidingRotationalSpeed == null) {
useCustomSlidingRotationalSpeed = false;
}
this.useCustomSlidingRotationalSpeed = useCustomSlidingRotationalSpeed;
if(typeof customSlidingRotationalSpeed == 'undefined') {
if(typeof customSlidingRotationalSpeed == 'undefined' || customSlidingRotationalSpeed == null) {
customSlidingRotationalSpeed = -0.1;
}
this.customSlidingRotationalSpeed = customSlidingRotationalSpeed;
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() {
return new this.engine.instance.WheelInfo({
return {
chassisConnectionPointLocal : this.chassisConnectionPointLocal,
chassisConnectionPointWorld : this.chassisConnectionPointWorld,
directionLocal : this.directionLocal,
@ -2805,7 +3328,6 @@ GameLib.D3.RaycastWheel.prototype.createInstance = function() {
deltaRotation : this.deltaRotation,
rollInfluence : this.rollInfluence,
maxSuspensionForce : this.maxSuspensionForce,
isFrontWheel : this.isFrontWheel,
clippedInvContactDotSuspension : this.clippedInvContactDotSuspension,
suspensionRelativeVelocity : this.suspensionRelativeVelocity,
suspensionForce : this.suspensionForce,
@ -2814,7 +3336,7 @@ GameLib.D3.RaycastWheel.prototype.createInstance = function() {
maxSuspensionTravel : this.maxSuspensionTravel,
useCustomSlidingRotationalSpeed : this.useCustomSlidingRotationalSpeed,
customSlidingRotationalSpeed : this.customSlidingRotationalSpeed
});
};
};
@ -3013,11 +3535,11 @@ GameLib.D3.RigidBody.prototype.addShape = function(
offset,
orientation
) {
if (!offset) {
if (!offset || typeof offset == 'undefined') {
offset = new GameLib.D3.Vector3(0,0,0);
}
if (!orientation) {
if (!orientation || typeof orientation == 'undefined') {
orientation = new GameLib.D3.Vector4(0,0,0,1);
}
@ -3038,50 +3560,26 @@ GameLib.D3.RigidBody.prototype.addShape = function(
};
///////////////////////// Methods to override //////////////////////////
GameLib.D3.RigidBody.prototype.onLateUpdate = function(
GameLib.D3.RigidBody.prototype.onUpdate = function(
deltaTime,
parentEntity
) {
if(parentEntity.mesh) {
if(parentEntity) {
var quaternion = new THREE.Quaternion();
quaternion.copy(this.instance.quaternion);
var quaternionCopy = quaternion.clone();
var position = new THREE.Vector3();
position.copy(this.instance.position);
// todo: create mesh superset with permutate field
// permutate : {
// offset : {
// quaternion : new Quaternion(),
// position : new Vec3()
// }
// }
parentEntity.position.x = position.x;
parentEntity.position.y = position.y;
parentEntity.position.z = position.z;
/*if(mesh.permutate) {
if(mesh.permutate.offset) {
if(mesh.permutate.offset.quaternion) {
var offsetQuaternion = new THREE.Quaternion();
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);
parentEntity.quaternion.x = quaternion.x;
parentEntity.quaternion.y = quaternion.y;
parentEntity.quaternion.z = quaternion.z;
parentEntity.quaternion.w = quaternion.w;
}
};
/**
* Rigid Wheel superset
@ -3187,16 +3685,22 @@ GameLib.D3.Scene = function(
}
this.entities = entities;
// todo: remove this from the scene class
// changes
//this.threeScene = new THREE.Scene();
//this.threeScene.render = true;
this.instance = this.createInstance();
// assoc array
//this.meshIdToMesh = {};
this.meshIdToMesh = {};
// 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,
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(
light
) {
this.threeScene.add(light);
this.instance.add(light);
};
/**
* 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 (
from,
to,

View File

@ -1,6 +1,8 @@
GameLib.D3.ComponentFollow = function(
componentId,
targetEntity
targetEntity,
targetOffset,
minDistance
) {
this.componentId = componentId || GameLib.D3.Tools.RandomId();
this.parentEntity = null;
@ -11,6 +13,14 @@ GameLib.D3.ComponentFollow = function(
//
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 //////////////////////////
@ -20,7 +30,12 @@ GameLib.D3.ComponentFollow.prototype.onUpdate = function(
) {
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;
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);
}
}
};

View File

@ -1,6 +1,7 @@
GameLib.D3.ComponentLookAt = function(
componentId,
targetEntity
targetEntity,
targetOffset
) {
this.componentId = componentId || GameLib.D3.Tools.RandomId();
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.
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 //////////////////////////