Game Development Reference
In-Depth Information
And now, finally, we can implement the player's update method:
Player.prototype.update = (function() {
var halfAccel = new THREE.Vector3();
var scaledVelocity = new THREE.Vector3();
return function(delta) {
var r = this._aggregateRotation
.multiplyScalar(delta)
.add(this.rotation);
r.x = Math.max(Math.PI * -0.5, Math.min(Math.PI * 0.5, r.x));
this.rotation.x = 0;
if (this.moveDirection.FORWARD) this.velocity.z -= Player.SPEED;
if (this.moveDirection.LEFT) this.velocity.x -= Player.SPEED;
if (this.moveDirection.BACKWARD) this.velocity.z += Player.SPEED;
if (this.moveDirection.RIGHT) this.velocity.x += Player.SPEED;
halfAccel.copy(this.acceleration).multiplyScalar(delta * 0.5);
this.velocity.add(halfAccel);
var squaredVelocity = this.velocity.x*this.velocity.x +
this.velocity.z*this.velocity.z;
if (squaredVelocity > Player.SPEED*Player.SPEED) {
var scalar = Player.SPEED / Math.sqrt(squaredVelocity);
this.velocity.x *= scalar;
this.velocity.z *= scalar;
}
scaledVelocity.copy(this.velocity).multiplyScalar(delta);
this.translateX(scaledVelocity.x);
this.translateZ(scaledVelocity.z);
this.position.y += scaledVelocity.y;
this.velocity.add(halfAccel);
this.velocity.add(scaledVelocity.multiply(
this.ambientFriction
));
this.rotation.set(r.x, r.y, r.z);
this._aggregateRotation.set(0, 0, 0);
};
})();
 
Search WWH ::




Custom Search