Difference Between Local and World Coordinates in Planck.js?
Understanding the distinction between local and world coordinates is essential for accurately positioning, moving, and managing collisions between rigid bodies in planck.js. While world coordinates define an absolute position within the entire 2D physics simulation canvas, local coordinates are relative strictly to a specific body’s origin and rotation. Mastering how these two coordinate systems interact allows developers to precisely apply forces, attach joints, and translate screen positions into simulation physics.
(Wait, the prompt said “Don’t create any horizontal rules in the article.” Let me remove that immediately and rewrite without it.)
Difference Between Local and World Coordinates in Planck.js?
Understanding the distinction between local and world coordinates is essential for accurately positioning, moving, and managing collisions between rigid bodies in planck.js. While world coordinates define an absolute position within the entire 2D physics simulation canvas, local coordinates are relative strictly to a specific body’s origin and rotation. Mastering how these two coordinate systems interact allows developers to precisely apply forces, attach joints, and translate screen positions into simulation physics.
What are World Coordinates?
World coordinates represent the global frame of reference for your entire physics simulation. Think of the world space as the master map or the stage where all physics bodies exist.
- Fixed Origin: The origin (0, 0) in world space is absolute and never moves, regardless of what the individual shapes or bodies are doing.
- Universal Alignment: Every object’s position, rotation, and velocity are ultimately measured against this global grid before being rendered to the screen.
- Usage: You use world coordinates when you want to spawn a new body at a specific location on the screen, apply a global force like gravity, or determine the distance between two independent objects.
What are Local Coordinates?
Local coordinates represent a frame of reference that is tied directly to an individual rigid body. Imagine standing on a moving ship; your position relative to the ship’s deck stays the same, even though the ship is moving across the ocean.
- Moving Origin: The origin (0, 0) of a local coordinate system is typically the center of mass or the anchor point of that specific body.
- Rotation Bound: If the body rotates, its local coordinate axes rotate along with it. “Up” in local coordinates always points toward the top of the object, even if the object is upside down in the world space.
- Usage: Local coordinates are used when defining the shapes (fixtures) of a body, placing vertices of a polygon, or setting local anchor points for joints.
Key Differences at a Glance
- Independence: World coordinates are completely independent of any single object. Local coordinates are bound entirely to the object’s current position and rotation.
- Stability: If an object moves, its world coordinates change constantly, but its local coordinates (and the local coordinates of its fixtures) remain identical.
- Mathematical Relationship: World coordinates are calculated by taking the local coordinates and applying the body’s current transform (its world position and rotation angle).
Converting Between Systems in Planck.js
Planck.js provides built-in methods on the Body object
to seamlessly convert vectors and points between these two systems.
Transforming Local to World
If you have a point relative to the body and need to know where it sits on the global map, use:
body.getWorldPoint(localPoint): Converts a local position to a world position.body.getWorldVector(localVector): Converts a local direction/vector to a world vector (ignoring the body’s position translation, only accounting for rotation).
Transforming World to Local
If you have a global coordinate (like a mouse click position) and need to know where that is relative to the body’s center, use:
body.getLocalPoint(worldPoint): Converts a global position to the body’s local space.body.getLocalVector(worldVector): Converts a global direction/vector into the body’s local directional space.