Physics Engines¶
Contents
arcade.PhysicsEnginePlatformer¶
- class arcade.PhysicsEnginePlatformer(player_sprite: arcade.sprite.Sprite, platforms: Union[arcade.sprite.Sprite, List[arcade.sprite_list.sprite_list.SpriteList]], gravity_constant: float = 0.5, ladders: Optional[Union[arcade.sprite.Sprite, List[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, it does not currently handle rotation.
- Parameters
player_sprite (Sprite) – The moving sprite
platforms (SpriteList) – The sprites it can’t move through
gravity_constant (float) – Downward acceleration per frame
ladders (SpriteList) – Ladders the user can climb on
- 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
- 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) –
arcade.PhysicsEngineSimple¶
- class arcade.PhysicsEngineSimple(player_sprite: arcade.sprite.Sprite, walls: Union[arcade.sprite_list.sprite_list.SpriteList, List[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 (SpriteList) – The sprites it can’t move through
arcade.PymunkPhysicsEngine¶
- class arcade.PymunkPhysicsEngine(gravity=(0, 0), damping: float = 1.0)[source]¶
Pymunk Physics Engine
- 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=None, body_type=0, damping=None, gravity=None, max_velocity=None, max_horizontal_velocity=None, max_vertical_velocity=None, radius: float = 0, collision_type: str = 'default')[source]¶
Add a sprite to the physics engine.
- add_sprite_list(sprite_list, mass: float = 1, friction: float = 0.2, elasticity: Optional[float] = None, moment=None, body_type=0, damping=None, collision_type=None)[source]¶
Add all sprites in a sprite list to the physics engine.
- 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[arcade.sprite.Sprite] [source]¶
Given a shape, what sprite is associated with it?
- get_sprites_from_arbiter(arbiter)[source]¶
Given a collision arbiter, return the sprites associated with the collision.
- 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
- 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.