
| Manual | Physics | Operations | Others |
This module is a Beta feature. There may be unrevealed bugs, and the final API may be different from the current version.
Bitty Engine is integrated with the Chipmunk2D physics library to offer 2D dynamics simulation in Lua. The knowledges of the concepts and programming interfaces in Chipmunk2D are widely applicable to programming it in Bitty Engine.
Reading the official documentations would be helpful to understand and master this module, although the original Chipmunk2D is written in C. You’ll find that almost every interface has its counterpart, i.e. the Lua operations in Bitty Engine Physics.Shape.new(Physics.Shape.Circle, ...), Physics.Body.new(...), body.position = ..., space:addBody(...) are equivalent to the C version cpCircleShapeNew(...), cpBodyNew(...), cpBodySetPosition(...), cpSpaceAddBody(...) in Chipmunk2D.
First of all, this module is just a physics engine. It has nothing to do with graphics, it is only responsible for receiving input data, and outputing calculation result.
If your program doesn’t contain complicated behaviours, then the generic algorithms and solutions would be capable. However, it is worth the time and effort required to learn it, because this module helps you simulate how objects interact with each other in a 2D world.
The physics module encapsuled complex calculations and procedures, exposed necessary interfaces, and runs at a decent speed. It uses some of the generic structures, i.e. Vec2, Rect to represent point, bounding box, etc.
Space
Spaces are containers for simulating objects in the physics module. You add bodies, shapes and joints to a space and then update the space as a whole. They control how all the rigid bodies, shapes, and constraints interact together.
Shape
By attaching shapes to bodies, you can define the a body’s shape. You can attach as many shapes to a single body as you need to in order to define a complex shape. Shapes contain the surface properties of an object such as how much friction or elasticity it has.
Body
A rigid body holds the physical properties of an object (mass, position, rotation, velocity, etc.). It does not have a shape until you attach one or more collision shapes to it. Rigid bodies generally tend to have a 1:1 correlation to sprites in a program. You should structure your program so that you use the position and rotation of the rigid body for drawing your sprite.
Constraint
Constraints and joints describe how bodies are attached to each other.
Arbiter
The physics module’s arbiter struct encapsulates a pair of colliding shapes and all of the data about their collision. Arbiters are created when a collision starts, and persist until those shapes are no longer colliding, thus you only get weak references to arbiters in code.
All the programming interfaces of the physics module are packed in a named table Physics.
Constants
Physics.NoGroup: used to indicate “no group” for ShapeFilterPhysics.AllCategories: used to indicate “all categories” for ShapeFilterPhysics.WildcardCollisionType: used to indicate “wildcard collision type” for collision handlersStatic Functions
Physics.momentForCircle(m, r1, r2, offset): calculates the moment of inertia for a hollow circle
r1, r2: the inner and outer diameters in no particular order; a solid circle has an inner diameter of 0Physics.areaForCircle(r1, r2): calculates the area of a hollow circlePhysics.momentForSegment(m, a, b, radius): calculates the moment of inertia for a line segment; the endpoints a and b are relative to the bodyPhysics.areaForSegment(a, b, radius): calculates the area of a beveled segment; will always be 0 if radius is 0Physics.momentForPolygon(m, verts, offset, radius): calculates the moment of inertia for a solid polygon shape assuming its center of gravity is at its centroid; the offset is added to each vertexPhysics.areaForPolygon(verts, radius): calculates the signed area of a polygon shape
Physics.centroidForPolygon(verts): calculates the centroid for a polygonPhysics.momentForBox(m, width, height): calculates the moment of inertia for a solid box centered on the bodyPhysics.momentForBox(m, box): calculates the moment of inertia for a solid box centered on the body
box: the specific box RectPhysics.convexHull(verts, tol = 0): calculates the convex hull of a given set of points
tol: the allowed amount to shrink the hull when simplifying it; a tolerance of 0.0 creates an exact hullVec2, and a secondary value for where the first vertex in the hull came fromPhysics.closetPointOnSegment(p, a, b): calculates the closest point on the line segment a-b to the point pConstructors
Physics.Space.new(): constructs a space objectObject Fields
space.id: readonly, gets the ID of the Space; you can use it to identify a Space object, i.e. in a Lua tablespace.iterations: gets or sets the iterations of the Space, defaults to 10; iterations allow you to control the accuracy of the solverspace.gravity: gets or sets the global gravity applied to the Space as Vec2, defaults to Vec2.new(0, 0), can be overridden on a per Body basis by writing custom integration functions; changing the gravity will activate all sleeping bodies in the Spacespace.damping: gets or sets the amount of simple damping to apply to the Space, defaults to 1; a value of 0.9 means that each Body will lose 10% of its velocity per second; like gravity, it can be overridden on a per Body basisspace.idleSpeedThreshold: gets or sets the speed threshold for a Body to be considered idle; the default value of 0 means the Space estimates a good threshold based on gravityspace.sleepTimeThreshold: gets or sets the sleep time threshold; the default value of infinity disables the sleeping featurespace.collisionSlop: gets or sets the amount of overlap between shapes that is allowed, defaults to 0.1; to improve stability, set this as high as you can without noticable overlappingspace.collisionBias: gets or sets the bias value controls what percentage of overlap remains unfixed after a second; the physics module allows fast moving objects to overlap, then fixes the overlap over time; overlapping objects are unavoidable even if swept collisions are supported, and this is an efficient and stable way to deal with overlapping objects; defaults to ~0.2%, with range of values from 0.0 to 1.0, but using 0 is not recommended for stability reasons; the default value is calculated as (1.0 - 0.1) ^ 60.0 meaning that it attempts to correct 10% of error ever 1/60th of a second; note that very very few programs will need to change this valuespace.collisionPersistence: gets or sets the number of frames the Space keeps collision solutions around for, defaults to 3, and very very very few programs will need to change this value; helps prevent jittering contacts from getting worsespace.staticBody: readonly, gets the dedicated static Body for the Spacespace.currentTimeStep: readonly, gets the current or most recent timestepspace.isLocked: readonly, gets the locked status of the Space; results in true when you cannot add/remove objects from the Space; in particular, spaces are locked when in a collision callback; instead, run your code in a post-step callback insteadspace.bodies: readonly, gets all the bodies in the Spacespace.shapes: readonly, gets all the shapes in the Spacespace.constraints: readonly, gets all the constraints in the SpaceMethods
space:cacheBoundingBox(shape): synchronizes Shape with the Body it has been attached to
space:setPostStepHandler(handler, key): sets the callback to be called before space:step(...) returns; only the first callback registered for any unique value of key will be recorded; it is only allowed to add this type of callback from inside of a collision handler or query callback; note that post-step callbacks are not run in any particular order, if you need to sequence a number of events, you’ll need to put them in a single callback
handler: in form of function (space) end, an invokable object which accepts Spacekey: can be one in Shape, Body, Constraint or integertrue if the callback is scheduled, otherwise false when the key has already been usedspace:setDefaultCollisionHandler(onBegan, onPreSolved = nil, onPostSolved = nil, onSeparated = nil): sets the default collision handler which is used to process all collisions that don’t have a more specific handler
onBegan: in form of function (space, arbiter) return boolean end, an invokable object which accepts Space, Arbiter, returns true if the collision is accepted, otherwise false for ignored until separation; this function is called when two shapes with types that match Physics.WildcardCollisionType begin collidingonPreSolved: in form of function (space, arbiter) return boolean end, an invokable object which accepts Space, Arbiter, returns true if the collision is accepted, otherwise false for ignored until separation; this function is called each step when two shapes with types that match Physics.WildcardCollisionType are colliding; it is called before the collision solver runs so that you can affect a collision’s outcomeonPostSolved: in form of function (space, arbiter) end, an invokable object which accepts Space, Arbiter; this function is called each step when two shapes with types that match Physics.WildcardCollisionType are colliding, it is called after the collision solver runs so that you can read back information about the collision to trigger events in your programonSeparated: in form of function (space, arbiter) end, an invokable object which accepts Space, Arbiter; this function is called when two shapes with types that match Physics.WildcardCollisionType stop collidingspace:setCollisionHandler(typeA, typeB, onBegan, onPreSolved = nil, onPostSolved = nil, onSeparated = nil): sets a collision handler for the specific collision type pair; whenever shapes with collision types typeA and typeB collide, this handler will be used to process the collision events
typeA: the first collision typetypeB: the second collision typeonBegan: in form of function (space, arbiter) return boolean end, an invokable object which accepts Space, Arbiter, returns true if the collision is accepted, otherwise false for ignored until separation; this function is called when two shapes with types that match this collision handler begin collidingonPreSolved: in form of function (space, arbiter) return boolean end, an invokable object which accepts Space, Arbiter, returns true if the collision is accepted, otherwise false for ignored until separation; this function is called each step when two shapes with types that match this collision handler are colliding, it is called before the collision solver runs so that you can affect a collision’s outcomeonPostSolved: in form of function (space, arbiter) end, an invokable object which accepts Space, Arbiter; this function is called each step when two shapes with types that match this collision handler are colliding, it is called after the collision solver runs so that you can read back information about the collision to trigger events in your programonSeparated: in form of function (space, arbiter) end, an invokable object which accepts Space, Arbiter; this function is called when two shapes with types that match this collision handler stop collidingspace:setWildcardHandler(type, onBegan, onPreSolved = nil, onPostSolved = nil, onSeparated = nil): sets a wildcard collision handler for given collision type; this handler will be used any time an object with this type collides with another object, regardless of its type
type: the specific collision typeonBegan: in form of function (space, arbiter) return boolean end, an invokable object which accepts Space, Arbiter, returns true if the collision is accepted, otherwise false for ignored until separation; this function is called when two shapes with types that match this collision handler begin collidingonPreSolved: in form of function (space, arbiter) return boolean end, an invokable object which accepts Space, Arbiter, returns true if the collision is accepted, otherwise false for ignored until separation; this function is called each step when two shapes with types that match this collision handler are colliding, it is called before the collision solver runs so that you can affect a collision’s outcomeonPostSolved: in form of function (space, arbiter) end, an invokable object which accepts Space, Arbiter; this function is called each step when two shapes with types that match this collision handler are colliding, it is called after the collision solver runs so that you can read back information about the collision to trigger events in your programonSeparated: in form of function (space, arbiter) end, an invokable object which accepts Space, Arbiter; this function is called when two shapes with types that match this collision handler stop collidingspace:addShape(shape): adds a Shape to the Space
shape: the specific Shape to be addedShape or nilspace:removeShape(shape): removes a Shape from the Space
shape: the specific Shape to be removedtrue for success, otherwise falsespace:hasShape(shape): gets whether the specific Shape exists in the Space
shape: the specific Shape to checktrue for exists, otherwise falsespace:addBody(body): adds a Body to the Space
body: the specific Body to be addedBody or nilspace:removeBody(body): removes a Body from the Space
body: the specific Body to be removedtrue for success, otherwise falsespace:hasBody(body): gets whether the specific Body exists in the Space
body: the specific Body to checktrue for exists, otherwise falsespace:addConstraint(constraint): adds a Constraint to the Space
constraint: the specific Constraint to be addedConstraint or nilspace:removeConstraint(constraint): removes a Constraint from the Space
constraint: the specific Constraint to be removedtrue for success, otherwise falsespace:hasConstraint(constraint): gets whether the specific Constraint exists in the Space
constraint: the specific Constraint to checktrue for exists, otherwise falsespace:query(point, maxDistance, queryAll = false[, filter]): performs a point query
point: the specific point Vec2 to query atmaxDistance: the max query distancequeryAll: true to query all matched, false to query nearestfilter: the query filterPointQuery if queries all; otherwise one PointQuery or nil if queries nearestspace:query(start, end, radius, queryAll = false[, filter]): performs a segment query
start: the start point Vec2 of the specific segmentend: the end point Vec2 of the specific segmentradius: the query radiusqueryAll: true to query all matched, false to query nearestfilter: the query filterSegmentQuery if queries all; otherwise one SegmentQuery or nil if queries nearestspace:query(bb[, filter]): performs a bounding box query
bb: the specific bounding box Rect to queryfilter: the query filterBoundingBoxQueryspace:query(shape): performs a shape query
shape: the specific Shape to query withShapeQueryspace:foreach(y, handler): iterates all shapes, bodies or constraints in the Space
y: the specific target type to iterate, can be one in Physics.Shape, Physics.Body or Physics.Constrainthandler: in form of function (obj) end, an invokable object which accepts Shape, Body or Constraint objectspace:reindexStatic(): reindexes all static shapesspace:reindexShape(shape): reindexes the specific Shapespace:reindexShapesForBody(body): reindexes all the shapes for a certain Body
space:useSpatialHash(dim, count): switches the Space to use a spatial hash instead of the bounding box tree
space:step(delta): updates the Space for the given time step
space:collect([opt[, threshold]]): collects all unused objects; the C version Chipmunk2D doesn’t offer any automatic memory management, to adapt it to Lua, the Shape, Body and Constraint objects are cached when it is added to a Space, this cache is either manually or automatically collectable; generally you don’t need to call this method manually, the default behaviour is that it will perform an automatic collecting when a specific count of objects (defaults to 1000) went obsolete
opt: can be one in “collect”, “stop”, “restart”, “isrunning”, “threshold”, “limit”, omit to perform a manual collectAvailable options:
| Option | Behaviour | Return value |
|---|---|---|
| “collect” | Performs a manual collecting | - |
| “stop” | Stops automatic collecting | - |
| “restart” | Restarts automatic collecting | - |
| “isrunning” | Gets whether automatic collecting is running | true for running, otherwise false |
| “threshold” | Gets the automatic collecting threshold | The threshold |
| “limit” | Sets the automatic collecting threshold | The threshold |
Constants
Physics.Shape.CirclePhysics.Shape.SegmentPhysics.Shape.PolygonConstructors
Physics.Shape.new(y, ...): constructs a shape object; y can be specialized as the following versionsPhysics.Shape.new(y = Physics.Shape.Circle, body, radius, offset): constructs a circle shape object
body: the Body to attach the circle Shape toradius: the circle radiusoffset: the offset from the Body’s center of gravity in Body local coordinatesPhysics.Shape.new(y = Physics.Shape.Segment, body, a, b, radius): constructs a segment shape object
body: the Body to attach the segment Shape toa: the first endpointb: the second endpointradius: the thickness of the segmentPhysics.Shape.new(y = Physics.Shape.Polygon, body, verts, t, radius): constructs a polygon shape object
body: the Body to attach the polygon Shape toverts: a list of Vec2 vertex, Transform will be applied to every one, a convex hull will be calculated from the vertexes automaticallyt: the specific Transformradius: the polygon Shape will be created with this radius, increasing the size of the ShapePhysics.Shape.new(y = Physics.Shape.Polygon, body, width, height, radius): shortcut to construct a box (polygon) shape object
body: the Body to attach the polygon Shape to; the box will always be centered at the center of gravity of the Bodywidth: the box widthheight: the box heightradius: the Shape’s radius; adding a small radius will bevel the corners and can significantly reduce problems where the box gets stuck on seams in your geometryPhysics.Shape.new(y = Physics.Shape.Polygon, body, box, radius): shortcut to construct a box (polygon) shape object
body: the Body to attach the polygon Shape to; the box will always be centered at the center of gravity of the Bodybox: the box Rectradius: the Shape’s radius; adding a small radius will bevel the corners and can significantly reduce problems where the box gets stuck on seams in your geometryObject Fields
shape.id: readonly, gets the ID of the Shape; you can use it to identify a Shape object, i.e. in a Lua tableshape.type: readonly, gets the geometry type of the Shapeshape.space: readonly, gets the Space where the Shape has been attached toshape.body: gets or sets the Body where the Shape has been attached toshape.mass: gets or sets the mass of the Shapeshape.density: gets or sets the density of the Shapeshape.moment: readonly, gets the moment of the Shapeshape.area: readonly, gets the area of the Shapeshape.centerOfGravity: readonly, gets the center of gravity of the Shapeshape.boundingBox: readonly, gets the bounding box of the Shapeshape.sensor: gets or sets the sensor of the Shape, a boolean value indicates whether this Shape is a sensor, sensors only call collision callbacks, and never generate real collisionsshape.elasticity: gets or sets the elasticity of the Shape; a value of 0.0 gives no bounce, while a value of 1.0 will give a “perfect” bounce; however due to inaccuracies in the simulation using 1.0 or greater is not recommendedshape.friction: gets or sets the friction coefficientshape.surfaceVelocity: gets or sets the surface velocity of the Shape; useful for creating conveyor belts or players that move around; this value is only used when calculating friction, not resolving the collisionshape.collisionType: gets or sets the collision type of the Shape; you can assign types to collision shapes that trigger callbacks when objects of certain types touchshape.filter: gets or sets the collision filter for this Shapeshape.offset: readonly, gets the offset Vec2 if this is a circle Shapeshape.radius: readonly, gets the radius of the Shapeshape.pointA: readonly, gets the first point Vec2 if this is a segment Shapeshape.pointB: readonly, gets the second point Vec2 if this is a segment Shapeshape.normal: readonly, gets the normal Vec2 if this is a segment Shapeshape.vertexes: readonly, gets the vertex points if this is a polygon Shape, in a list of Vec2Methods
shape:cacheBoundingBox(): synchronizes Shape with the Body it has been attached to
Rectshape:update(t): updates the Shape’s Transform
t: the specific TransformRectshape:query(p): performs a nearest point query, it finds the closest point on the surface of Shape to a specific point
p: the specific point Vec2PointQuery or nil, and a secondary number value for the distance between the points, a negative distance means the point is inside the Shapeshape:query(a, b, radius): performs a segment query against a Shape
a: the start point Vec2 of the specific segmentb: the end point Vec2 of the specific segmentradius: the segment radiusSegmentQuery or nilshape:collides(other): checks whether the two Shapes collide
other: the other ShapeContactsshape:setSegmentNeighbors(prev, next): when you have a number of segment shapes that are all joined together, things can still collide with the “cracks” between the segments; by setting the neighbor segment endpoints you can specify to avoid colliding with the inner parts of the crackConstants
Physics.Body.DynamicPhysics.Body.KinematicPhysics.Body.StaticConstructors
Physics.Body.new(y, ...): constructs a body object; y can be specialized as the following versionsPhysics.Body.new(y = Physics.Body.Dynamic, mass, moment): constructs a body objectPhysics.Body.new(y = Physics.Body.Kinematic): constructs a body objectPhysics.Body.new(y = Physics.Body.Static): constructs a body objectObject Fields
body.id: readonly, gets the ID of the Body; you can use it to identify a Body object, i.e. in a Lua tablebody.isSleeping: readonly, gets the whether the Body is sleepingbody.type: gets or sets the Body’s typebody.space: readonly, gets the Space where the Body has been attached tobody.mass: gets or sets the mass of the Bodybody.moment: gets or sets the moment of the Bodybody.position: gets or sets the position of the Bodybody.centerOfGravity: gets or sets the center of gravity of the Bodybody.velocity: gets or sets the velocity of the Bodybody.force: gets or sets the force of the Bodybody.angle: gets or sets the angle of the Bodybody.angularVelocity: gets or sets the angular velocity of the Bodybody.torque: gets or sets the torque of the Bodybody.rotation: readonly, gets the rotation of the Bodybody.shapes: readonly, gets the Shapes of the Bodybody.constraints: readonly, gets the Constraints of the BodyMethods
When objects in the physics module sleep, they sleep as a group of all objects that are touching or jointed together. When an object is woken up, all of the objects in its group are woken up.
body:activate(): resets the idle timer on this dynamic Body; if it was sleeping, wake it and any other bodies it was touchingbody:activate(filter): activates all bodies touching this static Body; if filter is not nil, then only bodies touching through filter will be awoken
filter: the specific filter Shapebody:sleep(): forces a Body to fall asleep immediately even if it’s in midair; cannot be called from a callbackbody:sleepWithGroup(group): sleeps a group of objects together; you can use this to initialize levels and start stacks of objects in a pre-sleeping state
group: it acts identically to body:sleep() if you pass nil as group by starting a new group; if you pass a sleeping Body for group, Body will be awoken when group is awokenbody:setVelocityHandler(handler): sets the callback used to update a Body’s velocitybody:setPositionHandler(handler): sets the callback used to update a Body’s position; note that it’s not generally recommended to override this unless you call the default position update functionbody:updateVelocity(gravity, damping, delta): default velocity integration functionbody:updatePosition(delta): default position integration function
body:localToWorld(point): converts from Body local coordinates to world space coordinates
point: the specific point to convertVec2body:worldToLocal(point): converts from world space coordinates to Body local coordinates
point: the specific point to convertVec2body:applyForceAtWorldPoint(force, point): adds the force to Body as if applied from the world point
force: the specific force Vec2 to applypoint: the specific point Vec2 to apply tobody:applyForceAtLocalPoint(force, point): adds the local force to Body as if applied from the Body local point
force: the specific force Vec2 to applypoint: the specific point Vec2 to apply tobody:applyImpulseAtWorldPoint(impulse, point): adds the impuse to Body as if applied from the world point
impulse: the specific impulse Vec2 to applypoint: the specific point Vec2 to apply tobody:applyImpulseAtLocalPoint(impulse, point): adds the local impuse to Body as if applied from the Body local point
impulse: the specific impulse Vec2 to applypoint: the specific point Vec2 to apply tobody:getVelocityAtWorldPoint(point): gets the velocity on a Body (in world units) at a point on the Body in world coordinates
point: the specific point Vec2 to get toVec2body:getVelocityAtLocalPoint(point): gets the velocity on a Body (in world units) at a point on the Body in local coordinates
point: the specific point Vec2 to get toVec2body:kineticEnergy(): gets the amount of kinetic energy contained by the Body
Body’s kinetic energybody:foreach(y, iterator):
body:foreach(y, ...): iterates all Shapes, Constraints or Arbiters of this Body; y can be specialized as the following versionsbody:foreach(y = Physics.Shape, iterator): iterates all Shapes of this Body
iterator: in form of function (shape) end, an invokable object which accepts Shapebody:foreach(y = Physics.Constraint, iterator): iterates all Constraints of this Body
iterator: in form of function (constraint) end, an invokable object which accepts Constraintbody:foreach(y = Physics.Arbiter, iterator): iterates all Arbiters of this Body
iterator: in form of function (arbiter) end, an invokable object which accepts ArbiterConstants
Physics.Constraint.DampedRotarySpringPhysics.Constraint.DampedSpringPhysics.Constraint.GearJointPhysics.Constraint.GrooveJointPhysics.Constraint.PinJointPhysics.Constraint.PivotJointPhysics.Constraint.RatchetJointPhysics.Constraint.RotaryLimitJointPhysics.Constraint.SimpleMotorPhysics.Constraint.SlideJointObject Fields
constraint.id: readonly, gets the ID of the Constraint; you can use it to identify a Constraint object, i.e. in a Lua tableconstraint.space: readonly, gets the Space where the Constraint belongs toconstraint.bodyA: readonly, gets the first Body the Constraint is attached toconstraint.bodyB: readonly, gets the second Body the Constraint is attached toconstraint.maxForce: gets or sets the maximum force that this Constraint is allowed to useconstraint.errorBias: gets or sets the maximum force that this Constraint is allowed to use, defaults to infinityconstraint.maxBias: gets or sets the maximum rate at which joint error is corrected, defaults to infinityconstraint.collideBodies: gets or sets whether the two bodies connected by the Constraint are allowed to collide, defaults to falseconstraint.impulse: readonly, gets the last impulse applied by this Constraintconstraint.isDampedRotarySpring: readonly, gets whether the Constraint is a DampedRotarySpringconstraint.isDampedSpring: readonly, gets whether the Constraint is a DampedSpringconstraint.isGearJoint: readonly, gets whether the Constraint is a GearJointconstraint.isGrooveJoint: readonly, gets whether the Constraint is a GrooveJointconstraint.isPinJoint: readonly, gets whether the Constraint is a PinJointconstraint.isPivotJoint: readonly, gets whether the Constraint is a PivotJointconstraint.isRatchetJoint: readonly, gets whether the Constraint is a RatchetJointconstraint.isRotaryLimitJoint: readonly, gets whether the Constraint is a RotaryLimitJointconstraint.isSimpleMotor: readonly, gets whether the Constraint is a SimpleMotorconstraint.isSlideJoint: readonly, gets whether the Constraint is a SlideJointMethods
constraint:setPreSolveHandler(handler): the post-solve handler that is called before the solver runs
handler: in form of function (space, constraint) end, an invokable object which accepts Space and Constraintconstraint:setPostSolveHandler(handler): the post-solve handler that is called before the solver runs
handler: in form of function (space, constraint) end, an invokable object which accepts Space and ConstraintConstructors
Physics.DampedRotarySpring.new(): constructs a damped rotary spring constraintObject Fields
constraint.restAngle: gets or sets the rest length of the springconstraint.stiffness: gets or sets the stiffness of the spring in force/distanceconstraint.damping: gets or sets the damping of the springConstructors
Physics.DampedSpring.new(): constructs a damped spring constraintObject Fields
constraint.anchorA: gets or sets the location of the first anchor relative to the first Bodyconstraint.anchorB: gets or sets the location of the second anchor relative to the second Bodyconstraint.restLength: gets or sets the rest length of the springconstraint.stiffness: gets or sets the stiffness of the spring in force/distanceconstraint.damping: gets or sets the damping of the springConstructors
Physics.GearJoint.new(): constructs a gear joint constraintObject Fields
constraint.phase: gets or sets the phase offset of the gearsconstraint.ratio: gets or sets the ratio of a gear jointConstructors
Physics.GrooveJoint.new(): constructs a groove joint constraintObject Fields
constraint.grooveA: gets or sets the first endpoint of the groove relative to the first Bodyconstraint.grooveB: gets or sets the second endpoint of the groove relative to the second Bodyconstraint.anchorB: gets or sets the location of the second anchor relative to the second BodyConstructors
Physics.PinJoint.new(): constructs a pin joint constraintObject Fields
constraint.anchorA: gets or sets the location of the first anchor relative to the first Bodyconstraint.anchorB: gets or sets the location of the second anchor relative to the second Bodyconstraint.distance: gets or sets the distance the joint will maintain between the two anchorsConstructors
Physics.PivotJoint.new(): constructs a pivot joint constraintObject Fields
constraint.anchorA: gets or sets the location of the first anchor relative to the first Bodyconstraint.anchorB: gets or sets the location of the second anchor relative to the second BodyConstructors
Physics.RatchetJoint.new(): constructs a ratchet joint constraintObject Fields
constraint.angle: gets or sets the angle of the current ratchet toothconstraint.phase: gets or sets the phase offset of the ratchetconstraint.ratchet: gets or sets the angular distance of each ratchetConstructors
Physics.RotaryLimitJoint.new(): constructs a rotary limit joint constraintObject Fields
constraint.min: gets or sets the minimum distance the joint will maintain between the two anchorsconstraint.max: gets or sets the maximum distance the joint will maintain between the two anchorsConstructors
Physics.SimpleMotor.new(): constructs a simple motor constraintObject Fields
constraint.rate: gets or sets the rate of the motorConstructors
Physics.SlideJoint.new(): constructs a slide joint constraintObject Fields
constraint.anchorA: gets or sets the location of the first anchor relative to the first Bodyconstraint.anchorB: gets or sets the location of the second anchor relative to the second Bodyconstraint.min: gets or sets the minimum distance the joint will maintain between the two anchorsconstraint.max: gets or sets the maximum distance the joint will maintain between the two anchorsOperators
arbiter:__len(): readonly, gets the number of contact points for this ArbiterObject Fields
arbiter.id: readonly, gets the ID of the Arbiter; you can use it to identify a Arbiter object, i.e. in a Lua tablearbiter.restitution: gets or sets the restitution (elasticity) that will be applied to the pair of colliding objectsarbiter.friction: gets or sets the friction coefficient that will be applied to the pair of colliding objectsarbiter.surfaceVelocity: gets or sets the relative surface velocity of the two Shapes in contactarbiter.totalImpulse: readonly, gets the total impulse including the friction that was applied by this Arbiter; this property should only be used from a post-solve or post-step callbackarbiter.totalKineticEnergy: readonly, gets the amount of energy lost in a collision including static, but not dynamic friction; this property should only be used from a post-solve or post-step callbackarbiter.contacts: gets or sets the Contact for the Arbiterarbiter.isFirstContact: readonly, gets whether the Arbiter is the first contact, true if this is the first step a pair of objects started colliding, otherwise falsearbiter.isRemoval: readonly, gets the whether the Arbiter is removal, true if the separate callback is due to a Shape being removed from the Space, otherwise falsearbiter.normal: readonly, gets the normal Vec2 of the collisionarbiter.point1A: readonly, gets the position of the first contact point on the surface of the first Shapearbiter.point1B: readonly, gets the position of the first contact point on the surface of the second Shapearbiter.depth1: readonly, gets the depth of the first contact pointarbiter.point2A: readonly, gets the position of the second contact point on the surface of the first Shapearbiter.point2B: readonly, gets the position of the second contact point on the surface of the second Shapearbiter.depth2: readonly, gets the depth of the second contact pointMethods
arbiter:ignore(): marks a collision pair to be ignored until the two objects separate; pre-solve and post-solve callbacks will not be called, but the separate callback will be calledarbiter:getShapes(): gets the Shapes in the order that they were defined in the collision handler associated with this Arbiter; i.e. if you defined the handler as space:setCollisionHandler(1, 2, ...), you you will find that shapeA.collisionType equals to 1 and shapeB.collisionType equals to 2
Shapesarbiter:getBodies(): gets the bodies in the order that they were defined in the collision handler associated with this Arbiter; i.e. if you defined the handler as space:setCollisionHandler(1, 2, ...), you you will find that bodyA.collisionType equals to 1 and bodyB.collisionType equals to 2
arbiter:callWildcardBeginA(space): if you want a custom callback to invoke the wildcard callback for the first collision type, you must call this function explicitly; you must decide how to handle the wildcard’s return value since it may disagree with the other wildcard handler’s return value or your own
arbiter:callWildcardBeginB(space): if you want a custom callback to invoke the wildcard callback for the second collision type, you must call this function explicitly; you must decide how to handle the wildcard’s return value since it may disagree with the other wildcard handler’s return value or your own
arbiter:callWildcardPreSolveA(space): if you want a custom callback to invoke the wildcard callback for the first collision type, you must call this function explicitly; you must decide how to handle the wildcard’s return value since it may disagree with the other wildcard handler’s return value or your own
arbiter:callWildcardPreSolveB(space): if you want a custom callback to invoke the wildcard callback for the second collision type, you must call this function explicitly; you must decide how to handle the wildcard’s return value since it may disagree with the other wildcard handler’s return value or your own
arbiter:callWildcardPostSolveA(space): if you want a custom callback to invoke the wildcard callback for the first collision type, you must call this function explicitlyarbiter:callWildcardPostSolveB(space): if you want a custom callback to invoke the wildcard callback for the second collision type, you must call this function explicitlyarbiter:callWildcardSeparateA(space): if you want a custom callback to invoke the wildcard callback for the first collision type, you must call this function explicitlyarbiter:callWildcardSeparateB(space): if you want a custom callback to invoke the wildcard callback for the second collision type, you must call this function explicitlyConstants
Physics.Transform.IdentityConstructors
Physics.Transform.new(a, b, c, d, tx, ty): constructs a transform objectPhysics.Transform.newTranspose(a, c, tx, b, d, ty): constructs a transform objectPhysics.Transform.newTranslate(translate): constructs a transform object
translate: Vec2Physics.Transform.newScale(scaleX, scaleY): constructs a transform objectPhysics.Transform.newScale(scale): constructs a transform objectPhysics.Transform.newRotate(rot): constructs a transform object
rot: rotation in radians or RotPhysics.Transform.newRigid(translate, radians): constructs a transform object
translate: Vec2Physics.Transform.newRigidInverse(trans): constructs a transform object
trans: TransformPhysics.Transform.newWrap(outer, inner): constructs a transform object
outer: Transforminner: TransformPhysics.Transform.newWrapInverse(outer, inner): constructs a transform object
outer: Transforminner: TransformPhysics.Transform.newOrtho(bb): constructs a transform object
bb: RectPhysics.Transform.newBoneScale(v1, v2): constructs a transform object
v1: Vec2v2: Vec2Physics.Transform.newAxialScale(axis, pivot, scale): constructs a transform object
axis: Vec2pivot: Vec2Operators
transform:__mul(other): multiplies with another Transform
other: the other TransformTransformObject Fields
transform.a: gets or sets the a component of the Transformtransform.b: gets or sets the b component of the Transformtransform.c: gets or sets the c component of the Transformtransform.d: gets or sets the d component of the Transformtransform.tx: gets or sets the tx component of the Transformtransform.ty: gets or sets the ty component of the Transformtransform.inversed: readonly, gets the inverse of a transform matrixMethods
transform:transformPoint(point): transform the specific point Vec2 with this Transform
Vec2transform:transformVector(point): transform the specific vector Vec2 with this Transform
Vec2transform:transformBoundingBox(bb): transform the specific bounding box Rect with this Transform
RectConstructors
Physics.ShapeFilter.new(group, categories, mask): constructs a shape filter objectObject Fields
shapeFilter.group: gets or sets the group of the shape filtershapeFilter.categories: gets or sets the categories of the shape filtershapeFilter.mask: gets or sets the mask of the shape filterObject Fields
contact.pointA: readonly, gets the Contact point on the first Shapecontact.pointB: readonly, gets the Contact point on the second Shapecontact.distance: readonly, gets the penetration distance of the two Shapes; overlapping means it will be negativeOperators
contacts:__len(): readonly, gets the number of contact pointsObject Fields
contacts.shape: readonly, gets the Shapecontacts.normal: readonly, gets the normal of the collisioncontacts.points: readonly, gets the contact points, in a list of ContactObject Fields
pointQuery.shape: readonly, gets the nearest ShapepointQuery.point: readonly, gets the closest point on the Shape’s surface (in world space coordinates)pointQuery.distance: readonly, gets the distance to the point; the distance is negative if the point is inside the shapepointQuery.gradient: readonly, gets the gradient of the signed distance functionObject Fields
segmentQuery.shape: readonly, gets the Shape that was hitsegmentQuery.point: readonly, gets the point of impactsegmentQuery.normal: readonly, gets the normal of the surface hitsegmentQuery.alpha: readonly, gets the normalized distance along the query segment, with range of values from 0.0 to 1.0Object Fields
boundingBoxQuery.shape: readonly, gets the Shape that was hitOperators
shapeQuery:__len(): readonly, gets the number of contact pointsObject Fields
shapeQuery.shape: readonly, gets the ShapeshapeQuery.normal: readonly, gets the normal of the collisionshapeQuery.points: readonly, gets the contact points, in a list of Contact