LogoLogo
  • Introduction
  • sdks
    • Feature Comparison
    • Unity
    • Godot
    • Defold
    • LÖVE
    • Unreal
  • Quick Start
    • Unity
    • Godot
    • Defold
    • LÖVE
    • Unreal
  • Server Side
    • Entities
    • Chunks
    • Dimensions
    • Events
    • Logging
    • Lua Environment
    • Git Primer
    • Physics
  • HTTP API
    • Authentication
    • Player API
  • Api Reference
    • Entity API
      • Create
      • Message
    • Client API
      • Message
    • Dimension API
      • Create
      • Delete
      • List
    • Events API
      • on_player_join
      • on_player_leave
    • Table API
      • Append
      • Remove
      • Join
    • Util API
      • Time
      • TimeMillis
    • HTTP Request API
      • Get
      • Post
      • Put
      • Delete
    • Physics API
      • NewBody
      • NewBoxShape
      • NewSphereShape
Powered by GitBook
On this page
  • Using Physics
  • Physics Objects
  • Shape
  • Body
  • Vector
  • Collisions
  • Fixed Body Collisions
  • Inelastic Collisions
  • Troubleshooting Collisions
  • Physics API
  • Shape
  • Body
  • Vector
  • Notes
  1. Server Side

Physics

PreviousGit PrimerNextAuthentication

Last updated 2 months ago

[This feature is still in Open Beta and may have some bugs. Please direct any feedback to the Planetary Processing Team on ]

Physics is an optional server-side setting that can be enabled for entities. When physics is applied to an entity, it is assigned a mass, a body (defining its shape), and a velocity. Forces can be applied to modify an entity’s velocity, and collisions between entities are simulated server-side.

Using Physics

Physics must be enabled on an entity for it to have a and interact in . An entity with physics enabled will not collide with another entity that does not have Physics enabled.

  1. Enable Physics:

self.Physics = true
  1. Assign a & :

The body can be either a sphere or a box.

local box = api.physics.NewBoxShape(1, 1, 1) -- width=1, length=1, depth=1
self.Body = api.physics.NewBody(box, 10) --shape=box, mass=10
  1. Set or apply :

--1: Set velocity
self.Body.Velocity = api.physics.NewVector(0, -20, 0)
--2: Apply a one-time force 
local vector = api.physics.NewVector(0, .2, 0)
self.Body:ApplyForce(vector)

Force

self.Body:ApplyForce(vector)

Physics Objects

Shape

Body

Vector

Collisions

  • Collisions are Elastic: No energy is lost during collisions.

  • Collision Detection: Collisions are only detected if entities are overlapping during an update.

Fixed Body Collisions

Inelastic Collisions

Troubleshooting Collisions

If the game's ticks per second (TPS) is low and an entity's speed is high, collisions may be missed. To prevent this, ensure:

EntityMaxSpeedEntityMinDiameter>TPS\frac{Entity Max Speed }{Entity Min Diameter} > TPS EntityMinDiameterEntityMaxSpeed​>TPS

A higher TPS reduces the risk of undetected collisions and unintended behaviour.

Physics API

A number of API calls are available for certain physics-related actions, such as creating a physics Body. They are accessed using the api.physics object available to all server-side scripts.

Methods


Shape

api.physics.NewBoxShape(1, 1, 1)
api.physics.NewSphereShape(1)
Shape
Parameters
Description

Box

width: float length: float depth: float

An axis-aligned bounding box.

Sphere

radius: float

It's just a sphere.


Body

api.physics.NewBody(shape, mass)

Fields

Field
Type
Description

Shape

Shape

The size and shape of the Body's collision boundaries.

Mass

float

The mass of the physics body, which dictates how effective forces are upon it.

Force

Vector

The XYZ vector of force being applied to the physics body on the current update.

Velocity

Vector

The current velocity of the physics body, for how fast it is moving in an XYZ direction.

Methods

Each of these methods takes the Body object self.Body as its first parameter. Hence you may use self.Body:ApplyForce(force) instead of self.Body.ApplyForce(self.Body, force).

Method
Parameters
Returns
Description

