Game Development Reference
In-Depth Information
We still have the maxAcceleration, maxSpeed, and maxSteering
constants, but the values have changed some. Like the previous
example, these values are determined through experimentation
and are completely subject to change depending on what kind of
handling you want the car to have. The two other major changes
are that the speed value has been replaced with velocity and is
now of type Vector3D. Acceleration keeps its name but is also a
Vector3D. These changes obviously affect their getter/setter
functions.
public function get velocity():Vector3D {
return _velocity;
}
public function set velocity(value:Vector3D):void {
_velocity = value;
if (_velocity.length
maxSpeed) {
var overage:Number = (_velocity.length - maxSpeed) /
maxSpeed;
_velocity.scaleBy(1 / (1 + overage));
>
}
if (_velocity.length < stoppingThreshold) {
_velocity.x = _velocity.y = 0;
}
}
public function get acceleration():Vector3D {
return _acceleration;
}
public function set acceleration(value:Vector3D):void {
_acceleration = value;
}
While the acceleration functions are not much different from
you would expect, the velocity setter has changed significantly. In
order to enforce a top speed and the stopping threshold, we must
measure the length of the vector, which is another term for its
magnitude. If the length property is greater than the top speed, we
scale the entire vector by the amount of the overage. This will
adjust the x and y properties of the vector in a single line instead
of having to do them separately. If the length property is less than
thestoppingthreshold,wealsosetthe x and y properties to 0. We
could have also scaled the vector by 0, but a simple variable
assignment is less overhead than performing calculations on all the
properties of the vector. Next let
s look at the changes to the Game
class. Only the readInput and moveVehicle methods have changed,
so that
'
'
s all we
'