changes to path components

beta.r3js.org
polygonboutique 2016-11-24 13:07:42 +01:00
parent a1e9357a7a
commit 87d327cb4a
2 changed files with 168 additions and 8 deletions

View File

@ -0,0 +1,158 @@
/**
*
* @param id
* @param name
* @param sensorLength
* @constructor
*/
GameLib.D3.ComponentPathAI = function ComponentPathAI(
id,
name,
sensorLength
) {
this.id = id || GameLib.D3.Tools.RandomId();
if (typeof name == 'undefined') {
name = this.constructor.name;
}
this.name = name;
this.parentEntity = null;
this.sensorLength = sensorLength || 5;
GameLib.D3.Utils.Extend(GameLib.D3.ComponentPathAI, GameLib.D3.ComponentInterface);
//#ifdef RUNTIME__
this.sensors = [];
//#endif
};
//#ifdef RUNTIME__
///////////////////////// Methods to override //////////////////////////
GameLib.D3.ComponentPathAI.prototype.onUpdate = function(
deltaTime,
parentEntity
) {
var forward = false;
var backward = false;
var left = false;
var right = false;
/*if (this.keyForwardPressed) { // Forward [i]
this.pathFollowingComponent.direction = 1;
} else if (this.keyBackPressed){
this.pathFollowingComponent.direction = -1;
} else {
this.pathFollowingComponent.direction = 0;
}
// left right
if (this.keyLeftPressed) { // Left [j]
this.pathFollowingComponent.offset.x = 0;
this.pathFollowingComponent.offset.y = 0;
this.pathFollowingComponent.offset.z = 1;
} else if (this.keyRightPressed) { // Right [l]
this.pathFollowingComponent.offset.x = 0;
this.pathFollowingComponent.offset.y = 0;
this.pathFollowingComponent.offset.z = -1;
}*/
if(this.sensorVisualizer) {
for(var i = 0, i = this.sensors.length; i < l; ++i) {
var sensor = this.sensors[i];
if(!this.sensorVisualizer.sensors[i]) {
var emptyGeometry = new THREE.Geometry();
var sensorMesh = new THREE.Mesh(
emptyGeometry,
new THREE.MeshBasicMaterial(
{
color : sensor.sensorColor,
wireframe : true
}
)
);
this.sensorVisualizer.sensors[i] = {
sensorMesh : sensorMesh
};
sys.game.scenes["MainScene"].instance.add(this.sensorVisualizer.sensors[i].sensorMesh);
}
if(this.sensorVisualizer.sensors[i].arrow) {
this.sensorVisualizer.sensors[s].sensorMesh.remove(this.sensorVisualizer.sensors[i].arrow);
}
// create new
}
}
};
GameLib.D3.ComponentPathAI.prototype.onSetParentEntity = function(
parentScene,
parentEntity
) {
this.parentEntity = parentEntity;
this.pathFollowingComponent = parentEntity.getComponent(GameLib.D3.ComponentPathFollowing);
if(!this.pathFollowingComponent) {
console.error("ComponentPathAI. NO PATH FOLLOWING COMPONENT");
}
// Compute bounding box
if(!this.parentEntity.mesh.geometry.boundingBox) {
this.parentEntity.mesh.geometry.computeBoundingBox();
}
var boundingBox = this.parentEntity.mesh.geometry.boundingBox;
var sensorLength = this.sensorLength;
var sensorColor = new THREE.Color(1, 0, 0);
// Create sensors
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,
boundingBox.max.z * this.parentEntity.mesh.scale.z,
0
)
}
);
// debug code
this.sensorVisualizer = {
sensors : []
};
};
//#endif

View File

@ -35,7 +35,7 @@ GameLib.D3.ComponentPathFollowing = function ComponentPathFollowing(
this.accel = accel || 2.0;
this.baseOffset = baseOffset || new GameLib.D3.Vector3();
this.maxOffset = maxOffset || new GameLib.D3.Vector3(Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
this.maxOffset = maxOffset || new GameLib.D3.Vector3(10, 10, 10);
this.steeringSpeed = steeringSpeed || 1.0;
@ -71,7 +71,7 @@ GameLib.D3.ComponentPathFollowing.prototype.onUpdate = function(
// extract rotation
var forward = new THREE.Vector3(-1, 0, 0);
var axis = new THREE.Vector3();
axis.crossVectors(forward, rotation).normalize();
axis.crossVectors(forward, rotation).normalize();
var radians = Math.acos(forward.dot(rotation));
var quaternion = new THREE.Quaternion().setFromAxisAngle(axis, radians);
@ -83,18 +83,20 @@ GameLib.D3.ComponentPathFollowing.prototype.onUpdate = function(
{ // update position
var t = deltaTime * this.accel;
var t = deltaTime * ((this.currentSpeed / this.maxSpeed) * this.steeringSpeed); // put this into another variable
t = t * t * t * (t * (6.0 * t - 15.0) + 10.0);
var targetVector = new THREE.Vector3(
this.maxOffset.x * this.offset.x,
this.maxOffset.y * this.offset.y,
this.maxOffset.z * this.offset.z
);
var lerpedOffset = new THREE.Vector3(
this.currentOffset.x,
this.currentOffset.y,
this.currentOffset.z
).lerp(new THREE.Vector3(
this.maxOffset.x * this.offset.x,
this.maxOffset.y * this.offset.y,
this.maxOffset.z * this.offset.z
), t);
).lerp(targetVector, t);
this.currentOffset.x = lerpedOffset.x;
this.currentOffset.y = lerpedOffset.y;