ForceMode2D

Namespace:

namespace Lenga\Engine\Enumerations;

Defines how a physics force is applied to a 2D Rigidbody2D. Pass a ForceMode2D case as the second argument to addForce() and related methods.

Values

Case Value Description
Force 0 Add a continuous force over time, scaled by the body's mass.
Impulse 1 Apply an instant force in a single frame, scaled by mass. Use for jumps and one-shot hits.

Example

use Lenga\Engine\Core\Behaviour;
use Lenga\Engine\Core\Rigidbody2D;
use Lenga\Engine\Enumerations\ForceMode2D;
use Lenga\Engine\Core\Input;
use Lenga\Engine\Enumerations\KeyCode;

class PlatformerJump extends Behaviour
{
    public float $jumpForce = 12.0;

    public function update(): void
    {
        if (Input::getKeyDown(KeyCode::SPACE)) {
            $rb = $this->getComponent(Rigidbody2D::class);
            $rb->addForce([0, $this->jumpForce], ForceMode2D::Impulse);
        }
    }
}