Physics Engines#

arcade.PymunkException#

class arcade.PymunkException[source]#

arcade.PymunkPhysicsEngine#

class arcade.PymunkPhysicsEngine(gravity=(0, 0), damping: float = 1.0, maximum_incline_on_ground: float = 0.708)[source]#

Pymunk Physics Engine

Parameters
  • gravity – The direction where gravity is pointing

  • damping – The amount of speed which is kept to the next tick. a value of 1.0 means no speed loss, while 0.9 has 10% loss of speed etc.

  • maximum_incline_on_ground – The maximum incline the ground can have, before is_on_ground() becomes False default = 0.708 or a little bit over 45° angle

add_collision_handler(first_type: str, second_type: str, begin_handler: Optional[Callable] = None, pre_handler: Optional[Callable] = None, post_handler: Optional[Callable] = None, separate_handler: Optional[Callable] = None)[source]#

Add code to handle collisions between objects.

add_sprite(sprite: arcade.sprite.Sprite, mass: float = 1, friction: float = 0.2, elasticity: Optional[float] = None, moment_of_inertia: Optional[float] = None, body_type: int = 0, damping: Optional[float] = None, gravity: Optional[Union[pymunk.vec2d.Vec2d, Tuple[float, float], pyglet.math.Vec2]] = None, max_velocity: Optional[int] = None, max_horizontal_velocity: Optional[int] = None, max_vertical_velocity: Optional[int] = None, radius: float = 0, collision_type: Optional[str] = 'default', moment_of_intertia: Optional[float] = None, moment: Optional[float] = None)[source]#

Add a sprite to the physics engine.

Parameters
  • sprite – The sprite to add

  • mass – The mass of the object. Defaults to 1

  • friction – The friction the object has. Defaults to 0.2

  • elasticity – How bouncy this object is. 0 is no bounce. Values of 1.0 and higher may behave badly.

  • moment_of_inertia – The moment of inertia, or force needed to change angular momentum. Providing infinite makes this object stuck in its rotation.

  • body_type – The type of the body. Defaults to Dynamic, meaning, the body can move, rotate etc. Providing STATIC makes it fixed to the world.

  • damping – See class docs

  • gravity – See class docs

  • max_velocity – The maximum velocity of the object.

  • max_horizontal_velocity – maximum velocity on the x axis

  • max_vertical_velocity – maximum velocity on the y axis

  • radius

  • collision_type

  • moment_of_intertia – Deprecated alias of moment_of_inertia compatible with a typo introduced in 2.6.2

  • moment – Deprecated alias of moment_of_inertia compatible with versions <= 2.6.1

add_sprite_list(sprite_list, mass: float = 1, friction: float = 0.2, elasticity: Optional[float] = None, moment_of_intertia: Optional[float] = None, body_type: int = 0, damping: Optional[float] = None, collision_type: Optional[str] = None)[source]#

Add all sprites in a sprite list to the physics engine.

apply_force(sprite: arcade.sprite.Sprite, force: Tuple[float, float])[source]#

Apply force to a Sprite.

apply_impulse(sprite: arcade.sprite.Sprite, impulse: Tuple[float, float])[source]#

Apply an impulse force on a sprite

apply_opposite_running_force(sprite: arcade.sprite.Sprite)[source]#

If a sprite goes left while on top of a dynamic sprite, that sprite should get pushed to the right.

check_grounding(sprite: arcade.sprite.Sprite)[source]#

See if the player is on the ground. Used to see if we can jump.

get_physics_object(sprite: arcade.sprite.Sprite) arcade.pymunk_physics_engine.PymunkPhysicsObject[source]#

Get the shape/body for a sprite.

get_sprite_for_shape(shape: Optional[pymunk.shapes.Shape]) Optional[arcade.sprite.Sprite][source]#

Given a shape, what sprite is associated with it?

get_sprites_from_arbiter(arbiter: pymunk.arbiter.Arbiter) Tuple[Optional[arcade.sprite.Sprite], Optional[arcade.sprite.Sprite]][source]#

Given a collision arbiter, return the sprites associated with the collision.

is_on_ground(sprite: arcade.sprite.Sprite) bool[source]#

Return true of sprite is on top of something.

remove_sprite(sprite: arcade.sprite.Sprite)[source]#

Remove a sprite from the physics engine.

resync_sprites()[source]#

Set visual sprites to be the same location as physics engine sprites. Call this after stepping the pymunk physics engine

set_friction(sprite: arcade.sprite.Sprite, friction: float)[source]#

Apply force to a Sprite.

set_horizontal_velocity(sprite: arcade.sprite.Sprite, velocity: float)[source]#

