Game Development Reference
In-Depth Information
protected function readInput():void {
vehicle.acceleration = new Vector3D();
if (_upPressed) {
vehicle.acceleration.x += Math.cos(vehicle.angle) *
Vehicle.maxAcceleration * Time.deltaTime
vehicle.acceleration.y += Math.sin(vehicle.angle) *
Vehicle.maxAcceleration * Time.deltaTime;
if (_downPressed) {
vehicle.acceleration.x += -Math.cos(vehicle.angle) *
Vehicle.maxAcceleration * Time.deltaTime
vehicle.acceleration.y += -Math.sin(vehicle.angle) *
Vehicle.maxAcceleration * Time.deltaTime;
if (_rightPressed) vehicle.angle += (Vehicle.maxSteering *
(vehicle.velocity.length / Vehicle.maxSpeed));
if (_leftPressed) vehicle.angle -= (Vehicle.maxSteering *
(vehicle.velocity.length / Vehicle.maxSpeed));
if (_spacePressed) {
At the onset of the readInput method, we create a new, empty
vector object for acceleration. If the up or down arrows are
pressed, the vector
s x and y components are adjusted accordingly.
If neither is pressed, the acceleration is empty and will have no
effect when combined with the velocity. If the space bar is pressed,
the velocity is scaled down by the amount of vehicle
s hand-brake
protected function moveVehicle():void {
vehicle.velocity = vehicle.velocity.add(vehicle.
vehicle.x += vehicle.velocity.x * Time.deltaTime;
vehicle.y += vehicle.velocity.y * Time.deltaTime;
the velocity down. We then combine the existing velocity vector
with the new acceleration vector. Another way to combine the two
would have been the Vector3D incrementBy method. It adds the
two relevant vectors without returning a new object. However, in
our case, assigning the result back to the velocity property of the
vehicle forces it through the maxSpeed check we looked at earlier.
If we used incrementBy method, we would have to do that check
manually here. Finally, to adjust the x and y positions of the
Search Nedrilad ::

Custom Search