Skip to content

Commit

Permalink
kinematic model of vehicle done! God bless AMR and Oriolo
Browse files Browse the repository at this point in the history
  • Loading branch information
dhorpynchenko committed Jan 10, 2019
1 parent 3c0d21a commit 7c12349
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 24 deletions.
111 changes: 110 additions & 1 deletion js/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ class Processor {
return levelParameters;
}

/**
*
* @param timeGone
*/
update(timeGone) {

}

reset() {

}

}

class Level {
Expand All @@ -51,4 +63,101 @@ class Item {
this.rotation = rotation;

}
}
}

Vehicle = (function () {

function Vehicle(maxSpeed, maxAcceleration, friction, maxSteeringSpeed, elementsCallback) {
this.maxSpeed = maxSpeed;
this.maxAcceleration = maxAcceleration;
this.friction = friction;
this.speed = 0;

// Wheels
this.angle = 0;
this.maxSteerSpeed = maxSteeringSpeed;
this.maxSteerAngle = Math.PI / 3; // 60 deg

this.body = null;
this.position = new THREE.Vector3();
this.length = 2.0;

let scope = this;
getTractor(function (object1) {
scope.body = object1;
elementsCallback(object1)
});

}

Vehicle.prototype = {
constructor: Vehicle,
metObstacle: function () {

},
update: function (forr, right, backw, left) {

if (this.body == null) {
return;
}

// Speed
if (forr || backw) {
let accel = this.maxAcceleration;
if (forr) {
accel = -accel;
}
this.speed = Math.max(-this.maxSpeed, Math.min(this.maxSpeed, this.speed + accel));
} else {
if (this.speed > 0) {
this.speed = Math.max(0, this.speed - this.friction);
} else {
this.speed = Math.min(0, this.speed + this.friction);
}
}

// Angle
if (left || right) {
let steeringAngle = this.maxSteerSpeed;
if (left) {
steeringAngle = -steeringAngle;
}
this.angle = Math.max(-this.maxSteerAngle, Math.min(this.maxSteerAngle, this.angle + steeringAngle));
} else {
if (this.angle > 0) {
this.angle = Math.max(0, this.angle - this.maxSteerSpeed);
} else {
this.angle = Math.min(0, this.angle + this.maxSteerSpeed);
}
}
// Calculating velocities. Using kinematic model of bicycle
let thettaSpeed = 0.5 * this.length * Math.tan(this.angle) * this.speed;
this.body.rotateY(thettaSpeed);

let v = new THREE.Vector3(1, 0, 0);
v.applyEuler(this.body.rotation);
v.projectOnPlane(new THREE.Vector3(0, 1, 0));

//console.log("%s %s %s %s", v.x, v.y, v.z, Math.atan2(v.z , v.x));
let thetta = Math.atan2(v.x, v.z);
thetta = -thetta;
if (thetta < 0) thetta += Math.PI * 2;
let xSpeed = Math.cos(thetta) * this.speed;
let ySpeed = Math.sin(thetta) * this.speed;

//console.log("Wheel angle %s, Thetta speed %s, thetta %s, xSpeed %s, ySpeed %s",
// this.angle.toFixed(3), thettaSpeed.toFixed(3), thetta.toFixed(3), xSpeed.toFixed(3), ySpeed.toFixed(3));

shift(this.body, xSpeed, 0, ySpeed);
this.position = this.body.position;
},
isIntersects: function (object) {

},
reset: function () {
this.position = new THREE.Vector3();
},

};
return Vehicle;
})();
39 changes: 17 additions & 22 deletions js/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ let camera;
let onRenderFunctions = [];
const debug = true;

let tractor;
let warehouse;
let processor;
let vehicle;

// Parameters
let fieldSize = [100, 100];
Expand All @@ -26,12 +25,6 @@ function iterate(obj, root) {
});
}

function shift(object, x = 0, y = 0, z = 0) {
object.position.x += x;
object.position.y += y;
object.position.z += z;
}

function init() {
processor = new Processor(5, fieldSize, haySize);
initScene();
Expand All @@ -43,13 +36,8 @@ function init() {
repeatX: fieldSize[0],
repeatY: fieldSize[1],
}));
getTractor(function (object) {
tractor = object;
if (tractor != null) {
scene.add(object);
}

//iterate(object, "_")
vehicle = new Vehicle(0.2, 0.001, 0.0005, 0.01, function (element) {
scene.add(element);
});


Expand All @@ -67,14 +55,23 @@ function init() {
// camera.lookAt(scene.position)
// });

if (debug)
if (debug) {
//The X axis is red. The Y axis is green. The Z axis is blue.
scene.add(new THREE.AxisHelper(100));
}

initRendering();
setupLevel();

let keyHandler = new KeyPressListener();

onRenderFunctions.push(function (delta, now) {
vehicle.update(keyHandler.isUpPressed(), keyHandler.isRightPressed(), keyHandler.isDownPressed(), keyHandler.isLeftPressed());
})
}

function setupLevel() {
//vehicle.reset();
let level = processor.generateLevel();
for (let i = 0; i < level.hays.length; i++) {
let item = level.hays[i];
Expand Down Expand Up @@ -104,12 +101,10 @@ function initScene() {
camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.01, 10000);
camera.position.z = 10; // Forward-backward
camera.position.x = 13; // Left-right
camera.position.y = 150; // Up-down
camera.position.y = 15; // Up-down

onRenderFunctions.push(function () {
if (tractor != null) {
camera.lookAt(tractor.position);
}
camera.lookAt(vehicle.position);
renderer.render(scene, camera);
})
}
Expand Down Expand Up @@ -157,8 +152,8 @@ function reset() {
}
obstacles.clear();

tractor.position.set(0, 0, 0);
vehicle.position.set(0, 0, 0);

}

window.addEventListener("load", init);
window.addEventListener("load", init);
52 changes: 51 additions & 1 deletion js/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,53 @@
function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}
}

KeyPressListener = (function () {
var leftKey = 'ArrowLeft', upKey = 'ArrowUp', rightKey = 'ArrowRight', downKey = 'ArrowDown';
var keystate = {};
document.addEventListener("keydown", function (e) {
keystate[e.key] = true;
//console.log(e.key);
});
document.addEventListener("keyup", function (e) {
delete keystate[e.key];
//console.log("Release " + e.key);
});

function isKeyPressed (key) {
return keystate[key];
}

function KeyPressListener() {

}

KeyPressListener.prototype = {

constructor: KeyPressListener,

isUpPressed: function () {
return isKeyPressed(upKey);
},

isDownPressed: function () {
return isKeyPressed(downKey);
},

isLeftPressed: function () {
return isKeyPressed(leftKey);
},

isRightPressed: function () {
return isKeyPressed(rightKey);
}
};

return KeyPressListener
})();

function shift(object, x = 0, y = 0, z = 0) {
object.position.x += x;
object.position.y += y;
object.position.z += z;
}

0 comments on commit 7c12349

Please sign in to comment.