Set a sprite’s velocity

set_position(sprite: arcade.sprite.Sprite, position: Union[pymunk.vec2d.Vec2d, Tuple[float, float]])[source]#

Apply an impulse force on a sprite

set_velocity(sprite: arcade.sprite.Sprite, velocity: Tuple[float, float])[source]#

Apply an impulse force on a sprite

step(delta_time: float = 0.016666666666666666, resync_sprites: bool = True)[source]#

Tell the physics engine to perform calculations.

Parameters
  • delta_time (float) – Time to move the simulation forward. Keep this value constant, do not use varying values for each step.

  • resync_sprites (bool) – Resynchronize Arcade graphical sprites to be at the same location as their Pymunk counterparts. If running multiple steps per frame, set this to false for the first steps, and true for the last step that’s part of the update.

arcade.PymunkPhysicsObject#

class arcade.PymunkPhysicsObject(body: Optional[pymunk.body.Body] = None, shape: Optional[pymunk.shapes.Shape] = None)[source]#

Object that holds pymunk body/shape for a sprite.

arcade.PhysicsEnginePlatformer#

class arcade.PhysicsEnginePlatformer(player_sprite: arcade.sprite.Sprite, platforms: Optional[Union[arcade.sprite_list.sprite_list.SpriteList, Iterable[arcade.sprite_list.sprite_list.SpriteList]]] = None, gravity_constant: float = 0.5, ladders: Optional[Union[arcade.sprite_list.sprite_list.SpriteList, Iterable[arcade.sprite_list.sprite_list.SpriteList]]] = None, walls: Optional[Union[arcade.sprite_list.sprite_list.SpriteList, Iterable[arcade.sprite_list.sprite_list.SpriteList]]] = None)[source]#

Simplistic physics engine for use in a platformer. It is easier to get started with this engine than more sophisticated engines like PyMunk.

Note: Sending static sprites to the walls parameter and moving sprites to the platforms parameter will have very extreme benefits to performance.

Note: This engine will automatically move any Sprites sent to the platforms parameter between a boundary_top and boundary_bottom or a boundary_left and boundary_right attribute of the Sprite. You need only set an initial change_x or change_y on it.

Parameters
  • player_sprite (Sprite) – The moving sprite

  • platforms (Optional[Union[SpriteList, Iterable[SpriteList]]]) – Sprites the player can’t move through. This value should only be used for moving Sprites. Static sprites should be sent to the walls parameter.

  • gravity_constant (float) – Downward acceleration per frame

  • ladders (Optional[Union[SpriteList, Iterable[SpriteList]]]) – Ladders the user can climb on

  • walls (Optional[Union[SpriteList, Iterable[SpriteList]]]) – Sprites the player can’t move through. This value should only be used for static Sprites. Moving sprites should be sent to the platforms parameter.

can_jump(y_distance: float = 5) bool[source]#

Method that looks to see if there is a floor under the player_sprite. If there is a floor, the player can jump and we return a True.

Returns

True if there is a platform below us

Return type

bool

disable_multi_jump()[source]#

Disables multi-jump.

Calling this function also removes the requirement to call increment_jump_counter() every time the player jumps.

enable_multi_jump(allowed_jumps: int)[source]#

Enables multi-jump. allowed_jumps should include the initial jump. (1 allows only a single jump, 2 enables double-jump, etc)

If you enable multi-jump, you MUST call increment_jump_counter() every time the player jumps. Otherwise they can jump infinitely.

Parameters

allowed_jumps (int) –

increment_jump_counter()[source]#

Updates the jump counter for multi-jump tracking

is_on_ladder()[source]#

Return ‘true’ if the player is in contact with a sprite in the ladder list.

jump(velocity: int)[source]#

Have the character jump.

update()[source]#

Move everything and resolve collisions.

Returns

SpriteList with all sprites contacted. Empty list if no sprites.

arcade.PhysicsEngineSimple#

class arcade.PhysicsEngineSimple(player_sprite: arcade.sprite.Sprite, walls: Union[arcade.sprite_list.sprite_list.SpriteList, Iterable[arcade.sprite_list.sprite_list.SpriteList]])[source]#

Simplistic physics engine for use in games without gravity, such as top-down games. It is easier to get started with this engine than more sophisticated engines like PyMunk.

Parameters
  • player_sprite (Sprite) – The moving sprite

  • walls (Union[SpriteList, Iterable[SpriteList]) – The sprites it can’t move through. This can be one or multiple spritelists.

update()[source]#

Move everything and resolve collisions.

Returns

SpriteList with all sprites contacted. Empty list if no sprites.