PRACTICAL RECIPES · BUILT FROM REPO EXAMPLES

Guides by intent

Start with what you want to build. Each guide links to the repository folder or engine source and uses the same APIs and patterns as the project code.

2D

Build a 2D game

2D

Draw a 2D scene

Start a frame, draw rectangles or batched quads, and present the result.

UBE · WASI 0.2
game.begin_frame()
arena {
    game.draw_rect(80.0, 90.0, 32.0, 32.0, color::green())
    game.present()
}
2D

Use a 2D camera

Convert world positions to screen positions and pan or zoom your view.

UBE · WASI 0.2
let camera = render::camera_new(800.0, 600.0)
let screen_pos = camera.world_to_screen(player_pos)
game.draw_rect(screen_pos.x, screen_pos.y, 24.0, 24.0, color::yellow())
2D

Build a platformer

Combine input edges, gravity, jump timing, and AABB collision resolution.

UBE · WASI 0.2
let wants_jump = controls.is_key_pressed(input::key_space())
if controls.is_key_down(input::key_left()) { player.vx = -speed }
let hit = physics2d::check_aabb_xy(
    player.x, player.y, player.w, player.h,
    ground.x, ground.y, ground.w, ground.h
)
if hit.has_collision { player.y -= hit.normal.y * hit.depth }
2D

Add collision queries

Use overlap tests, collision manifolds, and the spatial hash broadphase.

UBE · WASI 0.2
let hit = physics2d::check_aabb_xy(ax, ay, aw, ah, bx, by, bw, bh)
if hit.has_collision {
    ax -= hit.normal.x * hit.depth
    ay -= hit.normal.y * hit.depth
}
2D

Upload and draw a texture

Upload tightly packed RGBA8 pixels, bind the returned texture ID, and map its UV range onto a batched quad.

UBE · WASI 0.2
let pixels = []u8{
    255, 64, 64, 255,  64, 255, 64, 255,
    64, 64, 255, 255,  255, 255, 255, 255
}
let texture_id = platform::create_texture(2, 2, pixels)

let mut batch = render::batcher_new(1)
batch.draw_quad(100.0, 80.0, 128.0, 128.0,
    0.0, 0.0, 1.0, 1.0, color::white())
platform::bind_texture(texture_id)
platform::submit(batch.vertices)
platform::present(0.04, 0.04, 0.06)

3D

Render a 3D world

3D

Render a 3D scene

Set a perspective camera, load primitive meshes, and submit transform batches.

UBE · WASI 0.2
let camera = render3d::camera3d_new(eye, target, 800.0 / 600.0)
platform3d::set_camera(camera.view_projection(), camera.position)
let mut models = []math::Mat4{}
models.push(math::mat4_translate(0.0, 0.0, 0.0))
platform3d::draw_instances(cube_mesh, models)
3D

Build custom meshes

Create vertex and index buffers in Ube, upload them, then draw instances.

UBE · WASI 0.2
let mesh_id = platform3d::upload_mesh(vertices, indices)
let mut models = []math::Mat4{}
models.push(math::mat4_mul(
    math::mat4_translate(x, y, z),
    math::mat4_scale(sx, sy, sz)
))
platform3d::draw_instances(mesh_id, models)

CAMERA

Choose a camera behavior

Camera

Control a first-person camera

Derive the forward vector from yaw and pitch, clamp pitch, and move on the ground plane.

UBE · WASI 0.2
pitch = clamp_f32(pitch, -1.48, 1.48)
let cos_p = math::cos(pitch)
let forward = math::vec3(
    math::sin(yaw) * cos_p, math::sin(pitch),
    0.0 - math::cos(yaw) * cos_p
)
let target = eye.add(forward)
let camera = render3d::camera3d_new(eye, target, aspect)
Camera

Follow a player in third person

Orbit around the target with yaw, pitch, and boom distance; smooth the camera toward its desired position.

UBE · WASI 0.2
let offset = math::vec3(
    math::sin(orbit_yaw) * math::cos(orbit_pitch) * orbit_dist,
    math::sin(orbit_pitch) * orbit_dist,
    math::cos(orbit_yaw) * math::cos(orbit_pitch) * orbit_dist
)
let desired_eye = player_pos.add(offset)
camera_eye = lerp_vec3(camera_eye, desired_eye, clamp_f32(9.0 * dt, 0.0, 1.0))
let camera = render3d::camera3d_new(camera_eye, player_pos, aspect)
Camera

Make an isometric strategy camera

Pan relative to camera heading, rotate in fixed steps, and zoom around a ground focal point.

UBE · WASI 0.2
let pan_forward = math::vec3(math::sin(cam_yaw), 0.0, 0.0 - math::cos(cam_yaw)).normalized()
let pan_right = math::vec3(math::cos(cam_yaw), 0.0, math::sin(cam_yaw)).normalized()
if game.input.is_key_pressed(input::key_q()) { cam_yaw -= 0.785398 }
if game.input.is_key_pressed(input::key_e()) { cam_yaw += 0.785398 }
Camera

Add cinematic shake and spring lag

Decay shake trauma over time and use a clamped interpolation factor for spring-like camera follow.

UBE · WASI 0.2
shake_trauma = clamp_f32(shake_trauma - 1.4 * dt, 0.0, 1.0)
let shake_power = shake_trauma * shake_trauma
let shake = math::vec3(math::sin(time * 47.0), math::cos(time * 53.0), math::sin(time * 61.0)).scale(shake_power)
let follow = clamp_f32(7.5 * dt, 0.0, 1.0)
camera_pos = lerp_vec3(camera_pos, ideal_cam_pos, follow)

