Skip to content

Physics and movement ​

Physics ​

Physics is off until you turn it on, so parts stay where you put them by default. It runs on Jolt.

luau
Scene.PhysicsEnabled = true

local crate = Instance.new("Part")
crate.Position = Vector3.new(0, 20, 0)
crate.Parent = Scene
crate:ApplyImpulse(Vector3.new(0, 0, -20 * crate.Mass))

With it on, every part that isn't Anchored falls with Scene.Gravity, collides and tumbles. Anchored parts don't move. A part with CanCollide false passes through everything, so it falls through the floor unless anchored.

Stepping. 60 steps a second whatever the frame rate, at most four per frame. Parts are drawn interpolated between their last two steps, so motion is smooth on fast screens but up to a step behind. Scripts and queries see the simulated position; a part you move from a script is drawn there at once.

Shapes. A Part collides as its Shape: a ball as wide as its smallest side, a cylinder along X, wedges as drawn. A MeshPart collides as its box.

Moving parts.

  • AssemblyLinearVelocity (studs/s) and AssemblyAngularVelocity (rad/s) can be read and set.
  • ApplyImpulse(impulse) adds impulse / Mass to the velocity.
  • Mass (and GetMass()) is volume times density.
  • You can move or resize parts between steps.

Welds ​

luau
local weld = Instance.new("WeldConstraint")
weld.Part0 = cart
weld.Part1 = wheel
weld.Parent = Scene

A WeldConstraint holds two parts where they were relative to each other when the simulation first saw them, so they move as one and don't collide with each other. Weld to an anchored part to pin something in place. Moving a welded part moves everything welded to it, with physics on or off. A weld lets go while Enabled is false or when either part leaves the Scene. Model:PivotTo moves each part once.

Materials ​

Each Material has a density, friction and elasticity: metal is heavy, ice slides, plastic bounces a little. PhysicalProperties.new(Enum.Material.Ice) gives a material's values. Override per part:

luau
local ball = Instance.new("Part")
ball.Shape = Enum.PartType.Ball
-- bounces back almost as high, whatever it lands on
ball.CustomPhysicalProperties = PhysicalProperties.new(0.7, 0.3, 1, 1, 100)

PhysicalProperties.new(density, friction, elasticity, frictionWeight?, elasticityWeight?). Density is clamped to 0.0001–100, friction to 0–2, elasticity to 0–1. Set it to nil to go back to the material. When two parts touch, their friction and elasticity are averages weighted by FrictionWeight and ElasticityWeight, so a weight of 100 basically decides alone.

Constraints ​

An Attachment is a point with two axes on its parent part. Transform/Position are relative to the part, WorldTransform/WorldPosition are in the world, and Axis/SecondaryAxis are its X and Y. Constraints join Attachment0 to Attachment1 while Enabled:

ConstraintDoes
BallSocketConstraintSame point, free rotation
HingeConstraintSame point and axis, so it turns about that axis only. CurrentAngle in degrees. ActuatorType = Motor spins at AngularVelocity deg/s up to MotorMaxTorque. Servo turns to TargetAngle at AngularSpeed up to ServoMaxTorque. LimitsEnabled keeps it in LowerAngle..UpperAngle (the range always contains 0)
SpringConstraintPulls towards FreeLength apart with Stiffness per stud, slowed by Damping. LimitsEnabled adds MinLength/MaxLength
RopeConstraintAt most Length apart
RodConstraintExactly Length apart

Torques start at 0, so a motor does nothing until you give it one. CurrentLength on springs, ropes and rods is the current distance. Constrained parts still collide with each other.

luau
local axle = Instance.new("Attachment")
axle.Position = Vector3.new(1, 0, 0)
axle.Parent = car
local hub = Instance.new("Attachment")
hub.Parent = wheel

