DistanceJoint2D
A 2D distance joint.
| Type | Name | Interface Description |
|---|---|---|
| Functions | constructor() |
Examples
constructor()
let obj = new APJS.DistanceJoint2D();
Use Case
Rigid 2D pendulum using DistanceJoint2D — anchor (static RigidBody2D) + bob (dynamic RigidBody2D, useGravity=true) connected by a DistanceJoint2D on the bob.
// Optional helper — the pendulum SWINGS WITHOUT THIS SCRIPT (joint + gravity is enough).
// Attach this to the bob if you need to react to each swing pass (count swings, play tick SFX,
// trigger an animation when the bob crosses the bottom). Reads ScreenTransform.localPosition;
// detects sign-flips of the lateral offset relative to the anchor's known X.
@component()
export class PendulumSwingCounter extends APJS.BasicScriptComponent {
@serializeProperty
anchorX: number = 0; // anchor's world-X in localPosition coords (computed at scene-build time)
private st: APJS.ScreenTransform | null = null;
private lastSign: number = 0;
private swings: number = 0;
onStart(): void {
this.st = this.getSceneObject().getComponent("ScreenTransform") as APJS.ScreenTransform;
}
onUpdate(dt: number): void {
if (!this.st) return;
const dx = this.st.localPosition.x - this.anchorX;
const sign = dx === 0 ? 0 : (dx > 0 ? 1 : -1);
if (sign !== 0 && this.lastSign !== 0 && sign !== this.lastSign) {
this.swings++;
// Hook: play SFX, advance game state, etc.
console.log("[Pendulum] swing #" + this.swings);
}
if (sign !== 0) this.lastSign = sign;
}
}