LIGHTING

Shape light and color

Lighting

Create a day/night cycle

Rotate a directional sun and blend light and clear colors for the time of day.

UBE · WASI 0.2
let angle = time_of_day * 6.28318
let sun_dir = math::vec3(math::cos(angle), math::sin(angle), 0.35).normalized()
platform3d::set_directional_light(sun_dir, sun_color, sun_intensity)
platform3d::present(sky.r as f64, sky.g as f64, sky.b as f64)
Lighting

Light a 2D scene with a torch

Apply quadratic radial falloff to tile colors around a moving light source.

UBE · WASI 0.2
let dist_sq = dx * dx + dy * dy
let radius_sq = radius * radius
let mut intensity = ambient
if dist_sq < radius_sq {
    intensity += (1.0 - dist_sq / radius_sq) * brightness
}
batch.draw_rect_rgba(x, y, tile_size, tile_size, r * intensity, g * intensity, b * intensity, 1.0)
Lighting

Build a colored studio rig

Cycle a directional key light through hues and compare its effect on tinted meshes.

UBE · WASI 0.2
platform3d::set_directional_light(light_dir, key_color, 1.0)
platform3d::draw_instances_tinted(mesh_id, models, material_tints)
Lighting

Approximate a moving 3D point light

Calculate distance attenuation per object, then submit the result as per-instance tint colors.

UBE · WASI 0.2
let dist_sq = dx * dx + dy * dy + dz * dz
let dist = sqrt_approx(dist_sq)
let att = 1.0 / (1.0 + 0.08 * dist + 0.015 * dist * dist)
let r = 0.08 + att * 0.95
let g = 0.10 + att * 0.70
let b = 0.15 + att * 0.25
tints.push(color::rgba(r, g, b, 1.0))
Lighting

Add a Fresnel rim glow

Brighten grazing surfaces by cubing one minus the view-facing normal dot product.

UBE · WASI 0.2
let inv = 1.0 - ndotv
let fresnel = inv * inv * inv
let r = 0.08 + fresnel * 0.20
let g = 0.12 + fresnel * 0.85
let b = 0.20 + fresnel * 0.80
tints.push(color::rgba(r, g, b, 1.0))

INPUT

Make controls responsive

GAMEPLAY

Build game systems

Gameplay

Manage entities safely

Create entities in a World, validate handles, and despawn safely when they leave play.

UBE · WASI 0.2
let mut world = ecs::world_new(256)
let id = world.create_sprite_rgba(x, y, w, h, 0.2, 0.8, 0.3, 1.0)
if let entity = world.get(id) {
    game.draw_rect(entity.transform.position.x, entity.transform.position.y,
        entity.sprite.size.x, entity.sprite.size.y, entity.sprite.color)
}
Gameplay

Draw text and query a spatial hash

Draw compact bitmap HUD labels, then use an arena scoped grid to find broadphase candidates. Follow candidates with exact overlap tests.

UBE · WASI 0.2
batch.draw_text("SCORE: 10", 16.0, 16.0, 2.0, color::white())
let mut grid = physics2d::grid_new(32.0)
grid.insert(entity_id, x, y, width, height)
let nearby = grid.query_aabb(query_x, query_y, query_w, query_h)
// A grid hit is a candidate; check exact bounds before resolving collision.
if physics2d::overlaps(entity_bounds, query_bounds) {
    resolve_collision(entity_id)
}

PERSISTENCE

Keep state for as long as you need it

Persistence

Save game data between launches

Serialize game values to bytes, load them at startup, and handle failed writes. Ube values outside an arena live between frames; storage persists bytes after the process exits.

UBE · WASI 0.2
fn encode_score(score: i32) -> []u8 {
    let n = score as u32
    return []u8{
        (n & 255) as u8,
        ((n >> 8) & 255) as u8,
        ((n >> 16) & 255) as u8,
        ((n >> 24) & 255) as u8
    }
}

fn decode_score(view bytes: []u8) -> i32 {
    if bytes.len < 4 { return 0 }
    return (bytes[0] as i32) |
        ((bytes[1] as i32) << 8) |
        ((bytes[2] as i32) << 16) |
        ((bytes[3] as i32) << 24)
}

let loaded = storage::load_bytes("profile/best-score")
let mut best_score = 0
if loaded.found { best_score = decode_score(loaded.data) }

// After updating the record:
let bytes = encode_score(best_score)
if !storage::save_bytes("profile/best-score", bytes) {
    // Report the error or retry the save later.
}

SHADERS

Understand shader and material behavior

Shaders

Understand Groot’s built-in 2D and 3D shaders

The 2D shader samples the selected texture at the interpolated UV and multiplies it by vertex color. The 3D shader applies host-defined directional lighting and per-instance tint.

UBE · WASI 0.2
let texture_id = platform::create_texture(width, height, rgba_pixels)
let mut batch = render::batcher_new(1)
batch.draw_quad(x, y, w, h, 0.0, 0.0, 1.0, 1.0, color::white())
platform::bind_texture(texture_id)
platform::submit(batch.vertices)
platform::present(0.02, 0.02, 0.04)

// 3D appearance uses supported host inputs:
platform3d::set_directional_light(light_dir, light_color, intensity)
platform3d::draw_instances_tinted(mesh_id, models, tints)