class Eagle::KinematicBody2D

Overview

A body you move from code that still collides: players, enemies and moving platforms.

Set #velocity and call #move_and_slide in #physics_process. The body slides along walls and floors instead of stopping dead, and #on_floor? tells you when it can jump.

class Player < KinematicBody2D
  def ready : Nil
    box(20, 32)
  end

  def physics_process(dt : Float32) : Nil
    self.velocity += v2(0, 1400 * dt) # gravity
    self.velocity = v2(Input.axis("left", "right") * 220, velocity.y)
    self.velocity = v2(velocity.x, -520) if Input.pressed?("jump") && on_floor?
    move_and_slide(dt)
  end
end

Defined in:

eagle/nodes/physics_nodes.cr

Constructors

Instance Method Summary

Instance methods inherited from class Eagle::CollisionObject2D

add_shape(s : Physics2D::Shape) : self add_shape, body : Physics2D::Body body, body_entered : Eagle::Emitter(CollisionObject2D) body_entered, body_exited : Eagle::Emitter(CollisionObject2D) body_exited, box(w : Number, h : Number, offset : Vec2 = Vec2::ZERO) : self box, circle(radius : Number, offset : Vec2 = Vec2::ZERO) : self circle, debug=(debug : Bool) debug=, debug? : Bool debug?, draw(g : Graphics) : Nil draw, emit_body_entered(*args) : Nil emit_body_entered, emit_body_exited(*args) : Nil emit_body_exited, enter_tree : Nil enter_tree, exit_tree : Nil exit_tree, layer : UInt32 layer, layer=(v : UInt32) layer=, mask : UInt32 mask, mask=(v : UInt32) mask=, on_body_entered(&block : CollisionObject2D -> Nil) : Proc(CollisionObject2D, Nil) on_body_entered, on_body_exited(&block : CollisionObject2D -> Nil) : Proc(CollisionObject2D, Nil) on_body_exited, overlapping : Array(CollisionObject2D) overlapping, overlaps?(other : CollisionObject2D) : Bool overlaps?, physics_process(dt : Float32) : Nil physics_process, polygon(points : Array(Vec2)) : self polygon, pull_transform : Nil pull_transform, push_transform : Nil push_transform, world : Physics2D::World world, world=(world : Physics2D::World) world=

Constructor methods inherited from class Eagle::CollisionObject2D

new(type : Physics2D::BodyType, name : String = "", position : Vec2 = Vec2::ZERO, shape : Physics2D::Shape | Nil = nil) new

Instance methods inherited from class Eagle::Node2D

direction_to(other : Node2D) : Vec2 direction_to, distance_to(other : Node2D) : Float32 distance_to, down : Vec2 down, forward : Vec2 forward, global_position : Vec2 global_position, global_position=(p : Vec2) global_position=, global_rotation : Float32 global_rotation, global_scale : Vec2 global_scale, global_transform : Transform2D global_transform, look_at(target : Vec2) : Nil look_at, modulate : Color modulate, modulate=(modulate : Color) modulate=, parent_2d : Node2D | Nil parent_2d, position : Vec2 position, position=(p : Vec2) position=, right : Vec2 right, rotate(rad : Number) : Nil rotate, rotation : Float32 rotation, rotation=(rotation : Float32) rotation=, rotation_degrees : Float32 rotation_degrees, rotation_degrees=(d : Number) rotation_degrees=, scale : Vec2 scale, scale=(s : Vec2)
scale=(s : Number)
scale=
, to_global(local : Vec2) : Vec2 to_global, to_local(global : Vec2) : Vec2 to_local, top_level=(top_level : Bool) top_level=, top_level? : Bool top_level?, transform : Transform2D transform, transform=(t : Transform2D) transform=, translate(x : Number, y : Number) : Nil
translate(v : Vec2) : Nil
translate
, x : Float32 x, x=(v : Number) x=, y : Float32 y, y=(v : Number) y=

Constructor methods inherited from class Eagle::Node2D

new(name : String = "", position : Eagle::Vec2 = Vec2::ZERO, rotation : Float32 = 0_f32, scale : Eagle::Vec2 = Vec2::ONE) new

Instance methods inherited from class Eagle::Node

