Commit 85912666 authored by Ebru Cam's avatar Ebru Cam

Update CarController.js

parent 54fbdf7d
......@@ -99,79 +99,122 @@ function CarController(mapbox) {
*/
self.updateDestination = function (carID, mapbox) {
var car = CARS[carID];
var data = CARS[carID].dataBuffer;
if(!data) return;
if(mapbox){
var destination = [CARS[carID].dataBuffer.posX, CARS[carID].dataBuffer.posY,0];
}else {
var destination = data.position;
destination = new THREE.Vector3(destination.x, CARS[carID].model.root.position.y, destination.z);
if (data.velocity) {
var vel = data.velocity;
var velocityAtDestination = Math.sqrt(vel.x * vel.x + vel.y * vel.y + vel.z * vel.z);
// m/s in km/h
velocityAtDestination = velocityAtDestination * 3.6;
if(!mapbox) CARS[carID].model.speed = Math.sqrt(vel.x * vel.x + vel.y * vel.y + vel.z * vel.z);
else CARS[carID].speed = velocityAtDestination;
}
}
if (data.acceleration) {
var acc = data.acceleration;
var accAtDestination = Math.sqrt(acc.x * acc.x + acc.y * acc.y + acc.z * acc.z);
if(!mapbox) CARS[carID].model.acceleration = Math.sqrt(acc.x * acc.x + acc.y * acc.y + acc.z * acc.z);
else CARS[carID].speed = accAtDestination;
}
if (data.velocity) {
var vel = data.velocity;
var velocityAtDestination = Math.sqrt(vel.x * vel.x + vel.y * vel.y + vel.z * vel.z);
// m/s in km/h
velocityAtDestination = velocityAtDestination * 3.6;
if(!mapbox) CARS[carID].model.speed = Math.sqrt(vel.x * vel.x + vel.y * vel.y + vel.z * vel.z);
else CARS[carID].speed = velocityAtDestination;
}
var rotationAroundYAxis = 0.0;
if (data.acceleration) {
var acc = data.acceleration;
var accAtDestination = Math.sqrt(acc.x * acc.x + acc.y * acc.y + acc.z * acc.z);
if(!mapbox) CARS[carID].model.acceleration = Math.sqrt(acc.x * acc.x + acc.y * acc.y + acc.z * acc.z);
else CARS[carID].speed = accAtDestination;
}
if (data.rotation) {
var rotation = data.rotation;
var lastOrientation = new THREE.Vector3(0, 1, 0);
var matrixColumnElements = rotation.reduce(function(acc, val){ return acc.concat(val)}, []);
// Manually compute matrix * vector operation, three JS just seems to support pre-multiplications (vector * matrix)
// Everything computed in the coordinate system of the server (z is up / down)
var xRot = matrixColumnElements[0] * lastOrientation.x + matrixColumnElements[3] * lastOrientation.y + matrixColumnElements[6] * lastOrientation.z;
var yRot = matrixColumnElements[1] * lastOrientation.x + matrixColumnElements[4] * lastOrientation.y + matrixColumnElements[7] * lastOrientation.z;
var zRot = matrixColumnElements[2] * lastOrientation.x + matrixColumnElements[5] * lastOrientation.y + matrixColumnElements[8] * lastOrientation.z;
var orientationVector = new THREE.Vector3(xRot, yRot, zRot);
var angle = orientationVector.angleTo(lastOrientation);
//If the vector points to quadrant I or IV, ensure value range goes up to 2*pi
if (orientationVector.x < 0) {
angle = -angle;
angle += 2 * Math.PI;
}
rotationAroundYAxis = angle;
var rotationAroundYAxis = 0.0;
if (data.rotation) {
var rotation = data.rotation;
var lastOrientation = new THREE.Vector3(0, 1, 0);
var matrixColumnElements = rotation.reduce(function(acc, val){ return acc.concat(val)}, []);
// Manually compute matrix * vector operation, three JS just seems to support pre-multiplications (vector * matrix)
// Everything computed in the coordinate system of the server (z is up / down)
var xRot = matrixColumnElements[0] * lastOrientation.x + matrixColumnElements[3] * lastOrientation.y + matrixColumnElements[6] * lastOrientation.z;
var yRot = matrixColumnElements[1] * lastOrientation.x + matrixColumnElements[4] * lastOrientation.y + matrixColumnElements[7] * lastOrientation.z;
var zRot = matrixColumnElements[2] * lastOrientation.x + matrixColumnElements[5] * lastOrientation.y + matrixColumnElements[8] * lastOrientation.z;
var orientationVector = new THREE.Vector3(xRot, yRot, zRot);
var angle = orientationVector.angleTo(lastOrientation);
//If the vector points to quadrant I or IV, ensure value range goes up to 2*pi
if(mapbox){
if (orientationVector.x < 0) {
angle = -angle;
angle += 2 * Math.PI;
}
}
else {
if (orientationVector.x > 0) {
angle = -angle;
angle += 2 * Math.PI;
}
CARS[carID].targetStatus.steeringAtDestination = CARS[carID].dataBuffer.steeringAngle;
CARS[carID].targetStatus.velocityAtDestination = velocityAtDestination;
CARS[carID].targetStatus.accAtDestination = accAtDestination;
CARS[carID].targetStatus.rotationAroundYAxis = rotationAroundYAxis;
CARS[carID].targetStatus.destination = destination;
}else {
var destination = data.position;
destination = new THREE.Vector3(destination.x, CARS[carID].model.root.position.y, destination.z);
if (data.velocity) {
var vel = data.velocity;
var velocityAtDestination = Math.sqrt(vel.x * vel.x + vel.y * vel.y + vel.z * vel.z);
// m/s in km/h
velocityAtDestination = velocityAtDestination * 3.6;
if (!mapbox) CARS[carID].model.speed = Math.sqrt(vel.x * vel.x + vel.y * vel.y + vel.z * vel.z);
else CARS[carID].speed = velocityAtDestination;
}
rotationAroundYAxis = angle;
/*
console.log('Rotation: matrixColumnElements ', matrixColumnElements);
console.log('Rotation: lastOrientation ', lastOrientation);
console.log('Rotation: orientationVector ', orientationVector);
console.log('Rotation: rotationAroundYAxis ', rotationAroundYAxis);
*/
}
if (data.acceleration) {
var acc = data.acceleration;
var accAtDestination = Math.sqrt(acc.x * acc.x + acc.y * acc.y + acc.z * acc.z);
if (!mapbox) CARS[carID].model.acceleration = Math.sqrt(acc.x * acc.x + acc.y * acc.y + acc.z * acc.z);
else CARS[carID].speed = accAtDestination;
}
CARS[carID].targetStatus.steeringAtDestination = CARS[carID].dataBuffer.steeringAngle;
CARS[carID].targetStatus.velocityAtDestination = velocityAtDestination;
CARS[carID].targetStatus.accAtDestination = accAtDestination;
CARS[carID].targetStatus.rotationAroundYAxis = rotationAroundYAxis;
CARS[carID].targetStatus.destination = destination;
// CARS[carID].distanceToDestination = Number.MAX_VALUE;
//self.logCarState(carID);
var rotationAroundYAxis = 0.0;
if (data.rotation) {
var rotation = data.rotation;
var lastOrientation = new THREE.Vector3(0, 1, 0);
var matrixColumnElements = rotation.reduce(function (acc, val) {
return acc.concat(val)
}, []);
// Manually compute matrix * vector operation, three JS just seems to support pre-multiplications (vector * matrix)
// Everything computed in the coordinate system of the server (z is up / down)
var xRot = matrixColumnElements[0] * lastOrientation.x + matrixColumnElements[3] * lastOrientation.y + matrixColumnElements[6] * lastOrientation.z;
var yRot = matrixColumnElements[1] * lastOrientation.x + matrixColumnElements[4] * lastOrientation.y + matrixColumnElements[7] * lastOrientation.z;
var zRot = matrixColumnElements[2] * lastOrientation.x + matrixColumnElements[5] * lastOrientation.y + matrixColumnElements[8] * lastOrientation.z;
var orientationVector = new THREE.Vector3(xRot, yRot, zRot);
var angle = orientationVector.angleTo(lastOrientation);
//If the vector points to quadrant I or IV, ensure value range goes up to 2*pi
if (orientationVector.x > 0) {
angle = -angle;
angle += 2 * Math.PI;
}
rotationAroundYAxis = angle;
/*
console.log('Rotation: matrixColumnElements ', matrixColumnElements);
console.log('Rotation: lastOrientation ', lastOrientation);
console.log('Rotation: orientationVector ', orientationVector);
console.log('Rotation: rotationAroundYAxis ', rotationAroundYAxis);
*/
}
CARS[carID].targetStatus.steeringAtDestination = CARS[carID].dataBuffer.steeringAngle;
CARS[carID].targetStatus.velocityAtDestination = velocityAtDestination;
CARS[carID].targetStatus.accAtDestination = accAtDestination;
CARS[carID].targetStatus.rotationAroundYAxis = rotationAroundYAxis;
CARS[carID].targetStatus.destination = destination;
// CARS[carID].distanceToDestination = Number.MAX_VALUE;
//self.logCarState(carID);
}
}
/*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment