gale.physics.world module
This file contains the implementation of the class World: a physics simulation, in pixel units, wrapping a Box2D world without ever exposing Box2D itself — creates every Body/Joint, steps the simulation, and dispatches collision callbacks.
Author: Alejandro Mujica (aledrums@gmail.com)
- class gale.physics.world.World(gravity=(0, 900), pixels_per_meter=30.0, fixed_timestep=0.016666666666666666)[source]
Bases:
objectA physics simulation, in pixel units. Wraps a Box2D world (never exposed) and every Body/Joint created in it.
Follows the same update/fixed_update split Unity uses to decouple the game loop’s real, variable dt from the fixed timestep Box2D’s solver needs for stable results: call update(dt) once a frame, same as everything else in gale; it accumulates dt and calls fixed_update() (a real Box2D step) as many times as needed to consume it. fixed_update() is itself a public method, useful for tests (or the rare game) that want to drive the simulation one fixed step at a time directly.
Usage example:
world = World(gravity=(0, 900)) ground = world.create_static_body(200, 280, BoxShape(400, 20)) ball = world.create_dynamic_body(200, 50, CircleShape(radius=10))
# In your state: def update(self, dt: float) -> None:
self.world.update(dt)
- Parameters:
gravity (Tuple[float, float])
pixels_per_meter (float)
fixed_timestep (float)
- create_static_body(x, y, shape=None)[source]
- Parameters:
x (float) – Initial x position, in pixels.
y (float) – Initial y position, in pixels.
shape (Any | None) – A CircleShape/BoxShape/PolygonShape to attach immediately. The default value is None, so fixtures can be added later via Body.add_circle/add_box/add_polygon.
- Returns:
The new Body.
- Return type:
- create_dynamic_body(x, y, shape=None)[source]
- Parameters:
x (float) – Initial x position, in pixels.
y (float) – Initial y position, in pixels.
shape (Any | None) – A CircleShape/BoxShape/PolygonShape to attach immediately. The default value is None, so fixtures can be added later via Body.add_circle/add_box/add_polygon.
- Returns:
The new Body.
- Return type:
- create_kinematic_body(x, y, shape=None)[source]
- Parameters:
x (float) – Initial x position, in pixels.
y (float) – Initial y position, in pixels.
shape (Any | None) – A CircleShape/BoxShape/PolygonShape to attach immediately. The default value is None, so fixtures can be added later via Body.add_circle/add_box/add_polygon.
- Returns:
The new Body.
- Return type:
- destroy_body(body)[source]
- Parameters:
body (Body) – The Body to remove from this World.
- Return type:
None
- create_revolute_joint(body_a, body_b, anchor, **options)[source]
- Parameters:
- Returns:
The new RevoluteJoint.
- Return type:
- create_wheel_joint(body_a, body_b, anchor, axis=(0, 1), **options)[source]
- Parameters:
body_a (Body) – The chassis (or other body the wheel is attached to).
body_b (Body) – The wheel.
anchor (Tuple[float, float]) – The suspension’s attachment point, in pixels, in world coordinates.
axis (Tuple[float, float]) – The suspension’s movement axis, in body_a’s local frame. The default value is (0, 1) (straight up/down).
options (Any) – Forwarded to Box2D’s wheel joint definition (e.g. frequencyHz, dampingRatio, maxMotorTorque, motorSpeed, enableMotor).
- Returns:
The new WheelJoint.
- Return type:
- destroy_joint(joint)[source]
- Parameters:
joint (Joint) – The Joint to remove from this World.
- Return type:
None