Skip to content

KinematicActor

A body that is moved manually via code but still interacts with the physics world.

Description

Kinematic actors are not affected by world gravity or forces but can detect and react to collisions during movement. They provide methods like moveAndSlide for complex character movement.

Note: Kinematic actors now correctly interact with RigidActors, pushing them aside when moving into them.

Namespace

namespace pixelroot32::physics {
    class KinematicActor {
        // ...
    };
}

Inheritance

Constructors

  • KinematicActor(Scalar x, Scalar y, int w, int h) Constructs a new KinematicActor.

  • KinematicActor(Vector2 position, int w, int h) Constructs a new KinematicActor using a position vector.

Public Methods

  • bool moveAndCollide(Vector2 motion, KinematicCollision* outCollision = nullptr, bool testOnly = false, Scalar safeMargin = 0.08f, bool recoveryAsCollision = false) Moves the body along a vector and stops at the first collision.

    • motion: The movement vector.
    • outCollision: Pointer to store collision information (optional).
    • testOnly: If true, checks for collisions without moving the body.
    • safeMargin: Extra margin used for collision recovery (default 0.08).
    • recoveryAsCollision: If true, depenetration is reported as collision. Returns true if a collision occurred.
  • void moveAndSlide(Vector2 velocity, Vector2 upDirection = {0, -1}) Moves the body while sliding along surfaces.

    • velocity: The linear velocity vector (pixels/second) or displacement vector depending on usage.
    • upDirection: Direction considered "up" for floor detection (default: 0, -1).
  • bool is_on_ceiling() const Returns true if the body collided with the ceiling during the last moveAndSlide call.

  • bool is_on_floor() const Returns true if the body collided with the floor during the last moveAndSlide call.

  • bool is_on_wall() const Returns true if the body collided with a wall during the last moveAndSlide call.

  • void draw(pixelroot32::graphics::Renderer& renderer) Draws the actor.

Example

void Player::update(unsigned long dt) {
    Vector2 motion(0, 0);
    if (input.isButtonDown(0)) motion.x += 100 * dt / 1000.0f;

    // Automatic sliding against walls
    moveAndSlide(motion);
}