Module reference

Graphics/Physics module

Functions, objects and constants exposed by the Graphics/Physics module.

Graphics Physics module

Provides generic motion operations for objects used with the Graphics module.

Module functions

  • integrate

    Advances a motion state using acceleration and elapsed time.

    Graphics::Physics::integrate(state, acceleration, delta)

    The motion state is:

    [x, y, velocity_x, velocity_y]

    The acceleration is:

    [acceleration_x, acceleration_y]
  • apply_gravity

    Applies downward gravity to a motion state.

    Graphics::Physics::apply_gravity(state, gravity, delta)

    The positive vertical direction is upward, so gravity decreases velocity_y.

  • bounce

    Keeps a moving body inside a rectangular boundary and reflects its velocity when it reaches an edge.

    Graphics::Physics::bounce(state, bounds, restitution, radius)

    The bounds are specified as:

    [left, right, bottom, top]

    The restitution value must be between 0 and 1. A value of 1 preserves the full velocity, while lower values make the object lose energy during each bounce.

Motion state

Physics functions use the following motion-state format:

[x, y, velocity_x, velocity_y]

Example

$ball = [-0.72, 0.82, 0.72, 0]
$ball = Graphics::Physics::apply_gravity($ball, 1.25, 0.033)
$ball = Graphics::Physics::bounce($ball, [-1, 1, -1, 1], 0.78, 0.035)

The returned list is the updated motion state and can be passed into the next frame.

Return to the Graphics module documentation

Module functions

The following functions are available in this module.