<<(child : Node) : self <<, [](path : String) : Node [], []?(path : String) : Node | Nil []?, add(child : Node) : Node
add(*children : Node) : Nil
add
, add_child(child : Node) : Node add_child, add_to_group(group : String) : self add_to_group, ancestor_of?(node : Node) : Bool ancestor_of?, can_process? : Bool can_process?, child?(name : String) : Node | Nil child?, child_added : Eagle::Emitter(Node) child_added, child_count : Int32 child_count, child_removed : Eagle::Emitter(Node) child_removed, children : Array(Eagle::Node) children, children_of(type : T.class) : Array(T) forall T children_of, clear_children : Nil clear_children, draw(g : Graphics) : Nil draw, dump(io : IO = STDOUT, indent = 0) : Nil dump, each_ancestor(& : Node -> ) : Nil each_ancestor, each_child(& : Node -> ) : Nil each_child, each_descendant(&block : Node -> ) : Nil each_descendant, emit_child_added(*args) : Nil emit_child_added, emit_child_removed(*args) : Nil emit_child_removed, emit_tree_entered(*args) : Nil emit_tree_entered, emit_tree_exited(*args) : Nil emit_tree_exited, enter_tree : Nil enter_tree, exit_tree : Nil exit_tree, find(name : String) : Node | Nil find, find_all(type : T.class) : Array(T) forall T find_all, first_child : Node | Nil first_child, free : Nil free, get_node(path : String, type : T.class) : T forall T
get_node(path : String) : Node
get_node
, get_node?(path : String) : Node | Nil get_node?, groups : Set(String) groups, in_group?(group : String) : Bool in_group?, in_tree? : Bool in_tree?, input(event : Event) : Nil input, name : String name, name=(name : String) name=, on_child_added(&block : Node -> Nil) : Proc(Node, Nil) on_child_added, on_child_removed(&block : Node -> Nil) : Proc(Node, Nil) on_child_removed, on_tree_entered(&block : -> Nil) : Proc(Nil) on_tree_entered, on_tree_exited(&block : -> Nil) : Proc(Nil) on_tree_exited, parent : Node | Nil parent, path : String path, physics_process(dt : Float32) : Nil physics_process, process(dt : Float32) : Nil process, process_mode : Eagle::Node::ProcessMode process_mode, process_mode=(process_mode : Eagle::Node::ProcessMode) process_mode=, queue_free : Nil queue_free, queued_free? : Bool queued_free?, ready : Nil ready, ready_called? : Bool ready_called?, remove(child : Node) : Nil remove, remove_child(child : Node) : Nil remove_child, remove_from_group(group : String) : Nil remove_from_group, remove_from_parent : Nil remove_from_parent, resized(width : Int32, height : Int32) : Nil resized, root : Node root, to_s(io : IO) : Nil to_s, tree_entered : Eagle::Emitter() tree_entered, tree_exited : Eagle::Emitter() tree_exited, visible=(visible : Bool) visible=, visible? : Bool visible?, z_index : Int32 z_index, z_index=(z_index : Int32) z_index=

Constructor methods inherited from class Eagle::Node

new(name : String = "") new

Constructor Detail

def self.new(name : String = "", position : Vec2 = Vec2::ZERO, shape : Physics2D::Shape | Nil = nil) #

Creates a kinematic body.


Instance Method Detail

def floor_max_angle : Float32 #

Steepest slope that still counts as floor, in radians. About 46° by default.


def floor_max_angle=(floor_max_angle : Float32) #

Steepest slope that still counts as floor, in radians. About 46° by default.


def floor_normal : Vec2 #

The normal of the floor under the body, for slope-aware movement.


def floor_snap : Float32 #

How far below the body to look for ground each move, so #on_floor? stays true while resting.


def floor_snap=(floor_snap : Float32) #

How far below the body to look for ground each move, so #on_floor? stays true while resting.


def move_and_collide(motion : Vec2) : Vec2 | Nil #

Moves by motion and stops at the first obstacle, without sliding. Returns the hit normal, or nil if the path was clear. Good for bullets and pucks that bounce.


def move_and_slide(dt : Float32, velocity : Vec2 | Nil = nil) : Vec2 #

Moves by velocity * dt, sliding along anything in the way. Velocity into a surface is removed, so falling onto a floor zeroes the vertical speed. Updates #on_floor?, #on_wall? and #on_ceiling?. Returns the new velocity.


def on_ceiling? : Bool #

True when the last move hit a ceiling.


def on_floor? : Bool #

True when the last move ended on a floor, meaning a surface facing #up_direction within #floor_max_angle.


def on_wall? : Bool #

True when the last move hit a wall.


def physics_process(dt : Float32) : Nil #

Keeps the body in sync with the node.


def up_direction : Vec2 #

Which way is up, used to tell floors from ceilings. Screen up by default.


def up_direction=(up_direction : Vec2) #

Which way is up, used to tell floors from ceilings. Screen up by default.


def velocity : Vec2 #

Velocity in pixels per second, used by #move_and_slide.


def velocity=(velocity : Vec2) #

Velocity in pixels per second, used by #move_and_slide.