ApplyForce

force: vector

None

Apply a one time XYZ force to a physics body. It is applied on the next update.

GetBoxDimensions

None

width: float length: float depth: float

Get the dimensions of a box shaped Body. Errors if the Body is not a Box.

GetRadius

None

radius: float

Get the radius of a Body. Errors if the Body is not a Sphere.


Vector

api.physics.NewVector(1,1,0)

Fields

Field
Type

X

float

Y

float

Z

float

Methods

Each of these methods takes the Vector object as its first parameter. Hence you may use vector_a:Add(vector_b) instead of vector_a.Add(vector_a,vector_b).

v1 = api.physics.NewVector(1,1,0)
v2 = api.physics.NewVector(0,2,2)

sum_v1_v2 = v1:Add(v2)
-- sum_v1_2 is now &{1 3 2}
Method
Parameters
Returns
Description

Add

b: Vector

Vector

Add the corresponding vector value: a.X + b.X a.Y + b.Y a.Z + b.Z

Sub

b: Vector

Vector

Subtract the corresponding vector value: a.X - b.X a.Y - b.Y a.Z - b.Z

Dot

b: Vector

float

Measure the degree of directional similarity between the parameters: a.X*b.X + a.Y*b.Y + a.Z*b.Z

Mul

b: float

Vector

Multiply each vector value by a number:

a.X * b a.Y * b a.Z * b

Cross

b: Vector

Vector

Create a third vector, perpendicular to the parameters:

a.Y*b.Z - a.Z*b.Y a.Z*b.X - a.X*b.Z a.X*b.Y - a.Y*b.X

Normalize

None

Vector

Make a vector unit length:

mag = Sqrt(v.X^2 ​ +v.Y^2 ​ +v.Z^2)

norm = Mul(v, 1/mag)

Magnitude

None

Vector

Calculate the scale of the vector. mag = Sqrt(v.X^2 ​ +v.Y^2 ​ +v.Z^2)

Notes

  • Currently rotation is not supported.

  • Due to synchronization between multiplayer elements, physics is not deterministic. This means that you may experience different physics interactions each time you run a simulation.

The function applies a force to an entity once during a physics update and is then reset immediately afterward. To apply a continuous , you must reapply it each update.

Multiple can be applied within a single update cycle. These are combined and applied together in the next physics update.

The Planetary Processing physics engine currently supports two kinds of 3D : a box or a sphere. are used to define the boundaries for an entity's .

The represents an entity for the game's server-side physics. It defines the entity’s and mass. These attributes determine how the entity interacts with , , and other physics-based calculations.

For physics interactions in 3D space, motion occurs across the X, Y, and Z axes. So it helps to group XYZ values into to represent a single direction, for fields like velocities and .

When two entities with enabled and a intersect, they collide. The physics engine calculates a 3D elastic collision, determining the resulting motion .

To simulate a fixed body, such as a wall, assign it a very high (e.g., 999999). The should be several orders of magnitude larger than other entities that might collide with it. This ensures the remains effectively unchanged during collisions, with all energy transferred to the colliding object.

For absolute certainty that it won’t move, manually set its to 0 every update.

To create an inelastic collision (in which rebound momentum is not conserved), try applying an additional to an object when its changes.

Planetary Processing Physics is in continuous development. Please contact us on our if you would like to request specific functionality.

Discord
Discord
velocity
collisions
Body
Mass
Velocity
Forces
ApplyForce()
force
forces
forces
shapes
Shapes
collision
Body
Body
shape
forces
collisions
vectors
forces
Physics
Body
vectors
mass
mass
body’s velocity
velocity
force
velocity
Elastic Collision
Collision between two box-shaped bodies of the same size/shape. One depicted with a very high mass.
Inelastic collision
Inelastic collision between a faster and slower cube.
Collision fails because of a very low TPS.
Method
Parameters
Returns
Description

shape: Shape mass: float

Body

Create a physics body with a shape and mass capable of collisions.

width: float length: float depth: float

Box

An axis-aligned bounding box.

radius: float

Sphere

It's just a sphere.

NewBody
NewBoxShape
NewSphereShape