### Import Planck with Testbed (NPM) Source: https://piqnt.com/planck.js/docs/install Illustrates how to import Planck.js along with its testbed functionality using NPM. This setup is necessary for utilizing the included testing and visualization tools. ```javascript import { World, Testbed } from 'planck/with-testbed'; const world = new World(); const testbed = Testbed.mount(); testbed.start(world); ``` -------------------------------- ### Mount and Start Testbed Sequentially in Planck.js Source: https://piqnt.com/planck.js/docs/api/classes/Testbed Provides an example of mounting the Planck.js testbed first and then starting the simulation and rendering. This is useful for customizing the testbed before it begins. ```javascript const testbed = Testbed.mount(); const world = new World(); // ... configure world ... testbed.start(world); ``` -------------------------------- ### Import World Class from Planck.js (NPM) Source: https://piqnt.com/planck.js/docs/install Demonstrates importing the 'World' class from the Planck.js library after installation via NPM. This is the standard way to begin using Planck.js in your project. ```javascript import { World } from 'planck'; const world = new World(); ``` -------------------------------- ### Initialize and Start Planck.js Testbed Simulation Source: https://piqnt.com/planck.js/docs/testbed Code snippet demonstrating the basic usage of Planck.js Testbed: creating a physics 'World' and then starting the simulation using 'Testbed.start()'. ```javascript // Create a world const world = new World(); // Start simulation const testbed = Testbed.start(world); ``` -------------------------------- ### Install Planck.js via NPM Source: https://piqnt.com/planck.js/docs/install Installs the Planck.js library using npm. After installation, you can import the World class or the entire Planck namespace into your JavaScript code. ```bash npm install planck ``` -------------------------------- ### Include Planck.js via Script Tag Source: https://piqnt.com/planck.js/docs/install Demonstrates how to include the Planck.js library in an HTML file using a script tag pointing to a CDN. It then shows how to access and instantiate the 'World' class. ```html ``` -------------------------------- ### Include Planck with Testbed via Script Tag Source: https://piqnt.com/planck.js/docs/install Shows how to include the Planck.js library with its testbed functionality in an HTML file via a script tag. This enables the use of the testbed for debugging and visualization directly in the browser. ```html
``` -------------------------------- ### Import Planck Namespace (NPM) Source: https://piqnt.com/planck.js/docs/install Shows an alternative method of importing Planck.js via NPM, where the entire 'planck' namespace is imported. This allows access to all classes and functions under the 'planck' object. ```javascript import planck from 'planck'; const world = new planck.World(); ``` -------------------------------- ### Start Testbed Simulation in Planck.js Source: https://piqnt.com/planck.js/docs/api/classes/Testbed Illustrates how to start the Planck.js testbed simulation and rendering process using a provided World object. This method will mount the testbed if it hasn't been already. ```javascript const world = new World(); // ... configure world ... const testbedInterface = Testbed.start(world); ``` -------------------------------- ### Set up Planck.js Testbed Locally from Source Source: https://piqnt.com/planck.js/docs/testbed Commands to clone the Planck.js repository, install dependencies, and run the development server to use the testbed locally. ```bash git clone cd planck.js npm install npm run dev ``` -------------------------------- ### Mount and Start Planck.js Testbed Simulation Source: https://piqnt.com/planck.js/docs/testbed An alternative method for initializing Planck.js Testbed, where the testbed is first mounted and then linked to a newly created physics world for simulation. ```javascript // Mount testbed const testbed = Testbed.mount(); // Create a world const world = new World(); // Start simulation testbed.start(world); ``` -------------------------------- ### Import Testbed in JavaScript (NPM) Source: https://piqnt.com/planck.js/docs/testbed Demonstrates how to import the 'World' and 'Testbed' modules from the Planck.js library after installing it via npm. ```javascript import { World, Testbed } from 'planck/with-testbed'; ``` -------------------------------- ### Configure Planck.js Testbed Viewbox Source: https://piqnt.com/planck.js/docs/testbed Code examples showing how to set the center (x, y) and dimensions (width, height) of the testbed's viewbox, which controls the visible area of the physics simulation in physical units. ```javascript // Viewbox center testbed.x = 0; testbed.y = 0; // Viewbox size testbed.width = 30; testbed.height = 20; ``` -------------------------------- ### Iterate and Wake Up All Bodies in Planck.js World Source: https://piqnt.com/planck.js/docs/world Demonstrates how to retrieve the list of all bodies from a Planck.js World and iterate through them. This example specifically wakes up each body in the simulation. It uses `getBodyList()` and `getNext()` for traversal. ```javascript for (let b = myWorld.getBodyList(); b; b = b.getNext()) { b.setAwake(true); } ``` -------------------------------- ### Create Gear Joint - Planck.js Source: https://piqnt.com/planck.js/docs/joint/gear-joint Example of creating a Gear Joint in Planck.js. This requires two existing bodies (bodyA, bodyB) and two joints (joint1, joint2) which can be revolute or prismatic. The 'ratio' parameter defines the gear ratio, crucial for mechanical simulations. ```javascript new GearJoint({ bodyA: myBodyA, bodyB: myBodyB, joint1: myRevoluteJoint, joint2: myPrismaticJoint, ratio: 2 * Math.PI / myLength, }) ``` -------------------------------- ### Define a Pulley Joint in Planck.js Source: https://piqnt.com/planck.js/docs/joint/pulley Example of how to define a pulley joint using Planck.js. It involves specifying anchor points for two bodies and their corresponding ground anchors, along with an optional ratio for mechanical leverage. This setup simulates a pulley connecting two bodies. ```javascript let anchor1 = myBody1.getWorldCenter(); let anchor2 = myBody2.getWorldCenter(); let groundAnchor1 = Vec2(p1.x, p1.y + 10); let groundAnchor2 = Vec2(p2.x, p2.y + 12); let ratio = 1; new PulleyJoint({}, myBody1, myBody2, groundAnchor1, groundAnchor2, anchor1, anchor2, ratio); ``` -------------------------------- ### setSpringDampingRatio() Source: https://piqnt.com/planck.js/docs/api/classes/WheelJoint Sets or gets the damping ratio for a spring. ```APIDOC ## setSpringDampingRatio() ### Description Set/Get the spring damping ratio. ### Method POST ### Endpoint /websites/piqnt_planck_js/setSpringDampingRatio ### Parameters #### Query Parameters - **ratio** (number) - Required - The damping ratio for the spring. ### Request Example ```json { "ratio": 0.7 } ``` ### Response #### Success Response (200) - **status** (string) - Indicates the success of the operation. #### Response Example ```json { "status": "success" } ``` ``` -------------------------------- ### Instantiate Testbed in Planck.js Source: https://piqnt.com/planck.js/docs/api/classes/Testbed Demonstrates how to create a new instance of the Testbed class in Planck.js. This is the basic constructor for the Testbed. ```javascript const testbed = new Testbed(); ``` -------------------------------- ### getMaxForce() Source: https://piqnt.com/planck.js/docs/api/classes/FrictionJoint Gets the maximum friction force in N. ```APIDOC ## getMaxForce() ### Description Gets the maximum friction force in N. ### Method GET ### Returns number: The maximum friction force. ### Example ```javascript const maxForce = joint.getMaxForce(); ``` ``` -------------------------------- ### Body Navigation Source: https://piqnt.com/planck.js/docs/api/classes/Body Get the next body in the linked list. ```APIDOC ## GET /websites/piqnt_planck_js/getNext ### Description Get the next body in the linked list of bodies. ### Method GET ### Endpoint /websites/piqnt_planck_js/getNext ### Parameters None ### Response #### Success Response (200) - **nextBody** (Body) - The next Body object in the list, or null if this is the last body. #### Response Example ```json { "nextBody": { "id": "body-2", "type": "static" } } ``` ``` -------------------------------- ### getReactionTorque() Source: https://piqnt.com/planck.js/docs/api/classes/FrictionJoint Gets the reaction torque on bodyB in N*m. ```APIDOC ## getReactionTorque(inv_dt: number) ### Description Gets the reaction torque on bodyB in N*m. ### Method GET ### Parameters - **inv_dt** (number) - Required - Inverse time step. ### Returns number: The reaction torque on bodyB. ### Example ```javascript const reactionTorque = joint.getReactionTorque(inv_dt); ``` ``` -------------------------------- ### Testbed Class - Constructors Source: https://piqnt.com/planck.js/docs/api/classes/Testbed Documentation for the constructors available in the Testbed class. ```APIDOC ## Testbed Class ### Description Provides static methods for mounting and starting the testbed, which is used for simulating and rendering physics worlds. ### Constructors #### new Testbed() * **Description**: Initializes a new instance of the Testbed class. * **Returns**: `Testbed` - A new Testbed instance. ``` -------------------------------- ### getMaxTorque() Source: https://piqnt.com/planck.js/docs/api/classes/FrictionJoint Gets the maximum friction torque in N*m. ```APIDOC ## getMaxTorque() ### Description Gets the maximum friction torque in N*m. ### Method GET ### Returns number: The maximum friction torque. ### Example ```javascript const maxTorque = joint.getMaxTorque(); ``` ``` -------------------------------- ### Integrate Planck.js Testbed using CDN Source: https://piqnt.com/planck.js/docs/testbed Provides an HTML structure with a script tag to include Planck.js Testbed from a CDN, followed by JavaScript code to initialize and mount the testbed with a physics world. ```html ``` -------------------------------- ### getAnchorB() Source: https://piqnt.com/planck.js/docs/api/classes/FrictionJoint Gets the anchor point on bodyB in world coordinates. ```APIDOC ## getAnchorB() ### Description Gets the anchor point on bodyB in world coordinates. ### Method GET ### Returns Vec2: The anchor point on bodyB. ### Example ```javascript const anchorB = joint.getAnchorB(); ``` ``` -------------------------------- ### Mount Testbed in Planck.js Source: https://piqnt.com/planck.js/docs/api/classes/Testbed Shows how to mount the Planck.js testbed with optional configurations. It returns a TestbedInterface to further control the simulation. ```javascript const testbedInterface = Testbed.mount({ // options }); ``` -------------------------------- ### getAnchorA() Source: https://piqnt.com/planck.js/docs/api/classes/FrictionJoint Gets the anchor point on bodyA in world coordinates. ```APIDOC ## getAnchorA() ### Description Gets the anchor point on bodyA in world coordinates. ### Method GET ### Returns Vec2: The anchor point on bodyA. ### Example ```javascript const anchorA = joint.getAnchorA(); ``` ``` -------------------------------- ### getReactionForce() Source: https://piqnt.com/planck.js/docs/api/classes/FrictionJoint Gets the reaction force on bodyB at the joint anchor in Newtons. ```APIDOC ## getReactionForce(inv_dt: number) ### Description Gets the reaction force on bodyB at the joint anchor in Newtons. ### Method GET ### Parameters - **inv_dt** (number) - Required - Inverse time step. ### Returns Vec2: The reaction force on bodyB. ### Example ```javascript const reactionForce = joint.getReactionForce(inv_dt); ``` ``` -------------------------------- ### Testbed Class - Methods Source: https://piqnt.com/planck.js/docs/api/classes/Testbed Details on the static methods of the Testbed class for managing the simulation environment. ```APIDOC ## Testbed Class ### Description Provides static methods for mounting and starting the testbed, which is used for simulating and rendering physics worlds. ### Methods #### mount() * **Description**: Mounts the testbed. You can call `start` with a world afterward to begin the simulation and rendering. * **Method**: `static` * **Endpoint**: N/A (Class method) * **Parameters**: * `options` (`TestbedMountOptions`?) - Optional. Options for mounting the testbed. * **Returns**: `TestbedInterface` - An interface for interacting with the mounted testbed. #### start(world) * **Description**: Mounts the testbed if it hasn't been already, and then starts the simulation and rendering process. If you need to customize the testbed before starting, you can first run `const testbed = Testbed.mount()` and then call `testbed.start()`. * **Method**: `static` * **Endpoint**: N/A (Class method) * **Parameters**: * `world` (`World`) - Required. The physics world to simulate and render. * **Returns**: `TestbedInterface` - An interface for interacting with the started testbed. ``` -------------------------------- ### setSpringFrequencyHz() Source: https://piqnt.com/planck.js/docs/api/classes/WheelJoint Sets or gets the spring frequency in hertz. Setting the frequency to zero disables the spring. ```APIDOC ## setSpringFrequencyHz() ### Description Set/Get the spring frequency in hertz. Setting the frequency to zero disables the spring. ### Method POST ### Endpoint /websites/piqnt_planck_js/setSpringFrequencyHz ### Parameters #### Query Parameters - **hz** (number) - Required - The spring frequency in hertz. A value of 0 disables the spring. ### Request Example ```json { "hz": 50.0 } ``` ### Response #### Success Response (200) - **status** (string) - Indicates the success of the operation. #### Response Example ```json { "status": "success" } ``` ``` -------------------------------- ### TestbedInterface Query and Control Methods Source: https://piqnt.com/planck.js/docs/api/interfaces/TestbedInterface Methods for querying physics objects and controlling the simulation state. ```APIDOC ## TestbedInterface Query and Control Methods ### findAll() > **findAll**(`query`): (`Body` | `Fixture` | `Joint`)[] #### Parameters • **query** : `string` #### Returns (`Body` | `Fixture` | `Joint`)[] ### findOne() > **findOne**(`query`): `Body` | `Fixture` | `Joint` #### Parameters • **query** : `string` #### Returns `Body` | `Fixture` | `Joint` ### info() > **info**(`text`): `void` #### Parameters • **text** : `string` #### Returns `void` ### start() > **start**(`world`): `void` #### Parameters • **world** : `World` #### Returns `void` ### status() > **status**(`name`, `value`): `void` ##### Parameters • **name** : `string` • **value** : `any` ##### Returns `void` > **status**(`value`): `void` ##### Parameters • **value** : `string` | `object` ##### Returns `void` ``` -------------------------------- ### Sweep Methods Source: https://piqnt.com/planck.js/docs/api/classes/Sweep Details the methods available for the Sweep class, including advancing the sweep, getting transforms, and normalizing angles. ```APIDOC ## Methods ### advance() > **advance**(`alpha`): `void` Advance the sweep forward, yielding a new initial state. #### Parameters • **alpha** : `number` The new initial time #### Returns `void` * * * ### forward() > **forward**(): `void` #### Returns `void` * * * ### getTransform() > **getTransform**(`xf`, `beta`): `void` Get the interpolated transform at a specific time. #### Parameters • **xf** : `TransformValue` • **beta** : `number` = `0` A factor in [0,1], where 0 indicates alpha0 #### Returns `void` * * * ### normalize() > **normalize**(): `void` normalize the angles in radians to be between -pi and pi. #### Returns `void` * * * ### set() > **set**(`that`): `void` #### Parameters • **that** : `Sweep` #### Returns `void` * * * ### setLocalCenter() > **setLocalCenter**(`localCenter`, `xf`): `void` #### Parameters • **localCenter** : `Vec2Value` • **xf** : `TransformValue` #### Returns `void` * * * ### setTransform() > **setTransform**(`xf`): `void` #### Parameters • **xf** : `TransformValue` #### Returns `void` ``` -------------------------------- ### Display Information and Status in Planck.js Testbed Source: https://piqnt.com/planck.js/docs/testbed Illustrates how to use the 'info()' method to display general text messages and the 'status()' method to display key-value pairs (like score and time) that persist until changed, within the Planck.js Testbed interface. ```javascript // Use info() to print some text on screen testbed.info('Use arrow keys to move player'); testbed.step = function() { // Use status() to print key-values // Testbed will retain value of keys until they are changed testbed.status('score', score); testbed.status('time', time); }; ``` -------------------------------- ### Vec3 Class Constructors Source: https://piqnt.com/planck.js/docs/api/classes/Vec3 Documentation for the various constructors of the Vec3 class, allowing initialization with individual components, an object, or defaults. ```APIDOC ## Vec3 Class Constructors ### new Vec3(x, y, z) Creates a new 3D vector with the specified x, y, and z components. #### Parameters - **x** (number) - The x-component of the vector. - **y** (number) - The y-component of the vector. - **z** (number) - The z-component of the vector. #### Returns `Vec3` - A new Vec3 object. ### new Vec3(obj) Creates a new 3D vector from an object with x, y, and z properties. #### Parameters - **obj** (Vec3Value) - An object containing x, y, and z properties. #### Returns `Vec3` - A new Vec3 object. ### new Vec3() Creates a new 3D vector initialized to zero. #### Returns `Vec3` - A new Vec3 object. ``` -------------------------------- ### Get Type Source: https://piqnt.com/planck.js/docs/api/classes/PolygonShape Retrieves the type of the shape. Returns the string 'polygon'. Useful for identifying the shape type. ```javascript const type = polygonShape.getType(); // Returns "polygon" ``` -------------------------------- ### World Methods Source: https://piqnt.com/planck.js/docs/api/classes/World Provides documentation for various methods of the World class, including force management, body and joint creation/destruction, and simulation state retrieval. ```APIDOC ## Methods ### clearForces() > **clearForces**(): `void` Manually clear the force buffer on all bodies. By default, forces are cleared automatically after each call to `step`. The default behavior is modified by calling `setAutoClearForces`. The purpose of this function is to support sub-stepping. Sub-stepping is often used to maintain a fixed sized time step under a variable frame-rate. When you perform sub-stepping you will disable auto clearing of forces and instead call `clearForces` after all sub-steps are complete in one pass of your game loop. See `World.setAutoClearForces` #### Returns * `void` ``` ```APIDOC ### createBody() #### createBody(def) > **createBody**(`def`?): `Body` Create a rigid body given a definition. No reference to the definition is retained. Warning: This function is locked when a world simulation step is in progress. Use `queueUpdate` to schedule a function to be called after the step. ##### Parameters * **def?** : `BodyDef` - The definition for the body. ##### Returns * `Body` - The newly created Body object. #### createBody(position, angle) > **createBody**(`position`, `angle`?): `Body` ##### Parameters * **position** : `Vec2Value` - The initial position of the body. * **angle?** : `number` - The initial angle of the body in radians. ##### Returns * `Body` - The newly created Body object. ``` ```APIDOC ### createDynamicBody() #### createDynamicBody(def) > **createDynamicBody**(`def`?): `Body` ##### Parameters * **def?** : `BodyDef` - The definition for the dynamic body. ##### Returns * `Body` - The newly created dynamic Body object. #### createDynamicBody(position, angle) > **createDynamicBody**(`position`, `angle`?): `Body` ##### Parameters * **position** : `Vec2Value` - The initial position of the body. * **angle?** : `number` - The initial angle of the body in radians. ##### Returns * `Body` - The newly created dynamic Body object. ``` ```APIDOC ### createJoint() > **createJoint** <`T`>(`joint`): `T` Create a joint to constrain bodies together. No reference to the definition is retained. This may cause the connected bodies to cease colliding. Warning: This function is locked when a world simulation step is in progress. Use `queueUpdate` to schedule a function to be called after the step. #### Type Parameters * **T** _extends_ `Joint` - The type of the joint being created. #### Parameters * **joint** : `T` - The joint definition. #### Returns * `T` - The newly created joint. ``` ```APIDOC ### createKinematicBody() #### createKinematicBody(def) > **createKinematicBody**(`def`?): `Body` ##### Parameters * **def?** : `BodyDef` - The definition for the kinematic body. ##### Returns * `Body` - The newly created kinematic Body object. #### createKinematicBody(position, angle) > **createKinematicBody**(`position`, `angle`?): `Body` ##### Parameters * **position** : `Vec2Value` - The initial position of the body. * **angle?** : `number` - The initial angle of the body in radians. ##### Returns * `Body` - The newly created kinematic Body object. ``` ```APIDOC ### destroyBody() > **destroyBody**(`b`): `boolean` Destroy a body from the world. Warning: This automatically deletes all associated shapes and joints. Warning: This function is locked when a world simulation step is in progress. Use `queueUpdate` to schedule a function to be called after the step. #### Parameters * **b** : `Body` - The body to destroy. #### Returns * `boolean` - True if the body was successfully destroyed, false otherwise. ``` ```APIDOC ### destroyJoint() > **destroyJoint**(`joint`): `void` Destroy a joint. Warning: This may cause the connected bodies to begin colliding. Warning: This function is locked when a world simulation step is in progress. Use `queueUpdate` to schedule a function to be called after the step. #### Parameters * **joint** : `Joint` - The joint to destroy. #### Returns * `void` ``` ```APIDOC ### getAllowSleeping() > **getAllowSleeping**(): `boolean` #### Returns * `boolean` - True if sleeping is enabled, false otherwise. ``` ```APIDOC ### getAutoClearForces() > **getAutoClearForces**(): `boolean` Get the flag that controls automatic clearing of forces after each time step. #### Returns * `boolean` - True if auto-clearing of forces is enabled, false otherwise. ``` ```APIDOC ### getBodyCount() > **getBodyCount**(): `number` #### Returns * `number` - The number of bodies in the world. ``` ```APIDOC ### getBodyList() > **getBodyList**(): `Body` Get the world body list. With the returned body, use `Body.getNext` to get the next body in the world list. A null body indicates the end of the list. #### Returns * `Body` - The head of the world body list. ``` ```APIDOC ### getContactCount() > **getContactCount**(): `number` Get the number of contacts (each may have 0 or more contact points). #### Returns * `number` - The number of contacts in the world. ``` ```APIDOC ### getContactList() > **getContactList**(): `Contact` Get the world contact list. With the returned contact, use `Contact.getNext` to get the next contact in the world list. A null contact indicates the end of the list. Warning: contacts are created and destroyed in the middle of a time step. Use `ContactListener` to avoid missing contacts. #### Returns * `Contact` - The head of the world contact list. ``` ```APIDOC ### getContinuousPhysics() > **getContinuousPhysics**(): `boolean` #### Returns * `boolean` - True if continuous physics is enabled, false otherwise. ``` ```APIDOC ### getGravity() > **getGravity**(): `Vec2` Get the global gravity vector. #### Returns * `Vec2` - The global gravity vector. ``` ```APIDOC ### getJointCount() > **getJointCount**(): `number` #### Returns * `number` - The number of joints in the world. ``` -------------------------------- ### Get Radius Source: https://piqnt.com/planck.js/docs/api/classes/PolygonShape Returns the radius of the shape. For PolygonShape, this typically refers to the distance from the origin to the furthest vertex. ```javascript const radius = polygonShape.getRadius(); ``` -------------------------------- ### Vec3 Instance Methods Source: https://piqnt.com/planck.js/docs/api/classes/Vec3 Documentation for instance methods that operate on a Vec3 object, such as addition, multiplication, negation, and setting values. ```APIDOC ## Vec3 Instance Methods ### add(w) Adds another vector to this vector. #### Parameters - **w** (Vec3Value) - The vector to add. #### Returns `Vec3` - The result of the addition. ### mul(m) Multiplies this vector by a scalar value. #### Parameters - **m** (number) - The scalar value to multiply by. #### Returns `Vec3` - The result of the multiplication. ### neg() Negates this vector. #### Returns `Vec3` - The negated vector. ### set(x, y, z) Sets the components of this vector. #### Parameters - **x** (number) - The new x-component. - **y** (number) - The new y-component. - **z** (number) - The new z-component. #### Returns `Vec3` - The modified vector. ### setZero() Sets all components of this vector to zero. #### Returns `Vec3` - The modified vector. ### sub(w) Subtracts another vector from this vector. #### Parameters - **w** (Vec3Value) - The vector to subtract. #### Returns `Vec3` - The result of the subtraction. ``` -------------------------------- ### Get Child Count Source: https://piqnt.com/planck.js/docs/api/classes/PolygonShape Returns the number of child primitives that make up this shape. For PolygonShape, this is always 1. ```javascript const childCount = polygonShape.getChildCount(); // Returns 1 ``` -------------------------------- ### User Data and Miscellaneous Source: https://piqnt.com/planck.js/docs/api/classes/Body Methods for setting user-defined data and other miscellaneous operations. ```APIDOC ## POST /setUserData ### Description Set user-defined data for the body. ### Method POST ### Endpoint /setUserData ### Parameters #### Request Body - **data** (any) - Required - The user data to set. ### Request Example ```json { "data": {"id": 123, "name": "player1"} } ``` ### Response #### Success Response (200) - **status** (string) - Indicates success. #### Response Example ```json { "status": "success" } ``` ``` -------------------------------- ### Mat33 Constructors Source: https://piqnt.com/planck.js/docs/api/classes/Mat33 Details the constructors for creating a new Mat33 instance. ```APIDOC ## Mat33 Constructors ### new Mat33(a, b, c) Creates a new 3x3 matrix. #### Parameters * **a** (Vec3Value) - The first column vector. * **b** (Vec3Value) - The second column vector. * **c** (Vec3Value) - The third column vector. #### Returns `Mat33` - A new 3x3 matrix. ### new Mat33() Creates a new 3x3 matrix initialized to the zero matrix. #### Returns `Mat33` - A new 3x3 matrix. ``` -------------------------------- ### Class: ContactID Source: https://piqnt.com/planck.js/docs/api/classes/ContactID Contact ids to facilitate warm starting. ContactFeature: The features that intersect to form the contact point. ```APIDOC ## Class: ContactID Contact ids to facilitate warm starting. ContactFeature: The features that intersect to form the contact point. ### Constructor #### `new ContactID()` * **Returns**: `ContactID` ### Properties * **`indexA`** (`number`) - ContactFeature index on shapeA. Defaults to -1. * **`indexB`** (`number`) - ContactFeature index on shapeB. Defaults to -1. * **`key`** (`number`) - Used to quickly compare contact ids. Defaults to -1. * **`typeA`** (`ContactFeatureType`) - ContactFeature type on shapeA. Defaults to `ContactFeatureType.e_unset`. * **`typeB`** (`ContactFeatureType`) - ContactFeature type on shapeB. Defaults to `ContactFeatureType.e_unset`. ### Methods #### `recycle()` * **Returns**: `void` #### `set(that)` * **Parameters**: * **`that`** (`ContactID`) - The ContactID to set. * **Returns**: `void` #### `setFeatures(indexA, typeA, indexB, typeB)` * **Parameters**: * **`indexA`** (`number`) - The index on shapeA. * **`typeA`** (`ContactFeatureType`) - The type on shapeA. * **`indexB`** (`number`) - The index on shapeB. * **`typeB`** (`ContactFeatureType`) - The type on shapeB. * **Returns**: `void` #### `swapFeatures()` * **Returns**: `void` ``` -------------------------------- ### MouseJoint Methods Source: https://piqnt.com/planck.js/docs/api/classes/MouseJoint Lists and describes the methods available for interacting with MouseJoint instances, including getting and setting properties, and overriding methods from the base Joint class. ```APIDOC ## Methods ### getAnchorA() > **getAnchorA**(): `Vec2` Get the anchor point on bodyA in world coordinates. #### Returns * `Vec2` - The anchor point coordinates. #### Overrides `Joint.getAnchorA` --- ### getAnchorB() > **getAnchorB**(): `Vec2` Get the anchor point on bodyB in world coordinates. #### Returns * `Vec2` - The anchor point coordinates. #### Overrides `Joint.getAnchorB` --- ### getBodyA() > **getBodyA**(): `Body` Get the first body attached to this joint. #### Returns * `Body` - The attached body. #### Inherited from `Joint.getBodyA` --- ### getBodyB() > **getBodyB**(): `Body` Get the second body attached to this joint. #### Returns * `Body` - The attached body. #### Inherited from `Joint.getBodyB` --- ### getCollideConnected() > **getCollideConnected**(): `boolean` Get collide connected status. Note: modifying the collide connect flag won't work correctly because the flag is only checked when fixture AABBs begin to overlap. #### Returns * `boolean` - True if connected bodies collide, false otherwise. #### Inherited from `Joint.getCollideConnected` --- ### getDampingRatio() > **getDampingRatio**(): `number` Get the damping ratio (dimensionless). #### Returns * `number` - The damping ratio. --- ### getFrequency() > **getFrequency**(): `number` Get the frequency in Hertz. #### Returns * `number` - The frequency in Hertz. --- ### getMaxForce() > **getMaxForce**(): `number` Get the maximum force in Newtons. #### Returns * `number` - The maximum force allowed. --- ### getNext() > **getNext**(): `Joint` Get the next joint in the world joint list. #### Returns * `Joint` - The next joint. #### Inherited from `Joint.getNext` --- ### getReactionForce() > **getReactionForce**(`inv_dt`): `Vec2` Get the reaction force on bodyB at the joint anchor in Newtons. #### Parameters * **inv_dt** (`number`) - The inverse of the time step. #### Returns * `Vec2` - The reaction force vector. #### Overrides `Joint.getReactionForce` --- ### getReactionTorque() > **getReactionTorque**(`inv_dt`): `number` Get the reaction torque on bodyB in N*m. #### Parameters * **inv_dt** (`number`) - The inverse of the time step. #### Returns * `number` - The reaction torque value. #### Overrides `Joint.getReactionTorque` --- ### getTarget() > **getTarget**(): `Vec2` Get the current target position of the mouse joint. #### Returns * `Vec2` - The target coordinates. --- ### getType() > **getType**(): `string` Get the type of the concrete joint. #### Returns * `string` - The joint type identifier. #### Inherited from `Joint.getType` --- ### getUserData() > **getUserData**(): `unknown` Get the user data associated with this joint. #### Returns * `unknown` - The user data. #### Inherited from `Joint.getUserData` --- ### initVelocityConstraints() > **initVelocityConstraints**(`step`): `void` Initializes velocity constraints for the joint. #### Parameters * **step** (`TimeStep`) - The time step information. #### Returns * `void` #### Overrides `Joint.initVelocityConstraints` --- ### isActive() > **isActive**(): `boolean` Short-cut function to determine if either body connected to the joint is inactive. #### Returns * `boolean` - True if the joint is active, false otherwise. #### Inherited from `Joint.isActive` --- ### setDampingRatio() > **setDampingRatio**(`ratio`): `void` Set the damping ratio (dimensionless). #### Parameters * **ratio** (`number`) - The damping ratio to set. #### Returns * `void` --- ### setFrequency() > **setFrequency**(`hz`): `void` Set the frequency in Hertz. #### Parameters * **hz** (`number`) - The frequency in Hertz. #### Returns * `void` --- ### setMaxForce() > **setMaxForce**(`force`): `void` Set the maximum force in Newtons. #### Parameters * **force** (`number`) - The maximum force to set. #### Returns * `void` --- ### setTarget() > **setTarget**(`target`): `void` Use this to update the target point for the mouse joint. This should be called every time the mouse position changes. #### Parameters * **target** (`Vec2Value`) - The new target position. #### Returns * `void` --- ### setUserData() > **setUserData**(`data`): `void` Set the user data associated with this joint. #### Parameters * **data** (`unknown`) - The data to associate with the joint. #### Returns * `void` #### Inherited from `Joint.setUserData` --- ### shiftOrigin() > **shiftOrigin**(`newOrigin`): `void` Shift the origin for any points stored in world coordinates. #### Parameters * **newOrigin** (`Vec2Value`) - The new origin point. #### Returns * `void` #### Overrides `Joint.shiftOrigin` ``` -------------------------------- ### Implement Game-Loop Callback in Planck.js Testbed Source: https://piqnt.com/planck.js/docs/testbed Demonstrates how to define a function to be executed on each frame of the Planck.js Testbed simulation by assigning it to the 'testbed.step' property. ```javascript testbed.step = function() { // Code to run in each game loop }; ``` -------------------------------- ### Create a Dynamic Box Body Source: https://piqnt.com/planck.js/docs/hello-world Creates a dynamic body, which is capable of moving in response to forces and collisions. The 'type' is set to 'dynamic', and its initial 'position' is specified. Dynamic bodies require mass properties, typically defined by fixtures with non-zero density. ```javascript let body = world.createBody({ type: "dynamic", position: {x: 0, y: 4} }); ``` -------------------------------- ### EdgeShape Get Child Count - Planck.js Source: https://piqnt.com/planck.js/docs/api/classes/EdgeShape Returns the number of child primitives that make up the shape. For an EdgeShape, this is always 1. ```javascript getChildCount(): 1 ``` -------------------------------- ### TestbedInterface Drawing Methods Source: https://piqnt.com/planck.js/docs/api/interfaces/TestbedInterface Methods for drawing various shapes and elements within the Planck.js world. ```APIDOC ## TestbedInterface Drawing Methods ### color() > **color**(`r`, `g`, `b`): `string` #### Parameters • **r** : `number` • **g** : `number` • **b** : `number` #### Returns `string` ### drawAABB() > **drawAABB**(`aabb`, `color`): `void` #### Parameters • **aabb** : `AABBValue` • **color** : `string` #### Returns `void` ### drawChain() > **drawChain**(`points`, `color`): `void` #### Parameters • **points** : `Vec2Value`[] • **color** : `string` #### Returns `void` ### drawCircle() > **drawCircle**(`p`, `r`, `color`): `void` #### Parameters • **p** : `Vec2Value` • **r** : `number` • **color** : `string` #### Returns `void` ### drawEdge() > **drawEdge**(`a`, `b`, `color`): `void` #### Parameters • **a** : `Vec2Value` • **b** : `Vec2Value` • **color** : `string` #### Returns `void` ### drawPoint() > **drawPoint**(`p`, `r`, `color`): `void` #### Parameters • **p** : `Vec2Value` • **r** : `any` • **color** : `string` #### Returns `void` ### drawPolygon() > **drawPolygon**(`points`, `color`): `void` #### Parameters • **points** : `Vec2Value`[] • **color** : `string` #### Returns `void` ### drawSegment() > **drawSegment**(`a`, `b`, `color`): `void` #### Parameters • **a** : `Vec2Value` • **b** : `Vec2Value` • **color** : `string` #### Returns `void` ``` -------------------------------- ### Create a Planck.js Physics World Source: https://piqnt.com/planck.js/docs/hello-world Initializes a new Planck.js physics world. You can optionally specify gravity, which determines the downward acceleration of objects in the simulation. The 'World' object is central to managing all physics objects and their interactions. ```javascript let world = new World({ gravity: {x: 0, y: -10}, }); ``` -------------------------------- ### Getting World Manifold from Contact in JavaScript Source: https://piqnt.com/planck.js/docs/contacts Computes the world positions of contact points using the current body positions. This requires a pre-initialized worldManifold object. ```javascript contact.getWorldManifold(worldManifold); ``` -------------------------------- ### Joint Class Methods Source: https://piqnt.com/planck.js/docs/api/classes/Joint Lists and describes the core methods of the Joint class, including retrieving connected bodies, checking state, and solving constraints. ```APIDOC ## Joint Methods ### getAnchorA(): Vec2 **Description**: Gets the anchor point on the first body (`bodyA`) in world coordinates. **Returns**: - Vec2 - The world coordinates of the anchor point on `bodyA`. --- ### getAnchorB(): Vec2 **Description**: Gets the anchor point on the second body (`bodyB`) in world coordinates. **Returns**: - Vec2 - The world coordinates of the anchor point on `bodyB`. --- ### getBodyA(): Body **Description**: Gets the first body attached to this joint. **Returns**: - Body - The first `Body` object connected by the joint. --- ### getBodyB(): Body **Description**: Gets the second body attached to this joint. **Returns**: - Body - The second `Body` object connected by the joint. --- ### getCollideConnected(): boolean **Description**: Retrieves the state of the `collideConnected` flag. Note: modifying this flag after the joint is created may not work as expected, as it's primarily checked when fixture AABBs begin to overlap. **Returns**: - boolean - `true` if the connected bodies are set to collide, `false` otherwise. --- ### getNext(): Joint **Description**: Retrieves the next joint in the world's joint list. **Returns**: - Joint - The next `Joint` object in the sequence. --- ### getReactionForce(inv_dt: number): Vec2 **Description**: Calculates the reaction force exerted on the second body (`bodyB`) at the joint anchor, in Newtons. This is typically used for debugging or analyzing forces. **Parameters**: - **inv_dt** (number) - The inverse of the time step (1/dt). **Returns**: - Vec2 - The reaction force vector. --- ### getReactionTorque(inv_dt: number): number **Description**: Calculates the reaction torque exerted on the second body (`bodyB`) at the joint anchor, in Newton-meters (N*m). Useful for analyzing rotational forces. **Parameters**: - **inv_dt** (number) - The inverse of the time step (1/dt). **Returns**: - number - The reaction torque value. --- ### getType(): string **Description**: Returns the type of the concrete joint (e.g., 'revolute', 'prismatic'). **Returns**: - string - The type of the joint. --- ### getUserData(): unknown **Description**: Retrieves any user-defined data associated with the joint. **Returns**: - unknown - The user data. --- ### initVelocityConstraints(step: TimeStep): void **Description**: Initializes velocity constraints for the joint during the physics step. This is an abstract method and must be implemented by concrete joint subclasses. **Parameters**: - **step** (TimeStep) - The current time step information. **Returns**: - void --- ### isActive(): boolean **Description**: A shortcut method to determine if either of the bodies connected to the joint is currently inactive. **Returns**: - boolean - `true` if at least one connected body is inactive, `false` otherwise. --- ### setUserData(data: unknown): void **Description**: Sets user-defined data to be associated with the joint. **Parameters**: - **data** (unknown) - The data to associate with the joint. **Returns**: - void --- ### shiftOrigin(newOrigin: Vec2Value): void **Description**: Shifts the origin for any points stored in world coordinates. This is useful for large world simulations where the origin might need to be moved. **Parameters**: - **newOrigin** (Vec2Value) - The new origin point. **Returns**: - void --- ### solvePositionConstraints(step: TimeStep): boolean **Description**: Solves the position constraints for the joint during the physics step. This is an abstract method and must be implemented by concrete joint subclasses. It returns `true` if the position errors are within tolerance. **Parameters**: - **step** (TimeStep) - The current time step information. **Returns**: - boolean - `true` if position constraints are satisfied within tolerance, `false` otherwise. --- ### solveVelocityConstraints(step: TimeStep): void **Description**: Solves the velocity constraints for the joint during the physics step. This is an abstract method and must be implemented by concrete joint subclasses. **Parameters**: - **step** (TimeStep) - The current time step information. **Returns**: - void ``` -------------------------------- ### TestbedInterface Event Callbacks Source: https://piqnt.com/planck.js/docs/api/interfaces/TestbedInterface Event callback properties for handling user input like key presses. ```APIDOC ## TestbedInterface Event Callbacks ### keydown() > `optional` **keydown** : (`keyCode`, `label`) => `void` callback, to be implemented by user #### Parameters • **keyCode** : `number` • **label** : `string` #### Returns `void` ### keyup() > `optional` **keyup** : (`keyCode`, `label`) => `void` callback, to be implemented by user #### Parameters • **keyCode** : `number` • **label** : `string` #### Returns `void` ### step() > `optional` **step** : (`dt`, `t`) => `void` callback, to be implemented by user #### Parameters • **dt** : `number` • **t** : `number` #### Returns `void` ``` -------------------------------- ### EdgeShape Get Type - Planck.js Source: https://piqnt.com/planck.js/docs/api/classes/EdgeShape Returns the type of the shape, which is the string "edge" for an EdgeShape. This is useful for downcasting to concrete shape types. ```javascript getType(): "edge" ``` -------------------------------- ### Create a Static Platform Body Source: https://piqnt.com/planck.js/docs/hello-world Creates a static body to act as a platform within the physics world. Static bodies are immovable and do not collide with other static bodies. The 'type' is set to 'static', and its 'position' and 'angle' are defined. This is the first step in defining a physical object. ```javascript let platform = world.createBody({ type: "static", position: {x: 0, y: -10}, angle: Math.PI * 0.1 }); ``` -------------------------------- ### EdgeShape Get Radius - Planck.js Source: https://piqnt.com/planck.js/docs/api/classes/EdgeShape Returns the radius of the shape. For an EdgeShape, this is typically 0 unless a specific radius is applied for features like 'rounded edges'. ```javascript getRadius(): number ```