Physics
PhysicsWorld is an entity that simulates AABB collisions with gravity.
world = bf.PhysicsWorld(gravity=(0, 900))
world.add_layer("terrain")
world.add_layer("actors")
world.add_static("terrain", ground)
world.add_dynamic("actors", player)
world.set_layers_collide("actors", "terrain")
scene.add(world)
Physics layers ≠ scene layers
CollisionLayer names group bodies for collision filtering. They are unrelated to the old scene render layers.
Setup
add_layer(name)add_static(layer, *entities)/add_dynamic(layer, *entities)set_layers_collide(a, b, collide=True)set_collision_callback(a, b, callback)enable_spatial_hash(cell_size=64)track_entity(entity)
Queries
raycast(origin, direction, max_distance=..., *layers)query_rect(rect, layers=None)query_point(point, layers=None)check_collision(a, b)→Collision
Helpers: check_aabb, get_overlap, find_collisions.
DynamicEntity
Movable bodies usually inherit DynamicEntity:
velocity,mass,restitution,groundedset_velocity(x, y),apply_impulse(x, y),move_by_velocity(dt)- Override
on_collideX,on_collideY,on_landfor custom response