pushed new components (ai + colorlerp)
parent
758bfe0a2b
commit
18ac3ac1c3
|
@ -0,0 +1,60 @@
|
||||||
|
GameLib.D3.ComponentColorLerp = function(
|
||||||
|
componentId,
|
||||||
|
startColor,
|
||||||
|
endColor,
|
||||||
|
lerpSpeed
|
||||||
|
) {
|
||||||
|
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.ComponentColorLerp, GameLib.D3.ComponentInterface);
|
||||||
|
|
||||||
|
this.startColor = startColor || new GameLib.D3.Vector3(0, 0, 0);
|
||||||
|
this.endColor = endColor || new GameLib.D3.Vector3(1, 1, 1);
|
||||||
|
this.lerpSpeed = lerpSpeed || 1.0;
|
||||||
|
|
||||||
|
this.lerpTarget = this.endColor;
|
||||||
|
};
|
||||||
|
|
||||||
|
///////////////////////// Methods to override //////////////////////////
|
||||||
|
GameLib.D3.ComponentColorLerp.prototype.onUpdate = function(
|
||||||
|
deltaTime,
|
||||||
|
parentEntity
|
||||||
|
) {
|
||||||
|
var t = deltaTime * this.lerpSpeed;
|
||||||
|
// t = t * t * t * (t * (6.0 * t - 15.0) + 10.0);
|
||||||
|
|
||||||
|
parentEntity.mesh.material.color.r = parentEntity.mesh.material.color.r + (this.endColor.x - parentEntity.mesh.material.color.r) * t;
|
||||||
|
parentEntity.mesh.material.color.g = parentEntity.mesh.material.color.g + (this.endColor.y - parentEntity.mesh.material.color.g) * t;
|
||||||
|
parentEntity.mesh.material.color.b = parentEntity.mesh.material.color.b + (this.endColor.z - parentEntity.mesh.material.color.b) * t;
|
||||||
|
|
||||||
|
|
||||||
|
/* if( parentEntity.mesh.material.color.r == this.endColor.x
|
||||||
|
&& parentEntity.mesh.material.color.g == this.endColor.y
|
||||||
|
&& parentEntity.mesh.material.color.b == this.endColor.z
|
||||||
|
) {
|
||||||
|
|
||||||
|
|
||||||
|
console.error("switch target");
|
||||||
|
//this.lerpTarget = this.startColor;
|
||||||
|
|
||||||
|
} /!*else if (parentEntity.mesh.material.color.r == this.startColor.x
|
||||||
|
&& parentEntity.mesh.material.color.g == this.startColor.y
|
||||||
|
&& parentEntity.mesh.material.color.b == this.startColor.z) {
|
||||||
|
|
||||||
|
this.lerpTarget = this.endColor;
|
||||||
|
}*!/*/
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
GameLib.D3.ComponentColorLerp.prototype.onSetParentEntity = function(
|
||||||
|
parentScene,
|
||||||
|
parentEntity
|
||||||
|
) {
|
||||||
|
parentEntity.mesh.material.color = new THREE.Color(
|
||||||
|
this.startColor.x,
|
||||||
|
this.startColor.y,
|
||||||
|
this.startColor.z
|
||||||
|
);
|
||||||
|
};
|
|
@ -0,0 +1,404 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param componentId
|
||||||
|
* @param physicsWorld
|
||||||
|
* @constructor
|
||||||
|
*/
|
||||||
|
GameLib.D3.ComponentVehicleAIObjectAvoidance = function(
|
||||||
|
componentId,
|
||||||
|
physicsWorld
|
||||||
|
) {
|
||||||
|
this.componentId = componentId || GameLib.D3.Tools.RandomId();
|
||||||
|
this.parentEntity = null;
|
||||||
|
GameLib.D3.Utils.Extend(GameLib.D3.ComponentVehicleAIObjectAvoidance, GameLib.D3.ComponentInterface);
|
||||||
|
|
||||||
|
this.raycastVehicleComponent = null;
|
||||||
|
this.physicsWorld = physicsWorld || null;
|
||||||
|
this.sensors = [];
|
||||||
|
|
||||||
|
// debug
|
||||||
|
this.debugArrows = {};
|
||||||
|
|
||||||
|
|
||||||
|
console.log("constructor for : ComponentVehicleAIObjectAvoidance");
|
||||||
|
};
|
||||||
|
|
||||||
|
/////////////////////////////////////////////////////////////////////////
|
||||||
|
///////////////////////// Methods to override ///////////////////////////
|
||||||
|
/////////////////////////////////////////////////////////////////////////
|
||||||
|
GameLib.D3.ComponentVehicleAIObjectAvoidance.prototype.onSetParentEntity = function(
|
||||||
|
parentScene,
|
||||||
|
parentEntity
|
||||||
|
) {
|
||||||
|
this.parentEntity = parentEntity;
|
||||||
|
this.raycastVehicleComponent = parentEntity.getComponent(GameLib.D3.RaycastVehicle);
|
||||||
|
|
||||||
|
console.log("onSetParentEntity for : ComponentVehicleAIObjectAvoidance");
|
||||||
|
|
||||||
|
|
||||||
|
if(!this.raycastVehicleComponent) {
|
||||||
|
console.warn("NO RAYCAST VEHICLE FOUND!");
|
||||||
|
}
|
||||||
|
|
||||||
|
// create sensors
|
||||||
|
|
||||||
|
var boundingBox = this.parentEntity.mesh.geometry.boundingBox;
|
||||||
|
// this is taken from the main.js.
|
||||||
|
// this should be configurable inside the editor
|
||||||
|
var carBoxScaleModifier = { x : 3 / 4, y : 1 / 2, z : 0.90 };
|
||||||
|
|
||||||
|
var sensorLength = 0.65;
|
||||||
|
var sensorColor = new THREE.Color(0, 0, 1);
|
||||||
|
|
||||||
|
// . . . . . . . . . . FRONT . . . . . . . . . .
|
||||||
|
|
||||||
|
// right
|
||||||
|
this.sensors.push(
|
||||||
|
{
|
||||||
|
sensorLength : sensorLength,
|
||||||
|
|
||||||
|
sensorColor : sensorColor,
|
||||||
|
|
||||||
|
sensorDirection : new THREE.Vector3(
|
||||||
|
1,
|
||||||
|
0,
|
||||||
|
0
|
||||||
|
).normalize(),
|
||||||
|
|
||||||
|
sensorPositionOffset : new THREE.Vector3(
|
||||||
|
boundingBox.max.x * this.parentEntity.mesh.scale.x * carBoxScaleModifier.x,
|
||||||
|
boundingBox.max.z * this.parentEntity.mesh.scale.z * carBoxScaleModifier.z,
|
||||||
|
boundingBox.max.y * this.parentEntity.mesh.scale.y * carBoxScaleModifier.y // this is still swapped with y, because cannon.
|
||||||
|
)
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
// left
|
||||||
|
this.sensors.push(
|
||||||
|
{
|
||||||
|
sensorLength : sensorLength,
|
||||||
|
|
||||||
|
sensorColor : sensorColor,
|
||||||
|
|
||||||
|
sensorDirection : new THREE.Vector3(
|
||||||
|
1,
|
||||||
|
0,
|
||||||
|
0
|
||||||
|
).normalize(),
|
||||||
|
|
||||||
|
sensorPositionOffset : new THREE.Vector3(
|
||||||
|
boundingBox.max.x * this.parentEntity.mesh.scale.x * carBoxScaleModifier.x,
|
||||||
|
-boundingBox.max.z * this.parentEntity.mesh.scale.z * carBoxScaleModifier.z,
|
||||||
|
boundingBox.max.y * this.parentEntity.mesh.scale.y * carBoxScaleModifier.y // this is still swapped with y, because cannon.
|
||||||
|
)
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
// center
|
||||||
|
this.sensors.push(
|
||||||
|
{
|
||||||
|
sensorLength : sensorLength,
|
||||||
|
|
||||||
|
sensorColor : sensorColor,
|
||||||
|
|
||||||
|
sensorDirection : new THREE.Vector3(
|
||||||
|
1,
|
||||||
|
0,
|
||||||
|
0
|
||||||
|
).normalize(),
|
||||||
|
|
||||||
|
sensorPositionOffset : new THREE.Vector3(
|
||||||
|
boundingBox.max.x * this.parentEntity.mesh.scale.x * carBoxScaleModifier.x,
|
||||||
|
0,
|
||||||
|
0
|
||||||
|
)
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
// . . . . . . DIAGONAL FRONT . . . . . . . .
|
||||||
|
// right
|
||||||
|
this.sensors.push(
|
||||||
|
{
|
||||||
|
sensorLength : sensorLength,
|
||||||
|
|
||||||
|
sensorColor : sensorColor,
|
||||||
|
|
||||||
|
sensorDirection : new THREE.Vector3(
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
0
|
||||||
|
).normalize(),
|
||||||
|
|
||||||
|
sensorPositionOffset : new THREE.Vector3(
|
||||||
|
boundingBox.max.x * this.parentEntity.mesh.scale.x * carBoxScaleModifier.x,
|
||||||
|
boundingBox.max.z * this.parentEntity.mesh.scale.z * carBoxScaleModifier.z,
|
||||||
|
boundingBox.max.y * this.parentEntity.mesh.scale.y * carBoxScaleModifier.y // this is still swapped with y, because cannon.
|
||||||
|
)
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
// left
|
||||||
|
this.sensors.push(
|
||||||
|
{
|
||||||
|
sensorLength : sensorLength,
|
||||||
|
|
||||||
|
sensorColor : sensorColor,
|
||||||
|
|
||||||
|
sensorDirection : new THREE.Vector3(
|
||||||
|
0,
|
||||||
|
-1,
|
||||||
|
0
|
||||||
|
).normalize(),
|
||||||
|
|
||||||
|
sensorPositionOffset : new THREE.Vector3(
|
||||||
|
boundingBox.max.x * this.parentEntity.mesh.scale.x * carBoxScaleModifier.x,
|
||||||
|
-boundingBox.max.z * this.parentEntity.mesh.scale.z * carBoxScaleModifier.z,
|
||||||
|
boundingBox.max.y * this.parentEntity.mesh.scale.y * carBoxScaleModifier.y // this is still swapped with y, because cannon.
|
||||||
|
)
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// right
|
||||||
|
this.sensors.push(
|
||||||
|
{
|
||||||
|
sensorLength : sensorLength,
|
||||||
|
|
||||||
|
sensorColor : sensorColor,
|
||||||
|
|
||||||
|
sensorDirection : new THREE.Vector3(
|
||||||
|
0.5,
|
||||||
|
0.5,
|
||||||
|
0
|
||||||
|
).normalize(),
|
||||||
|
|
||||||
|
sensorPositionOffset : new THREE.Vector3(
|
||||||
|
boundingBox.max.x * this.parentEntity.mesh.scale.x * carBoxScaleModifier.x,
|
||||||
|
boundingBox.max.z * this.parentEntity.mesh.scale.z * carBoxScaleModifier.z,
|
||||||
|
boundingBox.max.y * this.parentEntity.mesh.scale.y * carBoxScaleModifier.y // this is still swapped with y, because cannon.
|
||||||
|
)
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
// left
|
||||||
|
this.sensors.push(
|
||||||
|
{
|
||||||
|
sensorLength : sensorLength,
|
||||||
|
|
||||||
|
sensorColor : sensorColor,
|
||||||
|
|
||||||
|
sensorDirection : new THREE.Vector3(
|
||||||
|
0.5,
|
||||||
|
-0.5,
|
||||||
|
0
|
||||||
|
).normalize(),
|
||||||
|
|
||||||
|
sensorPositionOffset : new THREE.Vector3(
|
||||||
|
boundingBox.max.x * this.parentEntity.mesh.scale.x * carBoxScaleModifier.x,
|
||||||
|
-boundingBox.max.z * this.parentEntity.mesh.scale.z * carBoxScaleModifier.z,
|
||||||
|
boundingBox.max.y * this.parentEntity.mesh.scale.y * carBoxScaleModifier.y // this is still swapped with y, because cannon.
|
||||||
|
)
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// right
|
||||||
|
this.sensors.push(
|
||||||
|
{
|
||||||
|
sensorLength : sensorLength,
|
||||||
|
|
||||||
|
sensorColor : sensorColor,
|
||||||
|
|
||||||
|
sensorDirection : new THREE.Vector3(
|
||||||
|
0.75,
|
||||||
|
0.25,
|
||||||
|
0
|
||||||
|
).normalize(),
|
||||||
|
|
||||||
|
sensorPositionOffset : new THREE.Vector3(
|
||||||
|
boundingBox.max.x * this.parentEntity.mesh.scale.x * carBoxScaleModifier.x,
|
||||||
|
boundingBox.max.z * this.parentEntity.mesh.scale.z * carBoxScaleModifier.z,
|
||||||
|
boundingBox.max.y * this.parentEntity.mesh.scale.y * carBoxScaleModifier.y // this is still swapped with y, because cannon.
|
||||||
|
)
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
// left
|
||||||
|
this.sensors.push(
|
||||||
|
{
|
||||||
|
sensorLength : sensorLength,
|
||||||
|
|
||||||
|
sensorColor : sensorColor,
|
||||||
|
|
||||||
|
sensorDirection : new THREE.Vector3(
|
||||||
|
0.75,
|
||||||
|
-0.25,
|
||||||
|
0
|
||||||
|
).normalize(),
|
||||||
|
|
||||||
|
sensorPositionOffset : new THREE.Vector3(
|
||||||
|
boundingBox.max.x * this.parentEntity.mesh.scale.x * carBoxScaleModifier.x,
|
||||||
|
-boundingBox.max.z * this.parentEntity.mesh.scale.z * carBoxScaleModifier.z,
|
||||||
|
boundingBox.max.y * this.parentEntity.mesh.scale.y * carBoxScaleModifier.y // this is still swapped with y, because cannon.
|
||||||
|
)
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
console.log("pushed sensors", this.sensors.length);
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
GameLib.D3.ComponentVehicleAIObjectAvoidance.prototype.onUpdate = function(
|
||||||
|
deltaTime,
|
||||||
|
parentEntity
|
||||||
|
) {
|
||||||
|
if(this.raycastVehicleComponent && this.physicsWorld) {
|
||||||
|
var vehicleVelocity = this.parentEntity.getComponent(GameLib.D3.RigidBody).instance.velocity;
|
||||||
|
var vehicleVelocityLength = 12;
|
||||||
|
|
||||||
|
|
||||||
|
// shoot rays for each sensor & check collisions.
|
||||||
|
var world = this.physicsWorld.instance;
|
||||||
|
var result = new CANNON.RaycastResult();
|
||||||
|
|
||||||
|
for(var s = 0, l = this.sensors.length; s < l; ++s) {
|
||||||
|
var sensor = this.sensors[s];
|
||||||
|
|
||||||
|
var from = new THREE.Vector3(
|
||||||
|
this.parentEntity.position.x,
|
||||||
|
this.parentEntity.position.y,
|
||||||
|
this.parentEntity.position.z
|
||||||
|
).add(new THREE.Vector3(
|
||||||
|
sensor.sensorPositionOffset.x,
|
||||||
|
sensor.sensorPositionOffset.y,
|
||||||
|
sensor.sensorPositionOffset.z
|
||||||
|
).applyQuaternion(new THREE.Quaternion(
|
||||||
|
this.parentEntity.quaternion.x,
|
||||||
|
this.parentEntity.quaternion.y,
|
||||||
|
this.parentEntity.quaternion.z,
|
||||||
|
this.parentEntity.quaternion.w
|
||||||
|
)));
|
||||||
|
|
||||||
|
var fromC = new CANNON.Vec3(
|
||||||
|
from.x,
|
||||||
|
from.y,
|
||||||
|
from.z
|
||||||
|
);
|
||||||
|
|
||||||
|
var to = new THREE.Vector3(
|
||||||
|
sensor.sensorDirection.x,
|
||||||
|
sensor.sensorDirection.y,
|
||||||
|
sensor.sensorDirection.z
|
||||||
|
).applyQuaternion(new THREE.Quaternion(
|
||||||
|
this.parentEntity.quaternion.x,
|
||||||
|
this.parentEntity.quaternion.y,
|
||||||
|
this.parentEntity.quaternion.z,
|
||||||
|
this.parentEntity.quaternion.w
|
||||||
|
)).normalize().multiplyScalar(sensor.sensorLength * vehicleVelocityLength);
|
||||||
|
|
||||||
|
|
||||||
|
var toC = new CANNON.Vec3(
|
||||||
|
from.x + to.x,
|
||||||
|
from.y + to.y,
|
||||||
|
from.z + to.z
|
||||||
|
);
|
||||||
|
|
||||||
|
world.raycastClosest(
|
||||||
|
fromC,
|
||||||
|
|
||||||
|
toC,
|
||||||
|
|
||||||
|
{
|
||||||
|
collisionFilterMask : 2 // check only group 2 (track)
|
||||||
|
},
|
||||||
|
|
||||||
|
result
|
||||||
|
);
|
||||||
|
|
||||||
|
if(result.hasHit) {
|
||||||
|
sensor.sensorColor = new THREE.Color(1, 0, 0);
|
||||||
|
} else {
|
||||||
|
sensor.sensorColor = new THREE.Color(0, 0, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// draw sensors
|
||||||
|
{
|
||||||
|
this.debugArrows.sensors = this.debugArrows.sensors || [];
|
||||||
|
|
||||||
|
for(var s = 0, l = this.sensors.length; s < l; ++s) {
|
||||||
|
|
||||||
|
if(!this.debugArrows.sensors[s]) {
|
||||||
|
this.debugArrows.sensors[s] = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!this.debugArrows.sensors[s].mesh) {
|
||||||
|
|
||||||
|
var geometry = new THREE.Geometry();
|
||||||
|
var mesh = new THREE.Mesh(
|
||||||
|
geometry,
|
||||||
|
new THREE.MeshBasicMaterial(
|
||||||
|
{
|
||||||
|
color : 0x000000,
|
||||||
|
wireframe : true
|
||||||
|
}
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
this.debugArrows.sensors[s].mesh = mesh;
|
||||||
|
sys.game.scenes["MainScene"].instance.add(this.debugArrows.sensors[s].mesh);
|
||||||
|
}
|
||||||
|
|
||||||
|
// remove old arrow, if we have one
|
||||||
|
if(this.debugArrows.sensors[s].arrow) {
|
||||||
|
this.debugArrows.sensors[s].mesh.remove(this.debugArrows.sensors[s].arrow);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
var sensor = this.sensors[s];
|
||||||
|
var sensorLength = sensor.sensorLength; // should get this from the sensor itself
|
||||||
|
|
||||||
|
this.debugArrows.sensors[s].arrow = new THREE.ArrowHelper(
|
||||||
|
new THREE.Vector3(
|
||||||
|
sensor.sensorDirection.x,
|
||||||
|
sensor.sensorDirection.y,
|
||||||
|
sensor.sensorDirection.z
|
||||||
|
).applyQuaternion(new THREE.Quaternion(
|
||||||
|
this.parentEntity.quaternion.x,
|
||||||
|
this.parentEntity.quaternion.y,
|
||||||
|
this.parentEntity.quaternion.z,
|
||||||
|
this.parentEntity.quaternion.w
|
||||||
|
)).normalize(),
|
||||||
|
|
||||||
|
new THREE.Vector3(
|
||||||
|
this.parentEntity.position.x,
|
||||||
|
this.parentEntity.position.y,
|
||||||
|
this.parentEntity.position.z
|
||||||
|
).add(new THREE.Vector3(
|
||||||
|
sensor.sensorPositionOffset.x,
|
||||||
|
sensor.sensorPositionOffset.y,
|
||||||
|
sensor.sensorPositionOffset.z
|
||||||
|
).applyQuaternion(new THREE.Quaternion(
|
||||||
|
this.parentEntity.quaternion.x,
|
||||||
|
this.parentEntity.quaternion.y,
|
||||||
|
this.parentEntity.quaternion.z,
|
||||||
|
this.parentEntity.quaternion.w
|
||||||
|
))),
|
||||||
|
|
||||||
|
sensorLength * vehicleVelocityLength,
|
||||||
|
|
||||||
|
sensor.sensorColor
|
||||||
|
);
|
||||||
|
|
||||||
|
this.debugArrows.sensors[s].mesh.add(this.debugArrows.sensors[s].arrow);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
|
@ -0,0 +1,52 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param componentId
|
||||||
|
* @param targetEntity GameLib.D3.Entity
|
||||||
|
* @param nodePath
|
||||||
|
* @constructor
|
||||||
|
*/
|
||||||
|
GameLib.D3.ComponentVehicleAIPathBoid = function(
|
||||||
|
componentId,
|
||||||
|
targetEntity,
|
||||||
|
nodePath
|
||||||
|
) {
|
||||||
|
this.componentId = componentId || GameLib.D3.Tools.RandomId();
|
||||||
|
this.parentEntity = null;
|
||||||
|
GameLib.D3.Utils.Extend(GameLib.D3.ComponentVehicleAIPathBoid, GameLib.D3.ComponentInterface);
|
||||||
|
|
||||||
|
this.targetEntity = targetEntity;
|
||||||
|
this.raycastVehicleComponent = null;
|
||||||
|
this.nodePath = nodePath;
|
||||||
|
this.debugArrows = {};
|
||||||
|
};
|
||||||
|
|
||||||
|
/////////////////////////////////////////////////////////////////////////
|
||||||
|
///////////////////////// Methods to override ///////////////////////////
|
||||||
|
/////////////////////////////////////////////////////////////////////////
|
||||||
|
GameLib.D3.ComponentVehicleAIPathBoid.prototype.onSetParentEntity = function(
|
||||||
|
parentScene,
|
||||||
|
parentEntity
|
||||||
|
) {
|
||||||
|
this.parentEntity = parentEntity;
|
||||||
|
this.raycastVehicleComponent = parentEntity.getComponent(GameLib.D3.RaycastVehicle);
|
||||||
|
|
||||||
|
if(!this.raycastVehicleComponent) {
|
||||||
|
console.warn("NO RAYCAST VEHICLE FOUND!");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
GameLib.D3.ComponentVehicleAIPathBoid.prototype.onUpdate = function(
|
||||||
|
deltaTime,
|
||||||
|
parentEntity
|
||||||
|
) {
|
||||||
|
if(this.targetEntity && this.raycastVehicleComponent && this.nodePath) {
|
||||||
|
|
||||||
|
// boid code.
|
||||||
|
|
||||||
|
var currentNodeIndex = this.nodePath.indexOf(this.targetEntity);
|
||||||
|
var distanceToCurrentNode = this.parentEntity.position.distanceTo(this.targetEntity.position);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
|
@ -0,0 +1,236 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param componentId
|
||||||
|
* @param targetEntity GameLib.D3.Entity
|
||||||
|
* @param steeringSpeed
|
||||||
|
* @param maxSteerAngle
|
||||||
|
* @constructor
|
||||||
|
*/
|
||||||
|
GameLib.D3.ComponentVehicleAIPathSteering = function(
|
||||||
|
componentId,
|
||||||
|
targetEntity,
|
||||||
|
steeringSpeed,
|
||||||
|
maxSteerAngle
|
||||||
|
) {
|
||||||
|
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.ComponentVehicleAIPathSteering, GameLib.D3.ComponentInterface);
|
||||||
|
|
||||||
|
//
|
||||||
|
this.targetEntity = targetEntity;
|
||||||
|
this.steeringSpeed = steeringSpeed || 2.5;
|
||||||
|
this.maxSteerAngle = maxSteerAngle || 0.5;
|
||||||
|
|
||||||
|
this.raycastVehicleComponent = null;
|
||||||
|
|
||||||
|
this.debugArrows = {};
|
||||||
|
};
|
||||||
|
|
||||||
|
///////////////////////// Methods to override //////////////////////////
|
||||||
|
|
||||||
|
GameLib.D3.ComponentVehicleAIPathSteering.prototype.onSetParentEntity = function(
|
||||||
|
parentScene,
|
||||||
|
parentEntity
|
||||||
|
) {
|
||||||
|
this.raycastVehicleComponent = parentEntity.getComponent(GameLib.D3.RaycastVehicle);
|
||||||
|
this.parentEntity = parentEntity;
|
||||||
|
|
||||||
|
if(!this.raycastVehicleComponent) {
|
||||||
|
console.warn("NO RAYCAST VEHICLE FOUND!");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
GameLib.D3.ComponentVehicleAIPathSteering.prototype.onUpdate = function(
|
||||||
|
deltaTime,
|
||||||
|
parentEntity
|
||||||
|
) {
|
||||||
|
|
||||||
|
if(this.targetEntity && this.raycastVehicleComponent) {
|
||||||
|
|
||||||
|
var v1 = new THREE.Vector3(
|
||||||
|
this.parentEntity.position.x,
|
||||||
|
this.parentEntity.position.y,
|
||||||
|
this.parentEntity.position.z
|
||||||
|
);
|
||||||
|
|
||||||
|
var v2 = new THREE.Vector3(
|
||||||
|
this.targetEntity.position.x,
|
||||||
|
this.targetEntity.position.y,
|
||||||
|
this.targetEntity.position.z
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
// get forward vector of the car.
|
||||||
|
var v3 = new THREE.Vector3(
|
||||||
|
1,
|
||||||
|
0,
|
||||||
|
0
|
||||||
|
);
|
||||||
|
|
||||||
|
var q1 = new THREE.Quaternion(
|
||||||
|
this.parentEntity.quaternion.x,
|
||||||
|
this.parentEntity.quaternion.y,
|
||||||
|
this.parentEntity.quaternion.z,
|
||||||
|
this.parentEntity.quaternion.w
|
||||||
|
);
|
||||||
|
|
||||||
|
var q2 = new THREE.Quaternion(
|
||||||
|
this.targetEntity.quaternion.x,
|
||||||
|
this.targetEntity.quaternion.y,
|
||||||
|
this.targetEntity.quaternion.z,
|
||||||
|
this.targetEntity.quaternion.w
|
||||||
|
);
|
||||||
|
|
||||||
|
var v1ToV2 = v2.sub(v1);
|
||||||
|
var forward = v3.applyQuaternion(q1).normalize();
|
||||||
|
|
||||||
|
var v1v2cpy = new THREE.Vector3(
|
||||||
|
v1ToV2.x,
|
||||||
|
v1ToV2.y,
|
||||||
|
v1ToV2.z
|
||||||
|
).applyQuaternion(q2).normalize();
|
||||||
|
|
||||||
|
v1v2cpy.y = 0;
|
||||||
|
v1v2cpy = v1v2cpy.normalize();
|
||||||
|
|
||||||
|
var forwardcpy = new THREE.Vector3(
|
||||||
|
forward.x,
|
||||||
|
0,
|
||||||
|
forward.z
|
||||||
|
).normalize();
|
||||||
|
|
||||||
|
var cos = v1v2cpy.dot(forwardcpy);
|
||||||
|
var angleRadians = Math.acos(cos);
|
||||||
|
var steerAngleModifier = GameLib.D3.Vector3.AngleDirection(
|
||||||
|
forwardcpy,
|
||||||
|
v1v2cpy,
|
||||||
|
new GameLib.D3.Vector3(
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
0
|
||||||
|
)
|
||||||
|
);
|
||||||
|
var steerAngle = Math.min(angleRadians, this.maxSteerAngle) * steerAngleModifier;
|
||||||
|
|
||||||
|
if(isNaN(steerAngle)) {
|
||||||
|
console.log("NOT A NUMBER", steerAngle);
|
||||||
|
}
|
||||||
|
|
||||||
|
// check if forward & movedirection are pointing in two different directions
|
||||||
|
/* if(cos < 0) {
|
||||||
|
console.log("movedirection & forward are pointing in different directions");
|
||||||
|
}*/
|
||||||
|
|
||||||
|
//if(steerAngle > 0.002 || steerAngle < -0.002) {
|
||||||
|
this.raycastVehicleComponent.instance.setSteeringValue(steerAngle, 0);
|
||||||
|
this.raycastVehicleComponent.instance.setSteeringValue(steerAngle, 1);
|
||||||
|
//}
|
||||||
|
|
||||||
|
this.raycastVehicleComponent.instance.applyEngineForce(-2000, 2);
|
||||||
|
this.raycastVehicleComponent.instance.applyEngineForce(-2000, 3);
|
||||||
|
|
||||||
|
|
||||||
|
// - - - - - - - - - - - DEBUG ARROW 1 - - - - - - - - -
|
||||||
|
{
|
||||||
|
if(!this.debugArrows.v1) {
|
||||||
|
this.debugArrows.v1 = this.debugArrows.v1 || {};
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!this.debugArrows.v1.mesh) {
|
||||||
|
|
||||||
|
var geometryTHREE = new THREE.Geometry();
|
||||||
|
var wireframeMesh = new THREE.Mesh(
|
||||||
|
geometryTHREE,
|
||||||
|
new THREE.MeshBasicMaterial(
|
||||||
|
{
|
||||||
|
color: 0xff0000,
|
||||||
|
wireframe: true,
|
||||||
|
opacity: 1
|
||||||
|
}
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
this.debugArrows.v1.mesh = wireframeMesh;
|
||||||
|
|
||||||
|
sys.game.scenes["MainScene"].instance.add(this.debugArrows.v1.mesh);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(this.debugArrows.v1.arrow) {
|
||||||
|
this.debugArrows.v1.mesh.remove(this.debugArrows.v1.arrow);
|
||||||
|
}
|
||||||
|
|
||||||
|
var len = v1ToV2.length();
|
||||||
|
this.debugArrows.v1.arrow = new THREE.ArrowHelper(
|
||||||
|
v1ToV2.normalize(),
|
||||||
|
|
||||||
|
new THREE.Vector3(
|
||||||
|
this.parentEntity.position.x,
|
||||||
|
this.parentEntity.position.y,
|
||||||
|
this.parentEntity.position.z
|
||||||
|
),
|
||||||
|
|
||||||
|
len || 100,
|
||||||
|
|
||||||
|
new THREE.Color(
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
0
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
this.debugArrows.v1.mesh.add( this.debugArrows.v1.arrow );
|
||||||
|
}
|
||||||
|
|
||||||
|
// - - - - - - - - - - - DEBUG ARROW 2 - - - - - - - - -
|
||||||
|
{
|
||||||
|
if(!this.debugArrows.v2) {
|
||||||
|
this.debugArrows.v2 = this.debugArrows.v2 || {};
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!this.debugArrows.v2.mesh) {
|
||||||
|
|
||||||
|
var geometryTHREE = new THREE.Geometry();
|
||||||
|
var wireframeMesh = new THREE.Mesh(
|
||||||
|
geometryTHREE,
|
||||||
|
new THREE.MeshBasicMaterial(
|
||||||
|
{
|
||||||
|
color: 0xff0000,
|
||||||
|
wireframe: true,
|
||||||
|
opacity: 1
|
||||||
|
}
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
this.debugArrows.v2.mesh = wireframeMesh;
|
||||||
|
|
||||||
|
sys.game.scenes["MainScene"].instance.add(this.debugArrows.v2.mesh);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(this.debugArrows.v2.arrow) {
|
||||||
|
this.debugArrows.v2.mesh.remove(this.debugArrows.v2.arrow);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.debugArrows.v2.arrow = new THREE.ArrowHelper(
|
||||||
|
forward,
|
||||||
|
|
||||||
|
new THREE.Vector3(
|
||||||
|
this.parentEntity.position.x,
|
||||||
|
this.parentEntity.position.y,
|
||||||
|
this.parentEntity.position.z
|
||||||
|
),
|
||||||
|
|
||||||
|
12,
|
||||||
|
|
||||||
|
new THREE.Color(
|
||||||
|
1.0,
|
||||||
|
0.0,
|
||||||
|
0.0
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
this.debugArrows.v2.mesh.add( this.debugArrows.v2.arrow );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
|
@ -26,8 +26,7 @@ GameLib.D3.Game.prototype.processPhysics = function (
|
||||||
|
|
||||||
GameLib.D3.Game.prototype.render = function(
|
GameLib.D3.Game.prototype.render = function(
|
||||||
dt,
|
dt,
|
||||||
renderer,
|
renderer
|
||||||
camera
|
|
||||||
) {
|
) {
|
||||||
for(var s in this.scenes) {
|
for(var s in this.scenes) {
|
||||||
var scene = this.scenes[s];
|
var scene = this.scenes[s];
|
||||||
|
|
|
@ -98,6 +98,28 @@ GameLib.D3.Vector3.prototype.lerp = function ( v, alpha ) {
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
GameLib.D3.Vector3.prototype.distanceTo = function(v) {
|
||||||
|
var dx = this.x - v.x,
|
||||||
|
dy = this.y - v.y,
|
||||||
|
dz = this.z - v.z;
|
||||||
|
|
||||||
|
return Math.sqrt(dx * dx + dy * dy + dz * dz);
|
||||||
|
};
|
||||||
|
|
||||||
|
GameLib.D3.Vector3.AngleDirection = function(forward, directionToCheck, up) {
|
||||||
|
var perp = forward.cross(directionToCheck);
|
||||||
|
var dir = perp.dot(up);
|
||||||
|
|
||||||
|
if (dir > 0.0) {
|
||||||
|
return 1.0;
|
||||||
|
} else if (dir < 0.0) {
|
||||||
|
return -1.0;
|
||||||
|
} else {
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
GameLib.D3.Vector3.prototype.multiply = function (s) {
|
GameLib.D3.Vector3.prototype.multiply = function (s) {
|
||||||
if (s instanceof GameLib.D3.Vector3) {
|
if (s instanceof GameLib.D3.Vector3) {
|
||||||
this.x *= s.x;
|
this.x *= s.x;
|
||||||
|
|
|
@ -837,7 +837,7 @@ GameLib.D3.World.prototype.fixupTriangleMeshShape = function(
|
||||||
|
|
||||||
|
|
||||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||||
// Decide if we want to create new rigid bodies, or create a compound mesh
|
// Decide if we want to create new rigiwyd bodies, or create a compound mesh
|
||||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||||
|
|
||||||
for(var e in bFaceIndexed) {
|
for(var e in bFaceIndexed) {
|
||||||
|
@ -850,6 +850,10 @@ GameLib.D3.World.prototype.fixupTriangleMeshShape = function(
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
var body = new GameLib.D3.RigidBody(this.engine, 0, 12);
|
var body = new GameLib.D3.RigidBody(this.engine, 0, 12);
|
||||||
|
|
||||||
|
//TODO: this is just a hack.
|
||||||
|
body.instance.collisionFilterGroup = 1 | 2; // puts this body in two groups.
|
||||||
|
|
||||||
body.addShape(shape);
|
body.addShape(shape);
|
||||||
this.addRigidBody(body);
|
this.addRigidBody(body);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue