SphereCollider3D

Namespace:

namespace Lenga\Engine\Core;

class SphereCollider3D

Properties

  • radius: local sphere radius.
  • offset: local offset from the GameObject transform.
  • isTrigger: detects overlap without producing a solid collision response.
  • materialPath: project-relative path to a Physics Material 3D asset.
  • material: a PhysicsMaterial3D object representing the collider material.

Methods

isTouching

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

Returns true when this sphere collider is touching another matching 3D collider.

Set $includeTriggers to false to ignore trigger contacts. Pass a $layerMask to limit the check to colliders on matching GameObject layers.

getContacts

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

Returns the current Collision3D contacts involving this sphere collider.

Set $includeTriggers to false to return only solid contacts. Pass a $layerMask to limit results to matching GameObject layers.

moveAndSlide

public function moveAndSlide(
    Vector3 $delta,
    float $skinWidth = 0.05,
    bool $includeTriggers = false,
    ?int $layerMask = null,
): KinematicMove3DResult

Moves the GameObject by $delta using sphere collision and slides along blocking 3D colliders.

This method is deprecated for new character code. Prefer CharacterController::move() for kinematic movement.

Example

use Lenga\Engine\Core\Behaviour;
use Lenga\Engine\Core\Debug;
use Lenga\Engine\Core\SphereCollider3D;
use Lenga\Engine\Core\Vector3;

class BallPhysics extends Behaviour
{
    public function start(): void
    {
        $collider = $this->getComponent(SphereCollider3D::class);
        $collider->radius = 0.5;
        $collider->offset = new Vector3(0.0, 0.0, 0.0);
    }

    public function update(): void
    {
        $collider = $this->getComponent(SphereCollider3D::class);

        // Check if touching any objects
        if ($collider->isTouching()) {
            $contacts = $collider->getContacts();
            foreach ($contacts as $contact) {
                Debug::log('In contact with: ' . $contact->gameObject->name);
            }
        }
    }
}