Basics

Guides

API Reference

Menu

Basics

Guides

API Reference

class: PhysicsWorld

[22:7] extends: FxObj

Wraps FXGL's PhysicsWorld, the jbox2d-backed physics simulator that drives every collidable entity in the game. Reached at runtime via fxgl.getPhysicsWorld; obtain a wrapper via the (Ajo) constructor. The main user-facing operations are registering CollisionHandlers to react when entities of two types touch, adjusting world gravity, and casting rays for line-of-sight or hit tests. Note: this wrapper is the registration point for CollisionHandler. After constructing a handler via new CollisionHandler(typeA, typeB) and wiring its callbacks, register it here with physicsWorld.addCollisionHandler(handler).

Methods

  • PhysicsWorld (Ajo = null)

    Creates a PhysicsWorld wrapper. Empty by default (PhysicsWorld is engine-owned and not user-constructible); the (Ajo) form wraps an existing instance from fxgl.getPhysicsWorld.

    • @p Ajo is an optional AussomJavaObject around a PhysicsWorld.
  • addCollisionHandler (object Handler)

    Registers a CollisionHandler with the running physics world. The handler fires for every pair of entities matching the type pair the handler was constructed with.

    • @p Handler is a CollisionHandler wrapper.
    • @r this object
  • removeCollisionHandler (object Handler)

    Removes a previously registered CollisionHandler.

    • @p Handler is a CollisionHandler wrapper.
    • @r this object
  • clearCollisionHandlers ()

    Removes every registered CollisionHandler.

    • @r this object
  • setGravity (double X, double Y)

    Sets the world gravity vector in pixels per second squared. Default in FXGL is (0, 0); a typical 2D platformer uses (0, 600) or similar.

    • @p X is the horizontal gravity.
    • @p Y is the vertical gravity (positive points down on screen).
    • @r this object
  • raycast (double Sx, double Sy, double Ex, double Ey)

    Casts a ray from (Sx, Sy) to (Ex, Ey) and returns the RaycastResult containing the first hit entity (if any) and the hit point.

    • @p Sx is the start X.
    • @p Sy is the start Y.
    • @p Ex is the end X.
    • @p Ey is the end Y.
    • @r An AussomJavaObject around a RaycastResult.
  • clear ()

    Clears all bodies and joints from the world (typically used on level transitions).

    • @r this object
  • toMeters (double Pixels)

    Converts a pixel distance to physics meters.

    • @p Pixels is the pixel distance.
    • @r A double in meters.
  • toPixels (double Meters)

    Converts a physics meters distance to pixels.

    • @p Meters is the meter distance.
    • @r A double in pixels.
  • addRevoluteJoint (object E1, object E2, double Ax1, double Ay1, double Ax2, double Ay2)

    Creates a hinge (revolute) joint between two entities at the given local anchors. Returns an AussomJavaObject around the RevoluteJoint so callers can set motor / limits / etc.

    • @p E1 is the first Entity wrapper.
    • @p E2 is the second Entity wrapper.
    • @p Ax1 is the anchor X on E1.
    • @p Ay1 is the anchor Y on E1.
    • @p Ax2 is the anchor X on E2.
    • @p Ay2 is the anchor Y on E2.
    • @r An AussomJavaObject around the RevoluteJoint.
  • addRopeJoint (object E1, object E2, double Ax1, double Ay1, double Ax2, double Ay2, double Length)

    Creates a rope joint between two entities at the given anchors with the supplied rope length.

    • @p E1 is the first Entity wrapper.
    • @p E2 is the second Entity wrapper.
    • @p Ax1 is the anchor X on E1.
    • @p Ay1 is the anchor Y on E1.
    • @p Ax2 is the anchor X on E2.
    • @p Ay2 is the anchor Y on E2.
    • @p Length is the maximum rope length.
    • @r An AussomJavaObject around the RopeJoint.
  • addRopeJointDefault (object E1, object E2)

    Creates a default rope joint between two entities (uses their current world positions as anchors).

    • @p E1 is the first Entity wrapper.
    • @p E2 is the second Entity wrapper.
    • @r An AussomJavaObject around the RopeJoint.
  • addPrismaticJoint (object E1, object E2, double Ax, double Ay, double Limit)

    Creates a prismatic (sliding) joint between two entities along the supplied axis with the given travel limit.

    • @p E1 is the first Entity wrapper.
    • @p E2 is the second Entity wrapper.
    • @p Ax is the axis X.
    • @p Ay is the axis Y.
    • @p Limit is the travel limit.
    • @r An AussomJavaObject around the PrismaticJoint.
  • addJoint (object E1, object E2, object JointDefObj)

    Creates a generic joint from a JointDef built directly via AJI (WeldJointDef, MotorJointDef, etc.).

    • @p E1 is the first Entity wrapper.
    • @p E2 is the second Entity wrapper.
    • @p JointDefObj is an AussomJavaObject around a JointDef.
    • @r An AussomJavaObject around the created Joint.
  • removeJoint (object JointObj)

    Removes a previously created joint.

    • @p JointObj is an AussomJavaObject around the Joint.
    • @r this object
  • getJBox2DWorld ()

    Returns the underlying jbox2d World as an AussomJavaObject. Use for advanced operations (iterating bodies, custom contact filters) not surfaced by the wrapper.

    • @r An AussomJavaObject around the jbox2d World.
  • toMeterPoint (double X, double Y)

    Converts a JavaFX Point2D (pixels) to a jbox2d Vec2 (meters).

    • @p X is the pixel X.
    • @p Y is the pixel Y.
    • @r An AussomJavaObject around a Vec2.
  • toPixelPoint (object VecObj)

    Converts a jbox2d Vec2 (meters) to a JavaFX Point2D (pixels).

    • @p VecObj is an AussomJavaObject (or Vec2 wrapper) around a Vec2.
    • @r An AussomJavaObject around a Point2D.