Godot:KinematicBody2D
lastmod 2023-04-26created 2022-03-224 edits
A special type of 2D body that can be controlled by the user.
They are not affected by physics at all.
To other types of bodies, such as characters or rigid bodies, they are the same as a static body.
Uses
There are two main uses.
- Simulated motion
- When this body is moved manually, either from code or from an AnimationPlayer (with AnimationPlayer.playback_process_mode set to
physics), the physics will automatically compute an estimate of its linear and angular velocity. - This makes it very useful for moving platforms or other AnimationPlayer-controlled objects (e.g. a door, a bridge that opens, etc.).
- Kinematic characters
- KinematicBody2D also has an API for moving objects while performing collision tests (the #move_and_collide and #move_and_slide methods).
- This makes it really useful for implementing characters that collide against the world but don't require advanced physics.
move_and_collide
KinematicCollision2D move_and_collide ( Vector2 rel_vec, bool infinite_inertia=true, bool exclude_raycast_shapes=true, bool test_only=false )
Moves the body along the vector rel_vec. The body stops if it collides.
Returns a KinematicCollision2D containing information about the collision when it stops, or when it touches another body along the motion.
If test_only is true, the body does not move but the would-be collision information is given.
move_and_slide
Vector2 move_and_slide ( Vector2 linear_velocity, Vector2 up_direction=Vector2( 0, 0 ), bool stop_on_slope=false, int max_slides=4, float floor_max_angle=0.785398, bool infinite_inertia=true )
Moves the body along a vector.
If the body collides with another, it slides along the other body rather than stopping immediately.
If the other body is a KinematicBody2D or RigidBody2D, it will also be affected by the motion of the other body.
You can use this to make moving and rotating platforms, or to make nodes push other nodes.
This method should be used in Node._physics_process, since it automatically uses the physics step's delta value in its calculation. Otherwise, the simulation will run at an incorrect speed.
-
linear_velocityis the velocity vector in pixels per second. Unlike #move_and_collide, you should not multiply it bydelta. (The physics engine handles applying the velocity.) -
up_directionis the up direction, used to determine what is a wall and what is a floor or a ceiling. If set to the default value ofVector2(0, 0), everything is considered a wall. This is useful for top-down games. - If
stop_on_slopeis true, the body will not slide on slopes when you include gravity inlinear_velocityand the body is standing still. - If the body collides, it will change direction a maximum of
max_slidestimes before it stops. -
floor_max_angleis the maximum angle (in radians) where a slope is still considered a floor (or a ceiling), rather than a wall. The default value equals 45 degrees. - If
infinity_inertiais-
true, the body will be able to push RigidBody2D nodes, but it won't also detect any collisions with them. -
false, it will interact with RigidBody2D nodes like StaticBody2D.
-
Returns the linear_velocity vector, rotated and/or scaled if a slide collision occurred. To get detailed information about collisions that occurred, use #get_slide_collision.
When the body touches a moving platform, the platform's velocity is automatically added to the body motion. If a collision occurs due to the platform's motion, it will always be first in the slide collisions.