physics.CharacterBody
pykraken.physics.CharacterBody A kinematic physics body designed for player-controlled characters.
More about physics.CharacterBody
Character bodies use a move-and-slide algorithm for movement and collision resolution. They automatically detect floor, wall, and ceiling contacts and slide along surfaces rather than bouncing off them. Ideal for platformer characters, NPCs, or any entity that needs precise movement control.
Inherits Body
At a glance
23 membersConstructormethodaccelerationpropertyair_steerpropertycapsule_shapepropertycollision_layerpropertycollision_maskpropertyfrictionpropertyis_validpropertymasspropertymax_speedpropertymotion_modepropertypospropertyrotationpropertystop_speedpropertyvelocitypropertyadd_collidermethoddestroymethodget_collidersmethodget_transformmethodis_on_ceilingmethodis_on_floormethodis_on_wallmethodmove_and_slidemethodReference
PYTHON APIConstructor
methodphysics.CharacterBody(world: physics.World)Create a new character body in the specified world.
The body is created at the origin with default movement properties.
Add colliders with add_collider before calling move_and_slide.
Args
- world (
World) — The physics world to create the body in.
acceleration
propertyacceleration: floatThe acceleration applied when moving.
air_steer
propertyair_steer: floatThe control factor for steering while in the air (0.0 to 1.0).
capsule_shape
propertycapsule_shape: CapsuleThe capsule shape used for ground detection and collision casting.
collision_layer
propertycollision_layer: intThe body's collision layer (category bits).
collision_mask
propertycollision_mask: intThe body's collision mask.
friction
propertyfriction: floatThe friction applied when on the ground.
is_valid
propertyis_valid: boolIndicates whether the body is not destroyed.
mass
propertymass: floatThe simulated mass of the character for pushing rigid bodies.
max_speed
propertymax_speed: floatThe maximum speed the character can move.
motion_mode
propertymotion_mode: physics.CharacterBody.MotionModeThe motion mode of the character.
pos
propertypos: Vec2The position of the body in world coordinates.
rotation
propertyrotation: floatThe rotation of the body in radians.
stop_speed
propertystop_speed: floatThe speed threshold below which the character will stop moving.
velocity
propertyvelocity: Vec2The velocity of the character body.
add_collider(self, circle: Circle, density: float = 1.0, friction: float = 0.20000000298023224, restitution: float = 0.0, enable_events: bool = False, is_sensor: bool = False) -> Noneadd_collider(self, polygon: Polygon, density: float = 1.0, friction: float = 0.20000000298023224, restitution: float = 0.0, enable_events: bool = False, is_sensor: bool = False) -> Noneadd_collider(self, rect: Rect, density: float = 1.0, friction: float = 0.20000000298023224, restitution: float = 0.0, enable_events: bool = False, is_sensor: bool = False) -> Noneadd_collider(self, capsule: Capsule, density: float = 1.0, friction: float = 0.20000000298023224, restitution: float = 0.0, enable_events: bool = False, is_sensor: bool = False) -> NoneOverloaded function.
add_collider(self, circle: Circle, density: float = 1.0, friction: float = 0.20000000298023224, restitution: float = 0.0, enable_events: bool = False, is_sensor: bool = False) -> None
Add a circular collider to the body.
Args
- circle (
Circle) — The circular shape to add as a collider. - density (
float, optional) — The density of the collider. Defaults to 1.0. - friction (
float, optional) — The friction coefficient of the collider. Defaults to 0.2. - restitution (
float, optional) — The restitution (bounciness) of the collider. Defaults to 0.0. - enable_events (
bool, optional) — Whether to enable hit, contact, and sensor events for this collider. Defaults to False. - is_sensor (
bool, optional) — Whether the collider is a sensor. Defaults to False.
add_collider(self, polygon: Polygon, density: float = 1.0, friction: float = 0.20000000298023224, restitution: float = 0.0, enable_events: bool = False, is_sensor: bool = False) -> None
Add a polygonal collider to the body.
Args
- polygon (
Polygon) — The polygonal shape to add as a collider. - density (
float, optional) — The density of the collider. Defaults to 1.0. - friction (
float, optional) — The friction coefficient of the collider. Defaults to 0.2. - restitution (
float, optional) — The restitution (bounciness) of the collider. Defaults to 0.0. - enable_events (
bool, optional) — Whether to enable hit, contact, and sensor events for this collider. Defaults to False. - is_sensor (
bool, optional) — Whether the collider is a sensor. Defaults to False.
add_collider(self, rect: Rect, density: float = 1.0, friction: float = 0.20000000298023224, restitution: float = 0.0, enable_events: bool = False, is_sensor: bool = False) -> None
Add a rectangular collider to the body.
Args
- rect (
Rect) — The rectangular shape to add as a collider. - density (
float, optional) — The density of the collider. Defaults to 1.0. - friction (
float, optional) — The friction coefficient of the collider. Defaults to 0.2. - restitution (
float, optional) — The restitution (bounciness) of the collider. Defaults to 0.0. - enable_events (
bool, optional) — Whether to enable hit, contact, and sensor events for this collider. Defaults to False. - is_sensor (
bool, optional) — Whether the collider is a sensor. Defaults to False.
add_collider(self, capsule: Capsule, density: float = 1.0, friction: float = 0.20000000298023224, restitution: float = 0.0, enable_events: bool = False, is_sensor: bool = False) -> None
Add a capsule collider to the body.
Args
- capsule (
Capsule) — The capsule shape to add as a collider. - density (
float, optional) — The density of the collider. Defaults to 1.0. - friction (
float, optional) — The friction coefficient of the collider. Defaults to 0.2. - restitution (
float, optional) — The restitution (bounciness) of the collider. Defaults to 0.0. - enable_events (
bool, optional) — Whether to enable hit, contact, and sensor events for this collider. Defaults to False. - is_sensor (
bool, optional) — Whether the collider is a sensor. Defaults to False.
destroy
methoddestroy(self) -> NoneDestroy the body manually.
get_colliders
methodget_colliders(self) -> list[physics.Collider]Return non-owning handles for this body's collider shapes.
get_transform
methodget_transform(self) -> TransformGet the current transform of the body (position, rotation, and scale).
Returns
- Transform — The current transform of the body.
is_on_ceiling
methodis_on_ceiling(self) -> boolCheck if the character is currently in contact with the ceiling.
Returns
- bool — True if the character is on the ceiling, False otherwise.
is_on_floor
methodis_on_floor(self) -> boolCheck if the character is currently in contact with the floor.
Returns
- bool — True if the character is on the floor, False otherwise.
is_on_wall
methodis_on_wall(self) -> boolCheck if the character is currently in contact with a wall.
Returns
- bool — True if the character is on a wall, False otherwise.
move_and_slide
methodmove_and_slide(self, delta: float = -1.0) -> NonePerform movement and collision resolution for the character.
This method moves the character according to the velocity property and resolves collisions by sliding along surfaces. It also updates the floor/ceiling/wall contact states.
Args
- delta (
float, optional) — The time step to use for movement. Defaults to -1.0, which uses the frame delta.