tiramisu/physics

Types

Physics body type

pub type BodyType {
  Dynamic
  Kinematic
  Fixed
}

Constructors

  • Dynamic

    Dynamic bodies are affected by forces and gravity

  • Kinematic

    Kinematic bodies can be moved programmatically but don’t respond to forces

  • Fixed

    Fixed (static) bodies don’t move

Collider shape

pub type ColliderShape {
  Box(width: Float, height: Float, depth: Float)
  Sphere(radius: Float)
  Capsule(half_height: Float, radius: Float)
  Cylinder(half_height: Float, radius: Float)
}

Constructors

  • Box(width: Float, height: Float, depth: Float)

    Box collider with half-extents

  • Sphere(radius: Float)

    Sphere collider with radius

  • Capsule(half_height: Float, radius: Float)

    Capsule collider (cylinder with rounded caps)

  • Cylinder(half_height: Float, radius: Float)

    Cylinder collider

Opaque handle to the Rapier physics world This is part of your Model and gets updated each frame

pub opaque type PhysicsWorld

Physics body configuration (immutable, declarative)

pub type RigidBody {
  RigidBody(
    body_type: BodyType,
    mass: option.Option(Float),
    restitution: Float,
    friction: Float,
    linear_damping: Float,
    angular_damping: Float,
    collider: ColliderShape,
    ccd_enabled: Bool,
  )
}

Constructors

  • RigidBody(
      body_type: BodyType,
      mass: option.Option(Float),
      restitution: Float,
      friction: Float,
      linear_damping: Float,
      angular_damping: Float,
      collider: ColliderShape,
      ccd_enabled: Bool,
    )

    Arguments

    body_type

    Type of rigid body

    mass

    Mass (only for Dynamic bodies)

    restitution

    Restitution (bounciness) 0.0 = no bounce, 1.0 = perfect bounce

    friction

    Friction coefficient

    linear_damping

    Linear damping (resistance to movement)

    angular_damping

    Angular damping (resistance to rotation)

    collider

    Collider shape

    ccd_enabled

    Enable continuous collision detection

Physics world configuration

pub type WorldConfig {
  WorldConfig(gravity: vec3.Vec3(Float))
}

Constructors

  • WorldConfig(gravity: vec3.Vec3(Float))

    Arguments

    gravity

    Gravity vector (typically Vec3(0.0, -9.81, 0.0))

Values

pub fn add_body(
  world: PhysicsWorld,
  id: String,
  body: RigidBody,
  initial_transform: transform.Transform,
) -> PhysicsWorld

Add a body to the physics world (internal use)

pub fn apply_force(id: String, force: vec3.Vec3(Float)) -> Nil

Apply a force to a rigid body (use in update function)

pub fn apply_impulse(
  id: String,
  impulse: vec3.Vec3(Float),
) -> Nil

Apply an impulse to a rigid body (use in update function)

pub fn enable_ccd(body: RigidBody) -> RigidBody

Enable continuous collision detection

pub fn get_transform(
  world: PhysicsWorld,
  id: String,
) -> Result(transform.Transform, Nil)

Get the current transform of a physics body Use this to sync physics transforms back to your scene graph

pub fn get_velocity(
  id: String,
) -> option.Option(vec3.Vec3(Float))

Get the velocity of a rigid body

pub fn new_world(config: WorldConfig) -> PhysicsWorld

Create a new physics world (call this in your init function)

pub fn register_body(
  world: PhysicsWorld,
  id: String,
  body: RigidBody,
  initial_transform: transform.Transform,
) -> PhysicsWorld

Register a physics body in the world Call this when you create a scene node with physics

pub fn remove_body(
  world: PhysicsWorld,
  id: String,
) -> PhysicsWorld

Remove a body from the physics world (internal use)

pub fn rigid_body(
  body_type: BodyType,
  collider: ColliderShape,
) -> RigidBody

Create a new rigid body with default settings

pub fn set_angular_damping(
  body: RigidBody,
  damping: Float,
) -> RigidBody

Set angular damping

pub fn set_friction(
  body: RigidBody,
  friction: Float,
) -> RigidBody

Set friction

pub fn set_linear_damping(
  body: RigidBody,
  damping: Float,
) -> RigidBody

Set linear damping

pub fn set_mass(body: RigidBody, mass: Float) -> RigidBody

Set mass for a rigid body

pub fn set_restitution(
  body: RigidBody,
  restitution: Float,
) -> RigidBody

Set restitution (bounciness)

pub fn set_velocity(
  id: String,
  velocity: vec3.Vec3(Float),
) -> Nil

Set the velocity of a rigid body (use in update function)

pub fn step(
  world: PhysicsWorld,
  delta_time: Float,
) -> PhysicsWorld

Step the physics simulation forward This should be called in your update function each frame Returns updated world with new transforms for all bodies

pub fn unregister_body(
  world: PhysicsWorld,
  id: String,
) -> PhysicsWorld

Unregister a physics body from the world

Search Document