githubEdit

Use Physics

Add realistic physics simulation with rigidbodies, colliders, and forces

Learn how to add realistic physics simulation to your Needle Engine projects using the built-in Rapier physics engine.

:::tip When to Use Physics

  • Realistic object movement and interactions

  • Gravity and falling objects

  • Collision detection and response

  • Force-based movement (explosions, wind, throwing)

  • Trigger zones for gameplay mechanics :::


Quick Start

Add physics to an object:

import { Behaviour, Rigidbody } from "@needle-tools/engine";

export class PhysicsExample extends Behaviour {
    start() {
        // Add a rigidbody component at runtime
        const rb = this.gameObject.addNewComponent(Rigidbody);

        // Configure physics properties
        rb.mass = 10;
        rb.drag = 0.5;
        rb.angularDrag = 0.5;

        console.log("Physics enabled!");
    }
}

:::tip Unity/Blender Setup In most cases, you'll add Rigidbody and Collider components directly in Unity or Blender, not in code. The exported glTF will include these physics components automatically. :::


Rigidbodies

Rigidbodies add physics simulation to objects. They respond to forces, gravity, and collisions.

Basic Rigidbody

Rigidbody Properties

Property
Type
Description

mass

number

Mass in kilograms (default: 1)

drag

number

Linear drag (air resistance)

angularDrag

number

Rotational drag

useGravity

boolean

Apply gravity force

isKinematic

boolean

Controlled by code, not physics

lockRotation

boolean

Prevent rotation from physics


Apply Forces

Move rigidbodies using physics forces:

Impulse Force (Instant)

Continuous Force

Torque (Rotation)


Set Velocity Directly

Control rigidbody velocity:


Kinematic Rigidbodies

Kinematic rigidbodies are controlled by code, not physics:

:::tip Kinematic Use Cases

  • Moving platforms

  • Elevators

  • Doors

  • Character controllers

  • Anything that moves predictably but should affect other physics objects :::


Collision Detection

Detect when rigidbodies collide:

Collision Events

Event
When Called

onCollisionEnter

Collision starts

onCollisionStay

Collision continues (every frame)

onCollisionExit

Collision ends

See: Physics Events Referencearrow-up-right for complete API


Trigger Zones

Triggers detect objects without physical collision:

:::tip Trigger vs Collision

  • Triggers: Objects pass through (detection zones, pickups, checkpoints)

  • Collisions: Physical response (solid walls, bouncing balls)

Mark colliders as "Is Trigger" in Unity/Blender to make them triggers. :::


Raycasting

Check for physics objects along a ray:

See: Perform Raycastingarrow-up-right for complete guide


Layers and Filtering

Use layers to control what collides with what:

:::tip Layer Configuration Layer collision matrices are usually configured in Unity's Physics Settings or Blender's physics properties, not in code. :::


Physics Materials

Control friction and bounciness:


Character Controller Example

Complete physics-based character:


Performance Tips

Reduce Physics Updates

Use Sleep

Physics objects automatically sleep when stationary. Don't wake them unnecessarily:

Simplify Colliders

  • Use primitive colliders (box, sphere, capsule) instead of mesh colliders

  • Mesh colliders are much slower, use sparingly

  • For complex shapes, combine multiple primitive colliders


Common Issues

Object Falls Through Floor

Problem: Fast-moving objects pass through thin colliders

Solution: Enable Continuous Collision Detection (CCD) in Unity/Blender, or use thicker colliders

Physics Feels Floaty

Problem: Movement doesn't feel responsive

Solution:

  • Increase drag on rigidbody

  • Use higher gravity (configure in Unity/Blender)

  • Reduce mass for lighter feel

Jittery Movement

Problem: Objects vibrate or jitter

Solution:

  • Reduce angularDrag if spinning too much

  • Check for multiple overlapping colliders

  • Ensure colliders don't start intersecting


Next Steps

Learn more:

Reference:

Last updated