class Eagle::Physics2D::World

Overview

A container that simulates bodies: gravity, collisions and contact resolution, plus spatial queries such as raycasts.

Defined in:

eagle/physics/world2d.cr

Instance Method Summary

Instance Method Detail

def add(type : BodyType, position : Vec2, shape : Shape) : Body #

Creates a body with one shape, adds it and returns it.


def add_body(b : Body) : Body #

Adds an existing body and returns it.


Pairs of bodies that started touching in the last step.


def bias_factor : Float32 #

How aggressively overlap is corrected each step, from 0 to 1. Too high looks jittery.


def bias_factor=(bias_factor : Float32) #

How aggressively overlap is corrected each step, from 0 to 1. Too high looks jittery.


def bodies : Array(Eagle::Physics2D::Body) #

Every body in the world.


def cell_size : Float32 #

Cell size of the broad-phase grid. Around twice the size of a typical body works well.


def cell_size=(cell_size : Float32) #

Cell size of the broad-phase grid. Around twice the size of a typical body works well.


def clear : Nil #

Removes every body.


def contact_begin : Eagle::Emitter(Body, Body) #

Emitted during a step when two bodies start touching.

world = Physics2D::World.new
world.on_contact_begin { |a, b| puts "#{a.tag} hit #{b.tag}" }

def contact_end : Eagle::Emitter(Body, Body) #

Emitted during a step when two bodies stop touching.


def emit_contact_begin(*args) : Nil #

Emitted during a step when two bodies start touching.

world = Physics2D::World.new
world.on_contact_begin { |a, b| puts "#{a.tag} hit #{b.tag}" }

def emit_contact_end(*args) : Nil #

Emitted during a step when two bodies stop touching.


Pairs of bodies that stopped touching in the last step.


def gravity : Vec2 #

Acceleration applied to dynamic bodies, in pixels per second squared.


def gravity=(gravity : Vec2) #

Acceleration applied to dynamic bodies, in pixels per second squared.


def iterations : Int32 #

Solver iterations per step. More iterations make stacks steadier but cost more time.


def iterations=(iterations : Int32) #

Solver iterations per step. More iterations make stacks steadier but cost more time.


def move_and_slide(body : Body, motion : Vec2, max_slides : Int32 = 4) : Array(Vec2) #

Moves a body by motion, sliding along obstacles. Returns the normals of the surfaces hit. KinematicBody2D#move_and_slide wraps this.


def on_contact_begin(&block : Body, Body -> Nil) : Proc(Body, Body, Nil) #

Emitted during a step when two bodies start touching.

world = Physics2D::World.new
world.on_contact_begin { |a, b| puts "#{a.tag} hit #{b.tag}" }

def on_contact_end(&block : Body, Body -> Nil) : Proc(Body, Body, Nil) #

Emitted during a step when two bodies stop touching.


def position_correction : Float32 #

Extra positional correction after each step. 0 relies on the velocity bias alone.


def position_correction=(position_correction : Float32) #

Extra positional correction after each step. 0 relies on the velocity bias alone.


def probe_floor(body : Body, up : Vec2, distance : Float32, max_angle : Float32) : Vec2 | Nil #

Looks for ground within distance below the body without moving it. Returns the floor normal if a surface within max_angle of up is found.


def query_point(p : Vec2, mask : UInt32 = 4294967295_u32) : Array(Body) #

Every body containing the point p.


def query_rect(r : Rect, mask : UInt32 = 4294967295_u32) : Array(Body) #

Every body whose bounds overlap r.


def query_shape(shape : Shape, mask : UInt32 = 4294967295_u32) : Array(Body) #

Every body overlapping a world-space shape, for area attacks and placement checks.


def raycast(origin : Vec2, direction : Vec2, max_distance : Number = 1e6, mask : UInt32 = 4294967295_u32, exclude : Body | Nil = nil) : RayHit | Nil #

--- queries ------------------------------------------------------------- Casts a ray and returns the closest hit, or nil. direction doesn't need to be normalized. Pass exclude to ignore one body, such as the one casting.


def remove_body(b : Body) : Nil #

Removes a body.


def slop : Float32 #

Overlap, in pixels, that is allowed before correction kicks in. It prevents jitter at rest.


def slop=(slop : Float32) #

Overlap, in pixels, that is allowed before correction kicks in. It prevents jitter at rest.


def step(dt : Float32) : Nil #

Advances the simulation by dt seconds. Use a fixed dt for stable results.