gale.physics.body module

This file contains the implementation of the class Body: a physics body and its fixtures, in pixel units, wrapping a single pymunk body without ever exposing pymunk itself. Created through World.create_static_body/create_dynamic_body/create_kinematic_body.

Author: Alejandro Mujica (aledrums@gmail.com)

class gale.physics.body.Body(pm_body, world, body_type, pixels_per_meter)[source]

Bases: object

A physics body, in pixel units. Wraps a single pymunk body (never exposed) plus its fixtures. Created through World.create_static_body/ create_dynamic_body/create_kinematic_body, never directly.

Usage example:

player = world.create_dynamic_body(100, 50, CircleShape(radius=10)) player.apply_impulse(0, -400) # jump

# Every frame, after world.update(dt): pygame.draw.circle(surface, “white”, player.position, 10)

Parameters:
  • pm_body (Any)

  • world (Any)

  • body_type (int)

  • pixels_per_meter (float)

property position: Vector2
property angle: float
property velocity: Vector2
property angular_velocity: float
set_velocity(vx, vy)[source]
Parameters:
  • vx (float) – Horizontal velocity, in pixels per second.

  • vy (float) – Vertical velocity, in pixels per second.

Return type:

None

apply_force(fx, fy)[source]

Apply a continuous force at this body’s center of mass (call every frame while the force should be acting, such as a thruster).

Parameters:
  • fx (float) – Horizontal force.

  • fy (float) – Vertical force.

Return type:

None

apply_torque(torque)[source]
Parameters:

torque (float) – The torque to apply this step.

Return type:

None

apply_impulse(ix, iy)[source]

Apply an instantaneous change in momentum at this body’s center of mass (a one-off “kick”, such as a jump).

Parameters:
  • ix (float) – Horizontal impulse.

  • iy (float) – Vertical impulse.

Return type:

None

add_circle(shape)[source]
Parameters:

shape (CircleShape) – The circle fixture to attach to this body.

Return type:

None

add_box(shape)[source]
Parameters:

shape (BoxShape) – The box fixture to attach to this body.

Return type:

None

add_polygon(shape)[source]
Parameters:

shape (PolygonShape) – The polygon fixture to attach to this body.

Return type:

None

property touching_bodies: List[Body]

Every other Body currently in contact with this one (a cheap way to answer “is this body resting on something,” with no event bookkeeping needed). Includes sensor overlaps, same as on_collision_begin/on_collision_end.

Type:

returns

set_damping(linear_damping=0.0, angular_damping=0.0)[source]

Slow this body’s linear/angular velocity down over time, e.g. to approximate air resistance — every fixed_update(), velocity is scaled by 1 / (1 + fixed_timestep * damping). 0 (the default for every new Body) means no damping at all.

Parameters:
  • linear_damping (float) – Damping applied to velocity. The default value is 0.0.

  • angular_damping (float) – Damping applied to angular_velocity. The default value is 0.0.

Return type:

None

destroy()[source]

Remove this body (and its fixtures) from its World. Do not use this Body afterwards.

Return type:

None