ExCollision.World.Body (ExCollision v1.1.0)

View Source

Dynamic body in the collision world (AABB + metadata). Implements the Collidable protocol.

Player and other moving objects are Body in the world, not MapObject. MapObject — static objects from TMX (objectgroup); Body — dynamic bodies with velocity and interpolation.

Option :on_collision — function (world, body_id, collided_ids, hit_static) -> world, called on collision; can remove body (bullet), apply damage, etc. (world, body_id, collided_body_ids, hit_static) -> world world — current world body_id — id of the body that collided collided_body_ids — list of body ids that were hit (e.g. players) hit_static — true if collision with static (wall, etc.)

Summary

Functions

AABB center (for rendering)

Interpolated center position for smooth rendering. alpha in [0, 1]: 0 = previous frame, 1 = current frame. If previous_aabb is nil, returns current center.

Interpolated top-left corner (min_x, min_y). Options: :clamp — clamp alpha to [0, 1].

Set collision callback: (world, body_id, [body ids], hit_static) -> world

Clear velocity

Set body velocity (vx, vy) in pixels/sec

Types

collision_callback()

@type collision_callback() :: (world :: term(),
                         body_id :: term(),
                         collided_body_ids :: [term()],
                         hit_static :: boolean() ->
                           world :: term())

t()

@type t() :: %ExCollision.World.Body{
  aabb: ExCollision.Geometry.AABB.t(),
  data: term(),
  id: term(),
  on_collision: collision_callback() | nil,
  previous_aabb: ExCollision.Geometry.AABB.t() | nil,
  static: boolean(),
  velocity: {float(), float()} | nil
}

Functions

center(body)

AABB center (for rendering)

from_center(id, center_x, center_y, width, height, opts \\ [])

from_xywh(id, x, y, w, h, opts \\ [])

interpolated_center(body, alpha, opts \\ [])

Interpolated center position for smooth rendering. alpha in [0, 1]: 0 = previous frame, 1 = current frame. If previous_aabb is nil, returns current center.

Options:

  • :clamp — if true, clamps alpha to [0, 1] before interpolating (default false).

interpolated_position(body, alpha, opts \\ [])

Interpolated top-left corner (min_x, min_y). Options: :clamp — clamp alpha to [0, 1].

move(body, dx, dy)

new(id, aabb, opts \\ [])

position(body)

set_on_collision(body, callback)

Set collision callback: (world, body_id, [body ids], hit_static) -> world

set_velocity(body, atom)

Clear velocity

set_velocity(body, vx, vy)

Set body velocity (vx, vy) in pixels/sec