ForceMode

Namespace:

namespace Lenga\Engine\Enumerations;

Defines how a physics force is applied to a 3D Rigidbody3D. Pass a ForceMode 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. Use for realistic pushes like wind or a rocket engine.
Acceleration 1 Add a continuous acceleration over time, ignoring mass. All objects accelerate at the same rate regardless of weight.
Impulse 2 Apply an instant force in a single frame, scaled by mass. Use for jumps, explosions, and one-shot hits.
VelocityChange 3 Apply an instant velocity change in a single frame, ignoring mass. All objects receive the same velocity delta.

Example

use Lenga\Engine\Core\Behaviour;
use Lenga\Engine\Core\Rigidbody3D;
use Lenga\Engine\Enumerations\ForceMode;
use Lenga\Engine\Core\Input;
use Lenga\Engine\Enumerations\KeyCode;

class Jumper extends Behaviour
{
    public float $jumpForce = 8.0;

    public function update(): void
    {
        if (Input::getKeyDown(KeyCode::SPACE)) {
            $rb = $this->getComponent(Rigidbody3D::class);
            // Apply an instant upward impulse when the player presses Space
            $rb->addForce([0, $this->jumpForce, 0], ForceMode::Impulse);
        }
    }
}