diff --git a/src/game-lib-component-mesh-permutation.js b/src/game-lib-component-mesh-permutation.js new file mode 100644 index 0000000..a90c2af --- /dev/null +++ b/src/game-lib-component-mesh-permutation.js @@ -0,0 +1,42 @@ +GameLib.D3.ComponentMeshPermutation = function( + componentId, + positionOffset, + quaternionOffset +) { + 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); + + // 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)); + + parentEntity.mesh.position.copy(position); + parentEntity.mesh.quaternion.copy(quaternion); + } +}; \ No newline at end of file diff --git a/src/game-lib-component-raycast-vehicle-controls.js b/src/game-lib-component-raycast-vehicle-controls.js new file mode 100644 index 0000000..411adb6 --- /dev/null +++ b/src/game-lib-component-raycast-vehicle-controls.js @@ -0,0 +1,24 @@ +GameLib.D3.ComponentRaycastVehicleControls = 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.ComponentRaycastVehicleControls, GameLib.D3.ComponentInterface); +}; + +///////////////////////// Methods to override ////////////////////////// +GameLib.D3.ComponentRaycastVehicleControls.prototype.onUpdate = function( + deltaTime, + parentEntity +) { + this.parentEntity.mesh.material.color = new THREE.Color(Math.random(), Math.random(), Math.random()); +}; + +GameLib.D3.ComponentRaycastVehicleControls.prototype.onRegistered = function( + parentScene +) { + // Hook in document.eventlisteners + // save values & in the update method we just apply them on the vehicle itself +}; \ No newline at end of file diff --git a/src/game-lib-entity.js b/src/game-lib-entity.js index 23eb99a..75fc2a4 100644 --- a/src/game-lib-entity.js +++ b/src/game-lib-entity.js @@ -10,8 +10,19 @@ GameLib.D3.Entity = function( this.componentIds = componentIds; + // constructed at runtime this.parentScene = null; this.mesh = null; + + // todo: + // add position, rotation & scale properties to entity itself! + + this.permutate = { + offset : { + position : null, + quaternion : null + } + }; }; /** diff --git a/src/game-lib-raycast-vehicle.js b/src/game-lib-raycast-vehicle.js index 22a7233..bfbcf2c 100644 --- a/src/game-lib-raycast-vehicle.js +++ b/src/game-lib-raycast-vehicle.js @@ -23,6 +23,8 @@ GameLib.D3.RaycastVehicle = function( this.wheels = wheels; this.instance = this.createInstance(); + + GameLib.D3.Utils.Extend(GameLib.D3.RaycastVehicle, GameLib.D3.ComponentInterface); }; /** @@ -43,7 +45,7 @@ GameLib.D3.RaycastVehicle.prototype.addWheel = function ( wheel ) { this.wheels.push(wheel); - this.instance.addWheel(wheel.instance); + wheel.wheelIndex = this.instance.addWheel(wheel.instance); }; /** @@ -53,4 +55,22 @@ 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); + }*/ +}; + +GameLib.D3.RaycastVehicle.prototype.onRegistered = function( + parentScene +) { + }; \ No newline at end of file diff --git a/src/game-lib-raycast-wheel.js b/src/game-lib-raycast-wheel.js index 763cabf..7bb482d 100644 --- a/src/game-lib-raycast-wheel.js +++ b/src/game-lib-raycast-wheel.js @@ -18,7 +18,6 @@ GameLib.D3.RaycastWheel = function( deltaRotation, rollInfluence, maxSuspensionForce, - isFrontWheel, clippedInvContactDotSuspension, suspensionRelativeVelocity, suspensionForce, @@ -33,147 +32,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, @@ -192,7 +188,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, @@ -201,6 +196,6 @@ GameLib.D3.RaycastWheel.prototype.createInstance = function() { maxSuspensionTravel : this.maxSuspensionTravel, useCustomSlidingRotationalSpeed : this.useCustomSlidingRotationalSpeed, customSlidingRotationalSpeed : this.customSlidingRotationalSpeed - }); + }; }; diff --git a/src/game-lib-rigid-body.js b/src/game-lib-rigid-body.js index 46b815b..7e750b9 100644 --- a/src/game-lib-rigid-body.js +++ b/src/game-lib-rigid-body.js @@ -120,11 +120,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); } @@ -145,7 +145,7 @@ GameLib.D3.RigidBody.prototype.addShape = function( }; ///////////////////////// Methods to override ////////////////////////// -GameLib.D3.RigidBody.prototype.onLateUpdate = function( +GameLib.D3.RigidBody.prototype.onUpdate = function( deltaTime, parentEntity ) { @@ -153,40 +153,10 @@ GameLib.D3.RigidBody.prototype.onLateUpdate = function( 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() - // } - // } - - /*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); } - - - }; \ No newline at end of file diff --git a/src/game-lib-utils.js b/src/game-lib-utils.js index 6da527d..1e5754a 100644 --- a/src/game-lib-utils.js +++ b/src/game-lib-utils.js @@ -10,6 +10,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,