Skip to content

Physics

const physics = require("@vectarine/physics")

A Physics API for 2D

This API is experimental and can change.

physics.newWorld2(gravity: Vec.Vec2?, camera: Camera2.Camera2?): World2

Create a new physics world A world is a collection of objects that can interact with each other You can optionally pass a camera to show debugging information like colliders and make objects clickable and draggable

physics.newRectangleCollider(size: Vec.Vec2): Collider2

Create a rectangle collider, this is a shorthand for newPolygonShape as rectangles are very common

Parameters

Returns Collider2

physics.newCircleCollider(radius: number): Collider2

Create a circle collider

Parameters

  • radius (number): number

Returns Collider2

physics.newPolygonCollider(points: { Vec.Vec2 }): Collider2

Create a polygon collider Polygon colliders are more expensive than circle or rectangle colliders

Parameters

  • points ({ Vec.Vec2 }): Vec2[]

Returns Collider2

physics.newVoxelCollider(
voxelSize: Vec.Vec2,
voxelsData: Tile.TilemapResource | Tile.GeneratedTilemap,
layer: number,
low: Vec.Vec2,
high: Vec.Vec2,
filledTileIds: { number },
): Collider2

Create a voxel collider A voxel collider is a efficient representation of a 2D grid of square colliders. If the tilemap resource is not loaded, this will produce an error. It is useful for building levels. You can split your terrain in parts and use multiple voxel colliders to have infinite terrain if needed.

You can create multiple voxel colliders on the same region with different filledTileIds to have different objects react differently to entities (for example, wall block the player, spikes hurt them, etc.)

Parameters

  • voxelSize (Vec.Vec2): Vec2 The size of a single voxel
  • voxelsData (Tile.TilemapResource | Tile.GeneratedTilemap): TilemapResource A tilemap resource containing the data of the voxels. The tileId of each voxel will be used to determine if it is filled or not.
  • layer (number): number The layer of the tilemap resource to use for the voxels data
  • low (Vec.Vec2): Vec2 The lowest coordinate of the voxel collider in the tilemap, it is rounded down to get an integer coordinate.
  • high (Vec.Vec2): Vec2 The highest coordinate of the voxel collider in the tilemap, it is rounded down to get an integer coordinate.
  • filledTileIds ({ number }): { number } A list of tileIds that are considered filled and will be colliders. All other tileIds will be considered empty and won’t have colliders.
export type World2 = ...
World2:removeObject(object: Object2)

Remove an object from the world Also removes all joints attached to the object

World2:step(deltaTime: number)

Step the physics simulation forward in time If you don’t call this, nothing will move!

World2:getObjects(tags: { string }?): { Object2 }

Get all objects containing the given tags. Returns all objects if no tags are given.

World2:getObjectsAtPoint(point: Vec.Vec2): { Object2 }

Get all objects at a given point Most of the time, there will only be 0 or 1 object at a given point

World2:getObjectsInArea(position: Vec.Vec2, size: Vec.Vec2): { Object2 }

Get all objects in a given rectangular area

World2:getObjectsIntersectingRay(
position: Vec.Vec2,
direction: Vec.Vec2,
maxTimeOfImpact: number?,
): { { object: Object2, timeOfImpact: number } }

Get all objects intersecting a ray. The direction does not have to be normalized and represents the “velocity” of the ray. For performance reasons, you should set a maximum time of impact which is the maximum time the ray will travel at its speed. The time of impact is the time the ray will travel to reach the object. For normalized directions, this is the distance.

World2:getJoints(): { Joint2 }

Get all joints in the world

Returns { Joint2 }

World2:createObject(
position: Vec.Vec2,
mass: number,
collider: Collider2,
tags: { string },
bodyType: "static" | "dynamic" | "kinematic",
): Object2

Create a new object into the world. Objects are specific to a given world. You can give the object tags to find it later Objects can have 3 body types:

  • static: objects never move (like the ground)
  • dynamic: objects that move and react to collisions (player, enemies, pushable crates, etc.)
  • kinematic: objects that can be manually moved with code but cannot be moved by other objects (like moving platforms or elevators)

Parameters

  • position (Vec.Vec2): Vec2
  • mass (number): number Must be greater than 0
  • collider (Collider2): Collider
  • tags ({ string }): { string }
  • bodyType ("static" | "dynamic" | "kinematic"): “static” | “dynamic” | “kinematic”

Returns Object2

World2:createDistanceJoint(object1: Object2, object2: Object2): Joint2

Create a distance joint between two objects Once the joint is created, the distance between the objects will be fixed. This can be used to create pendulums, or wheels.

Parameters

Returns Joint2

export type Collider2 = ...

Represents a 2d collider. Used for collisions

export type Object2 = ...
Object2:getPoints(): { Vec.Vec2 }

Return the points of the collider in the object space If the collider is not a polygon (for example a circle), return a close approximation. For tilemap colliders, this will return 4 point per filled tile, representing the corner of the tile.

Returns { Vec2 }

Object2:getContacts(): { Object2 }

Returns all objects that are touching this object

Object2:setMass(mass: number)

Set the mass of the object

Parameters

  • mass (number): number Must be greater than 0
Object2:setLockRotation(lock: boolean)

Prevents an object from rotating (objects can rotate by default) It can still move around. Useful for characters that shouldn’t fall over.

Object2:setLockTranslation(lock: boolean)

Prevents an object from moving (objects can move by default) It can still rotate. Useful for rotating platforms that shouldn’t move.

Object2:setRestitution(restitution: number)

Set the restitution of the object. Higher values make objects bounce more.

Object2:setFriction(friction: number)

Set the friction of the object. Friction hinders movement of objects in contacts. This is different from linear damping which exists even when an object is alone in a scene. Higher values make objects slide less. Defaults to 0.5

export type Joint2 = ...
Joint2:getObject1(): Object2

Get the first object of the joint. Throws an error if the joint is invalid.

Returns Object2

Joint2:getObject2(): Object2

Get the second object of the joint Throws an error if the joint is invalid.

Returns Object2

Joint2:remove()

Remove the joint from the world, destroying it.

type WorldData = ...
camera: Camera2.Camera2?

A camera to show debugging information like colliders and make objects clickable and draggable

gravity: Vec.Vec2

The gravity of the world that will be applied to all objects Set to 0 if you want to control the gravity separately for each object

type Object2Type = ...
position: Vec.Vec2

Where is object currently is

speed: Vec.Vec2

Where is object is going (and how fast)

linearDamping: number

How much friction the object has when moving in air. 0 is no friction, 1> like moving in a fluid.

angularDamping: number

How much the object resists to rotation. 0 is no resistance

type Joint2Type = ...