Rigidbody3D

3D rigid body physics component bound to the Lenga runtime.

Namespace:

namespace Lenga\Engine\Core;

class Rigidbody3D

Use Rigidbody3D when a GameObject should be moved by 3D physics, gravity, velocity, and forces.

Properties

Property Type Description
bodyType string Dynamic, Kinematic, or Static. Dynamic bodies are moved by physics.
velocity Vector3 World-space linear velocity.
angularVelocity Vector3 World-space angular velocity.
useGravity bool Enables or disables gravity for this body.
gravityScale float Multiplier applied to world gravity.
linearDrag float Damps translational velocity.
angularDrag float Damps rotational velocity.
freezeRotation bool Prevents simulated rotation.
collisionDetection string Discrete or Continuous. Use continuous detection for fast-moving bodies.

Methods

addForce

public function addForce(Vector3 $force, ForceMode $mode = ForceMode::Force): void

Applies a force to this body.

Force and Acceleration are applied over the next simulation step. Impulse and VelocityChange apply an immediate velocity change.

isTouching

public function isTouching(bool $includeTriggers = true, ?int $layerMask = null): bool

Returns true when any collider on this Rigidbody's GameObject is touching another matching collider.

getContacts

public function getContacts(bool $includeTriggers = true, ?int $layerMask = null): array

Returns current 3D contacts for colliders owned by this Rigidbody's GameObject.

Example

<?php

declare(strict_types=1);

namespace Game\Scripts;

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

final class LaunchPad extends Behaviour
{
    public float $launchStrength = 12.0;

    private ?Rigidbody3D $body = null;

    public function start(): void
    {
        $this->body = $this->getComponent(Rigidbody3D::class);
    }

    public function update(): void
    {
        if (!$this->body instanceof Rigidbody3D || !Input::getKeyDown(KeyCode::SPACE)) {
            return;
        }

        $this->body->addForce(new Vector3(0.0, $this->launchStrength, 0.0), ForceMode::Impulse);
    }
}