Physics
const physics = require("@vectarine/physics")A Physics API for 2D
This API is experimental and can change.
Functions
Section titled “Functions”physics.newWorld2
Section titled “physics.newWorld2”physics.newWorld2(gravity: Vec.Vec2?, camera: Camera2.Camera2?): World2Create 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
Section titled “physics.newRectangleCollider”physics.newRectangleCollider(size: Vec.Vec2): Collider2Create a rectangle collider, this is a shorthand for newPolygonShape as rectangles are very common
Parameters
size(Vec.Vec2): Vec2
Returns Collider2
physics.newCircleCollider
Section titled “physics.newCircleCollider”physics.newCircleCollider(radius: number): Collider2Create a circle collider
Parameters
radius(number): number
Returns Collider2
physics.newPolygonCollider
Section titled “physics.newPolygonCollider”physics.newPolygonCollider(points: { Vec.Vec2 }): Collider2Create a polygon collider Polygon colliders are more expensive than circle or rectangle colliders
Parameters
points({ Vec.Vec2 }): Vec2[]
Returns Collider2
physics.newVoxelCollider
Section titled “physics.newVoxelCollider”physics.newVoxelCollider( voxelSize: Vec.Vec2, voxelsData: Tile.TilemapResource | Tile.GeneratedTilemap, layer: number, low: Vec.Vec2, high: Vec.Vec2, filledTileIds: { number },): Collider2Create 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 voxelvoxelsData(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 datalow(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.
World2
Section titled “World2”export type World2 = ...World2:removeObject
Section titled “World2:removeObject”World2:removeObject(object: Object2)Remove an object from the world Also removes all joints attached to the object
World2:step
Section titled “World2:step”World2:step(deltaTime: number)Step the physics simulation forward in time If you don’t call this, nothing will move!
World2:getObjects
Section titled “World2:getObjects”World2:getObjects(tags: { string }?): { Object2 }Get all objects containing the given tags. Returns all objects if no tags are given.
World2:getObjectsAtPoint
Section titled “World2:getObjectsAtPoint”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
Section titled “World2:getObjectsInArea”World2:getObjectsInArea(position: Vec.Vec2, size: Vec.Vec2): { Object2 }Get all objects in a given rectangular area
World2:getObjectsIntersectingRay
Section titled “World2:getObjectsIntersectingRay”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
Section titled “World2:getJoints”World2:getJoints(): { Joint2 }Get all joints in the world
Returns { Joint2 }
World2:createObject
Section titled “World2:createObject”World2:createObject( position: Vec.Vec2, mass: number, collider: Collider2, tags: { string }, bodyType: "static" | "dynamic" | "kinematic",): Object2Create 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): Vec2mass(number): number Must be greater than 0collider(Collider2): Collidertags({ string }): { string }bodyType("static" | "dynamic" | "kinematic"): “static” | “dynamic” | “kinematic”
Returns Object2
World2:createDistanceJoint
Section titled “World2:createDistanceJoint”World2:createDistanceJoint(object1: Object2, object2: Object2): Joint2Create 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
Collider2
Section titled “Collider2”export type Collider2 = ...Represents a 2d collider. Used for collisions
Object2
Section titled “Object2”export type Object2 = ...Object2:getPoints
Section titled “Object2:getPoints”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
Section titled “Object2:getContacts”Object2:getContacts(): { Object2 }Returns all objects that are touching this object
Object2:setMass
Section titled “Object2:setMass”Object2:setMass(mass: number)Set the mass of the object
Parameters
mass(number): number Must be greater than 0
Object2:setLockRotation
Section titled “Object2:setLockRotation”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
Section titled “Object2:setLockTranslation”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
Section titled “Object2:setRestitution”Object2:setRestitution(restitution: number)Set the restitution of the object. Higher values make objects bounce more.
Object2:setFriction
Section titled “Object2:setFriction”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
Joint2
Section titled “Joint2”export type Joint2 = ...Joint2:getObject1
Section titled “Joint2:getObject1”Joint2:getObject1(): Object2Get the first object of the joint. Throws an error if the joint is invalid.
Returns Object2
Joint2:getObject2
Section titled “Joint2:getObject2”Joint2:getObject2(): Object2Get the second object of the joint Throws an error if the joint is invalid.
Returns Object2
Joint2:remove
Section titled “Joint2:remove”Joint2:remove()Remove the joint from the world, destroying it.
WorldData
Section titled “WorldData”type WorldData = ...WorldData.camera
Section titled “WorldData.camera”camera: Camera2.Camera2?A camera to show debugging information like colliders and make objects clickable and draggable
WorldData.gravity
Section titled “WorldData.gravity”gravity: Vec.Vec2The 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
Object2Type
Section titled “Object2Type”type Object2Type = ...Object2Type.position
Section titled “Object2Type.position”position: Vec.Vec2Where is object currently is
Object2Type.speed
Section titled “Object2Type.speed”speed: Vec.Vec2Where is object is going (and how fast)
Object2Type.linearDamping
Section titled “Object2Type.linearDamping”linearDamping: numberHow much friction the object has when moving in air. 0 is no friction, 1> like moving in a fluid.
Object2Type.angularDamping
Section titled “Object2Type.angularDamping”angularDamping: numberHow much the object resists to rotation. 0 is no resistance
Joint2Type
Section titled “Joint2Type”type Joint2Type = ...