local hinge = Instance.new("HingeConstraint")
hinge.Attachment0 = axle
hinge.Attachment1 = hub
hinge.ActuatorType = Enum.ActuatorType.Motor
hinge.AngularVelocity = 180
hinge.MotorMaxTorque = math.huge
hinge.Parent = Scene

Touch events ​

Touched(other) fires on both parts when they start touching, TouchEnded(other) when they separate, both after the step. A resting part keeps touching what it rests on, even asleep. A CanCollide = false part still touches what it passes through, so an anchored one makes a trigger zone; two such parts don't touch each other. Nothing fires with CanTouch false or physics off.

Characters ​

A CharacterController walks its parent part like a character, without physics. The part is an upright capsule, as wide as its narrower side and as tall as the part.

luau
local body = Instance.new("Part")
body.Size = Vector3.new(2, 5, 2)
body.Parent = Scene

local controller = Instance.new("CharacterController")
controller.Parent = body

Run.Heartbeat:Connect(function()
	controller:Move(Vector3.new(input.X, 0, input.Y))
end)

It slides along walls, steps up ledges up to StepHeight (2), walks slopes up to MaxSlopeAngle (46°), slides off steeper ones and falls with Scene.Gravity. Adding it anchors the part so physics leaves it alone, and the parts of the character's own Model never block it.

  • Move(direction): flattened and capped to length 1, at WalkSpeed (16) studs/s
  • Jump(): jumps JumpHeight (7.2) studs
  • AutoRotate (on) faces the part the way it walks
  • MoveDirection, Velocity, OnGround, and Landed when it lands

Raycasts and shape casts ​

luau
local hit = Scene:Raycast(origin, direction)
if hit then
	print(hit.Instance, hit.Position, hit.Normal, hit.Distance)
end
MethodCasts
Scene:Raycast(origin, direction, params?)A line
Scene:Spherecast(position, radius, direction, params?)A sphere
Scene:Blockcast(transform, size, direction, params?)A box
Scene:Shapecast(part, direction, params?)A copy of the part's shape, never hitting the part itself

Each takes RaycastParams and returns Instance, Position, Normal and Distance, or nil. Shapes move without turning.

  • Ball parts are spheres; everything else is its box. Rays hit a MeshPart's triangles, shape casts its box.
  • A cast doesn't hit a part it starts inside or touching.
  • Parts with both CanCollide and CanQuery false are skipped. CanQuery only matters when CanCollide is false.
  • Results are the same with physics on or off.

Queries, including GetPartBoundsInBox, GetPartBoundsInRadius and GetPartsInPart, use a bounding volume tree kept up to date as parts move: 300 raycasts among 10,000 parts take about 1 ms instead of 150. Overlap results come in no particular order.

Pathfinding ​

luau
local path = Pathfinding:CreatePath({ AgentRadius = 1.5, AgentHeight = 5, AgentCanJump = true })
path:ComputeAsync(enemy.Position, target.Position)
if path.Status == Enum.PathStatus.Success then
	for _, waypoint in path:GetWaypoints() do
		moveTo(waypoint.Position, waypoint.Action == Enum.PathWaypointAction.Jump)
	end
end

ComputeAsync(start, finish) finds a walk across the tops of colliding parts. Waypoints have Position (on the ground), Action (Walk, or Jump where the next leg needs one) and Label.

ParameterDefault
AgentRadius2
AgentHeight5
AgentCanJumptrue
WaypointSpacing4, the longest gap on a straight stretch

Parts that hold a CharacterController are characters, not walls, so a path can start from a character's own position. The agent stands anywhere with AgentHeight of headroom and AgentRadius of clearance, steps up 2 studs, jumps up to 7.2 and drops up to 20. Each end snaps to the floor below it. An end inside a part gives FailStartNotEmpty or FailFinishNotEmpty; no route or no floor gives NoPath.

The search covers up to 64 studs around the two ends, on a 1-stud grid that coarsens for big areas. ComputeAsync doesn't actually yield. Path.Blocked, costs and modifiers aren't built yet.

Apache License 2.0