Class: Physics

Physics(accValue, maxSpeed, decValue)

The Physics class performs position-velocity-acceleration Vector related calculations for more advanced Sprite movement.
For example, can set speed and motion angle of a sprite with constant velocity (no deceleration), or player character can accelerate at an angle and automatically decelerate at a fixed rate.
Similar to the Rectangle class design, the Physics position Vector can be set to reference the position of a Sprite, which keeps them synchronized.
A Physics object needs to be updated once per frame; for Sprites, this is handled by their update method.

Constructor

new Physics(accValue, maxSpeed, decValue)

Initialize position, velocity, and acceleration vectors, and set acceleration, maximum speed, and deceleration values.
Parameters:
Name Type Description
accValue number default magnitude of acceleration when using accelerateAtAngle
maxSpeed number maximum speed: if speed is ever above this amount, it will be reduced to this amount
decValue number when not accelerating, object will decelerate (decrease speed) by this amount
Source:

Methods

accelerateAtAngle(angleDegrees)

Accelerate this object in the given direction.
(Uses the constant acceleration value specified in constructor.)
Parameters:
Name Type Description
angleDegrees number the direction of acceleration
Source:

acceleratePercentAtAngle(percent, angleDegrees)

Accelerate this object by a multiple of base acceleration value in the given direction.
Parameters:
Name Type Description
percent number percentage of acceleration value (specified in constructor) to apply
angleDegrees number the direction of acceleration
Source:

getMotionAngle() → {number}

Get the current angle of motion (in degrees) of this object.
(The angle is measured from the positive x-axis, and angles increase in clockwise direction, since positive y-axis is down.)
Source:
Returns:
current angle of motion
Type
number

getSpeed() → {number}

Get the current speed of this object (pixels/second).
Source:
Returns:
current speed
Type
number

setMotionAngle(angleDegrees)

Set the angle of motion (in degrees) for this object (without changing the current speed).
(The angle is measured from the positive x-axis, and angles increase in clockwise direction, since positive y-axis is down.)
Parameters:
Name Type Description
angleDegrees number the new angle of motion
Source:

setSpeed(speed)

Set the speed (pixels/second) for this object (without changing the current angle of motion).
Parameters:
Name Type Description
speed number the new speed
Source:

update()

Update the values for position, velocity, and acceleration vectors.
This method should be called once per frame (automatically handled for Sprite objects).
Source: