⚠️ Beta State

PyBevy is in an early and experimental stage. The API is incomplete, subject to breaking changes without notice, and you should expect bugs. Many features are still under development.

2D Top Down Camera

A 2D camera that smoothly follows a player character.

Introduction

A top-down camera follows the player with smooth interpolation. Instead of snapping to the player's position, the camera eases toward it using exponential decay for a polished feel.

import math
from pybevy.prelude import *
 
PLAYER_SPEED = 100.0
CAMERA_DECAY_RATE = 2.0

Player Component

@component
class Player(Component):
    pass
 
 
@resource
class PlayerPosition(Resource):
    def __init__(self):
        self.x = 0.0
        self.y = 0.0

Setup

Create a background rectangle and a bright circle for the player.

def setup(
    commands: Commands,
    meshes: ResMut[Assets[Mesh]],
    materials: ResMut[Assets[ColorMaterial]],
) -> None:
    commands.spawn(
        Mesh2d(meshes.add(Rectangle(1000.0, 700.0).mesh())),
        MeshMaterial2d(materials.add(ColorMaterial(color=Color.srgb(0.2, 0.2, 0.3)))),
    )
    commands.spawn(
        Player(),
        Mesh2d(meshes.add(Circle(radius=25.0).mesh())),
        MeshMaterial2d(materials.add(ColorMaterial(color=Color.srgb(6.25, 9.4, 9.1)))),
        Transform.from_xyz(0.0, 0.0, 2.0),
    )
    commands.spawn(Camera2d())

Movement and Camera Systems

Move the player with WASD. The camera smoothly interpolates toward the player using exponential decay.

def move_player(
    query: Query[Mut[Transform], With[Player]],
    pos: ResMut[PlayerPosition],
    time: Res[Time],
    kb: Res[ButtonInput],
) -> None:
    for player in query:
        dx = 0.0
        dy = 0.0
        if kb.pressed(KeyCode.KeyW):
            dy += 1.0
        if kb.pressed(KeyCode.KeyS):
            dy -= 1.0
        if kb.pressed(KeyCode.KeyA):
            dx -= 1.0
        if kb.pressed(KeyCode.KeyD):
            dx += 1.0
 
        length = math.sqrt(dx * dx + dy * dy)
        if length > 0.0:
            dx /= length
            dy /= length
 
        player.translation.x += dx * PLAYER_SPEED * time.delta_secs()
        player.translation.y += dy * PLAYER_SPEED * time.delta_secs()
        pos.x = player.translation.x
        pos.y = player.translation.y
 
 
def update_camera(
    query: Query[Mut[Transform], With[Camera2d]],
    pos: Res[PlayerPosition],
    time: Res[Time],
) -> None:
    for camera in query:
        decay = 1.0 - math.exp(-CAMERA_DECAY_RATE * time.delta_secs())
        camera.translation.x += (pos.x - camera.translation.x) * decay
        camera.translation.y += (pos.y - camera.translation.y) * decay

Running the App

@entrypoint
def main(app: App) -> App:
    return (
        app
        .add_plugins(DefaultPlugins)
        .insert_resource(PlayerPosition())
        .add_systems(Startup, setup)
        .add_systems(Update, (move_player, update_camera))
    )
 
if __name__ == "__main__":
    main().run()

Running this example

Use PyBevy's hot reload feature to run and develop this example. If you don't have PyBevy installed, check out the Quick Start guide.

$pybevy watch 2d_top_down_camera.py

The code will reload automatically when you make changes to the file.


From Python to Rust

Notice how the core concepts in the code—Commands, Assets, App, and Systems—are identical to the original Bevy example?

This is the power of pybevy! It lets you learn Bevy's powerful, data-driven architecture in friendly Python.

When your project grows and you're ready for maximum, native performance, you'll already know the concepts to start writing systems in Bevy Engine with Rust.