Skip to content

Physics

How it is used: Body properties and methods are called on the object from its script (self.obj.apply_impulse(…)); world functions (studio.set_gravity(…)) work from an object script or main.py.

import pygamestudio as studio
class ObjectScript:
def __init__(self, obj):
self.obj = obj
def on_start(self):
studio.set_gravity((0, 980))
self.obj.apply_impulse((0, -420)) # a jump, once
def on_update(self, dt):
self.obj.apply_force((60, 0)) # push, every frame it runs

The movement and spin damping of the body, as (linear, angular).

The bounce of the body.

The friction of the body.

The gravity scale of the body (0 floats, 1 is normal).

How hard the body is to push around.

The body type: 'dynamic', 'static' or 'kinematic'.

Object method — is this object a rigid body? Called as self.obj.is_physics_enabled(). The world-wide switch has the same name; see The world.

True while the body keeps its rotation locked upright.

How quickly movement and spin die down.

  • linear (float): movement damping.
  • angular (float): spin damping.

Sets the bounce of the body; 0.2 by default.

  • elasticity (float): 0 lands dead, 1 bounces back fully.

Object method — turns the rigid body on or off (self.obj.set_physics_enabled(False)). Turning it off while the game runs removes the body and the object stays where it is.

  • enabled (bool): True / False.

True keeps the body upright — a character never tips over. Keep it off when the body should roll: a locked angle only slides, while a rolling body follows its spin with its angle (a textured sprite or a polygon turns visibly).

  • fixed (bool): True locks the rotation, False lets the body spin.

Sets the friction of the body; 0.6 by default.

  • friction (float): 0 = ice, 1 = sticky.

Sets the gravity scale.

  • scale (float): 0 floats, 1 is normal, negative falls upwards.

Sets the mass; 1.0 by default.

  • mass (float): larger is harder to push.

Sets how the body behaves.

  • type (str): 'dynamic' (falls, is pushed, rotates), 'static' (never moves, pushes everything else) or 'kinematic' (moved by the script, pushes the others on the way).

The offset of the shape from the object’s centre (content pixels).

The polygon vertices of the body shape.

The box size used by rect and ellipse.

The type of the body shape.

Back to the object-sized default box.

set_physics_shape_ellipse(radius_x, radius_y)

Section titled “set_physics_shape_ellipse(radius_x, radius_y)”

Shortcut: set both radii and switch to ellipse.

  • radius_x (float): horizontal radius, in content pixels.
  • radius_y (float): vertical radius, in content pixels.

Moves the shape away from the object’s centre (content pixels).

  • x (float): horizontal offset.
  • y (float): vertical offset.

Explicit vertices (content pixels, top-left origin) — switches to polygon.

  • points (list): vertex list, e.g. [(0, 0), (50, 0), (25, 40)].

Sets the box size used by rect and ellipse (content pixels).

  • width (int): box width.
  • height (int): box height.

Sets the type of the body shape; it starts as a rect as big as the object — that is what the Inspector materializes when you tick Enable Physics.

  • type (str): 'bbox' (the rendered box), 'rect', 'ellipse' or 'polygon'.

Push every frame while it is called — wind, thrusters, a conveyor. The force is mass × acceleration; the accumulator is cleared after every physics step.

  • force ((float, float)): force components, e.g. (0, 800) pushes down.

One instant push: a jump, a hit, an explosion. The impulse is mass × pixels/second.

  • impulse ((float, float)): impulse components, e.g. (0, -400) for a jump.

The spin speed in degrees per second (None without a body).

Speed in pixels per second (None without a body).

True while the body stands on something. A short “coyote” memory keeps it True just after walking off a ledge, so a jump still works.

Sets the spin speed.

  • deg_per_second (float): degrees per second.

Sets the speed directly.

  • velocity ((float, float)): velocity components in pixels per second, e.g. (200, 0).

The global gravity in pixels/second².

The current physics-only time scale.

The world of the running scene, or None (in the editor, or without a scene).

World function — True while the simulation runs (studio.is_physics_enabled()). Not the object method of the same name above.

The first physics object a ray from start to end hits, or None. Only objects with Enable Physics are detected.

  • start ((float, float)): ray start, in world coordinates.
  • end ((float, float)): ray end, in world coordinates.

Sets the global gravity. (0, 0) is space; a negative y pulls upwards.

  • gravity ((float, float)): gravity in pixels/second²; default (0, 980).

World function — pauses or resumes the whole simulation (studio.set_physics_enabled(False)); bodies freeze in place.

  • enabled (bool): True runs the simulation, False pauses it.

Sets the physics-only time scale: 0.5 slow motion, 2 fast forward, 0 a paused world that still draws.

  • scale (float): time scale, >= 0.

Fired when two bodies touch. Enabling physics is enough to receive it; with Enable Collision on as well, the pair is still reported exactly once.

  • other (object): the other object.

Fired when two bodies part.

  • other (object): the other object.