Game Development Reference
In-Depth Information
Because child objects' position, rotation, and scaling are relative to the
parent's position, rotation, and scaling, you could create a third-person
camera by positioning the camera on the positive z axis (and probably a
little higher on the y axis) and giving the
player
object a geometry and
material (remember,
Player
inherits from
Mesh
, so you can instantiate
a player with
new Player(geometry, material)
).
Player movement
We'll finish off the code to look around in a moment, but because it's closely related
to the player's movement, let's address that first.
Physical movement
Getting movement right is a complex topic. Although simply adding a constant
velocity to an object's position can work for some simulations, more advanced games
will want to have acceleration at play (for example, for gravity) and potentially other
forces as well. The most straightforward approach to linear forces is to keep track of
acceleration and velocity vectors and add them together:
// When the mesh is instantiated
mesh.velocity = new THREE.Vector3(0, 0, 0);
mesh.acceleration = new THREE.Vector3(0, 0, 0);
// Called in the animation loop
function update(delta) {
// Apply acceleration
mesh.velocity.add(mesh.acceleration().clone().
multiplyScalar(delta));
// Apply velocity
mesh.position.add(mesh.velocity.clone().multiplyScalar(delta));
}