Skip to content
Edit this page

Physics & Tilemaps

Pite includes a pure-Rust 2D physics subsystem built on rapier2d (running at a fixed 60 Hz rate, dt = 1/60) and a chunked TileMap2D API.


2D Physics

Physics Nodes

  • StaticBody2D: Non-moving collision body (floors, walls).
  • CharacterBody2D: Kinematic character controller with move_and_slide().
  • RigidBody2D: Dynamic simulation body affected by gravity and forces.
  • Area2D: Trigger zones with entry/exit overlap signals.
  • CollisionShape2D: Child of any physics body or area defining its collider (shape: "box", "circle", "capsule").

Character Movement

class Player(pite.CharacterBody2D):
    speed: float = 200.0

    def _physics_process(self, delta: float):
        if pite.held.right:
            self.velocity.x = self.speed
        elif pite.held.left:
            self.velocity.x = -self.speed
        else:
            self.velocity.x = 0.0

        self.velocity.y += 980.0 * delta

        if pite.pressed.space and self.is_on_floor:
            self.velocity.y = -400.0

        self.move_and_slide()
  • move_and_slide() -> bool: Advances the body along velocity * dt using sliding collision resolution (ignoring sensor areas). Updates velocity, position, and contact flags.
  • is_on_floor: bool, is_on_wall: bool, is_on_ceiling: bool: Contact flags relative to up_direction (default (0.0, -1.0)). These are attributes, not methods (if self.is_on_floor:).

RigidBody2D Dynamics

class Ball(pite.RigidBody2D):
    def _ready(self):
        self.apply_impulse((0.0, -200.0))
  • apply_impulse((x, y)): Instantaneous momentum change scaled by mass.
  • apply_force((x, y)): Continuous force application over the current step.
  • Properties: mass: float, gravity_scale: float, lock_rotation: bool.

Area2D Triggers

class Coin(pite.Area2D):
    def _ready(self):
        self.body_entered.connect(self.on_body)

    def on_body(self, body_id: str):
        print(f"Collected by {body_id}")

Built-in signals: body_entered(body_id: str), body_exited(body_id: str), area_entered(area_id: str), area_exited(area_id: str).

Spatial Queries and Raycasts

hit = pite.raycast((x, y), (target_x, target_y), mask=1)
if hit is not None:
    print(f"Hit {hit['body']} at {hit['point']} with normal {hit['normal']}")

Returns None if no solid obstacle intersects, or a dict with point ((float, float)), normal ((float, float)), distance (float), collider (str node ID), and body (str node ID).


Tilemaps

class Level(pite.TileMap2D):
    def _ready(self):
        tile = self.get_cell(0, 0, layer=0)
        self.set_cell(0, 0, "grass", layer=0)
        self.erase_cell(0, 0, layer=0)
        self.clear(layer=None)

        col, row = self.world_to_map(pite.mouse())
        pos = self.map_to_world(col, row)

        tile_w, tile_h = self.tile_size
        layers = self.layers
        cells = self.get_used_cells(layer=0)
  • tile_size -> tuple[int, int]: Tile dimensions (width, height) in pixels.
  • layers -> list[str]: List of layer names in the tilemap.
  • get_cell(col, row, layer=0) -> str | None: Returns the atlas frame name at (col, row).
  • set_cell(col, row, frame, layer=0): Places an atlas frame at (col, row).
  • erase_cell(col, row, layer=0) -> bool: Erases the cell at (col, row).
  • clear(layer=None): Clears all cells in layer (or all layers if omitted).
  • world_to_map(pos_or_x, y=None) -> tuple[int, int]: Converts world position to grid cell (col, row).
  • map_to_world(col, row) -> Vec2: Converts grid cell (col, row) to world center position.
  • get_used_cells(layer=0) -> list[tuple[int, int]]: Returns populated (col, row) coordinates.