2015-04-02 12:38:06 +02:00
|
|
|
var BouncingCube = function(size, style) {
|
|
|
|
Cube.call(this, size, style);
|
|
|
|
|
|
|
|
this.fixed_center = new THREE.Vector3();
|
|
|
|
this.center = new THREE.Vector3();
|
|
|
|
|
|
|
|
this.speed = new THREE.Vector3(0,0,300);
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|
2015-04-02 12:38:06 +02:00
|
|
|
BouncingCube.prototype = Object.create(Cube.prototype);
|
|
|
|
BouncingCube.prototype.constructor = BouncingCube;
|
|
|
|
|
|
|
|
BouncingCube.prototype.update = function() {
|
|
|
|
// Compute new center
|
|
|
|
var speed_clone = this.speed.clone();
|
2015-04-28 16:47:44 +02:00
|
|
|
speed_clone.multiply(BouncingCube.DT);
|
2015-04-02 12:38:06 +02:00
|
|
|
|
2015-04-28 16:47:44 +02:00
|
|
|
this.speed.add(BouncingCube.G);
|
2015-04-02 12:38:06 +02:00
|
|
|
|
|
|
|
if (this.speed.dot(this.speed) > 100) {
|
|
|
|
this.center.add(speed_clone);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.center.z < 0) {
|
2015-04-28 16:47:44 +02:00
|
|
|
this.speed.multiply(BouncingCube.FRICTION);
|
2015-04-02 12:38:06 +02:00
|
|
|
this.center.z = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the mesh
|
|
|
|
this.mesh.position.set(this.center.x, this.center.y, this.center.z);
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|
2015-04-28 16:47:44 +02:00
|
|
|
|
|
|
|
// Static variables
|
|
|
|
BouncingCube.DT = new THREE.Vector3(0.1,0.1,0.1);
|
|
|
|
BouncingCube.FRICTION = new THREE.Vector3(1, 1, -0.5);
|
|
|
|
BouncingCube.G = new THREE.Vector3(0,0,-10);
|
|
|
|
|