Basics

Guides

API Reference

Menu

Basics

Guides

API Reference

class: PhysicsComponent

[16:7] extends: Component

Wraps FXGL's PhysicsComponent, the jbox2d-backed component that gives an Entity dynamics (mass, friction, restitution, gravity, linear and angular velocity, impulses, forces). Attach with entity.addComponent before adding the entity to a GameWorld; the physics body is created on the first physics tick after the entity is added.

Methods

  • PhysicsComponent ()

    Creates a new PhysicsComponent. Defaults to a static body type; call setBodyType("DYNAMIC") to make it movable.

  • adopt (object Ajo)

    Wraps an existing PhysicsComponent AussomJavaObject.

    • @p Ajo is an AussomJavaObject around a PhysicsComponent.
    • @r A new wrapper.
  • setBodyType (string TypeName)

    Sets the body type: "STATIC" (immovable), "KINEMATIC" (moved by velocity but not affected by forces), or "DYNAMIC" (full physics simulation).

    • @p TypeName is the body type name.
    • @r this object
  • setLinearVelocity (double X, double Y)

    Sets the linear velocity in pixels per second.

    • @p X is the X velocity.
    • @p Y is the Y velocity.
    • @r this object
  • setVelocityX (double X)

    Sets just the X linear velocity.

    • @p X is the X velocity.
    • @r this object
  • setVelocityY (double Y)

    Sets just the Y linear velocity.

    • @p Y is the Y velocity.
    • @r this object
  • getVelocityX ()

    Returns the X linear velocity.

    • @r A double.
  • getVelocityY ()

    Returns the Y linear velocity.

    • @r A double.
  • setAngularVelocity (double Velocity)

    Sets the angular velocity in radians per second.

    • @p Velocity is the angular velocity.
    • @r this object
  • isOnGround ()

    Returns whether this entity's body is currently grounded (touching another collidable below).

    • @r A bool.
  • isMovingX ()

    Returns whether the entity is moving horizontally.

    • @r A bool.
  • isMovingY ()

    Returns whether the entity is moving vertically.

    • @r A bool.
  • isMoving ()

    Returns whether the entity is moving in any direction.

    • @r A bool.
  • onPhysicsInitialized (callback Cb)

    Registers a callback that runs once the body has been created in the physics world (after the first physics tick following entity-add). Use this to set velocity or apply impulses on a dynamic body at spawn -- calls made before the body exists are silently lost otherwise.

    • @p Cb is a callback with no arguments.
    • @r this object
  • setDensity (double Density)

    Sets the fixture density. Required for dynamic bodies to have mass and respond properly to forces. Convenience that builds a FixtureDef internally; reset by any subsequent setDensityFriction call.

    • @p Density is the density value (units per area).
    • @r this object
  • setDensityFriction (double Density, double Friction)

    Sets the fixture density and friction together.

    • @p Density is the density value.
    • @p Friction is the friction coefficient (0.0 to 1.0).
    • @r this object
  • setDensityFrictionRestitution (double Density, double Friction, double Restitution)

    Sets density, friction, and restitution (bounciness) on the fixture in one call.

    • @p Density is the density value.
    • @p Friction is the friction coefficient (0.0 to 1.0).
    • @p Restitution is the restitution coefficient (0.0 = no bounce, 1.0 = elastic).
    • @r this object
  • applyLinearImpulse (double Ix, double Iy, double Px, double Py, bool Wake = true)

    Applies an instantaneous linear impulse to the body at the given point. The most common way to give a dynamic body a one-shot velocity change (e.g. firing a cannonball).

    • @p Ix is the X impulse.
    • @p Iy is the Y impulse.
    • @p Px is the application point X.
    • @p Py is the application point Y.
    • @p Wake selects whether to wake the body if asleep.
    • @r this object
  • applyForce (double Fx, double Fy, double Px, double Py)

    Applies a continuous force to the body at the given point. Use for thrust, wind, or other ongoing forces.

    • @p Fx is the X force.
    • @p Fy is the Y force.
    • @p Px is the application point X.
    • @p Py is the application point Y.
    • @r this object
  • applyForceToCenter (double Fx, double Fy)

    Applies a continuous force to the body's center of mass.

    • @p Fx is the X force.
    • @p Fy is the Y force.
    • @r this object
  • overwritePosition (double X, double Y)

    Teleports the body to the given world position. Use for level resets and respawn -- normal setPosition on the entity is ignored once a dynamic body is active.

    • @p X is the new X.
    • @p Y is the new Y.
    • @r this object
  • overwriteAngle (double Degrees)

    Teleports the body's rotation to the given angle in degrees.

    • @p Degrees is the new rotation.
    • @r this object
  • getLinearVelocity ()

    Returns the linear velocity as an AussomJavaObject around a Point2D. Read components via .invoke("getX") / .invoke("getY").

    • @r An AussomJavaObject around a Point2D.
  • addGroundSensor (object HitBoxObj)

    Adds a ground sensor (a hit box that detects contact below the body). isOnGround returns true while the sensor is in contact with another fixture.

    • @p HitBoxObj is an AussomJavaObject around a HitBox.
    • @r this object
  • addSensor (object HitBoxObj, object HandlerObj)

    Adds a sensor with a custom collision handler. The sensor fires the handler's onCollisionBegin / End callbacks but does not participate in physics resolution.

    • @p HitBoxObj is an AussomJavaObject around a HitBox.
    • @p HandlerObj is an AussomJavaObject around a SensorCollisionHandler.
    • @r this object
  • removeSensor (object HitBoxObj)

    Removes a sensor previously added via addSensor or addGroundSensor.

    • @p HitBoxObj is the HitBox AussomJavaObject originally passed to addSensor.
    • @r this object
  • setRaycastIgnored (bool Ignored)

    Marks this entity as ignored by raycast queries.

    • @p Ignored is a bool.
    • @r this object
  • isRaycastIgnored ()

    Returns whether this entity is ignored by raycast queries.

    • @r A bool.
  • getBody ()

    Escape hatch returning the underlying jbox2d Body as an AussomJavaObject. Use this when something deeper than the wrapper exposes is needed (custom contact filters, iterating fixtures, body-level transforms).

    • @r An AussomJavaObject around the jbox2d Body. Throws IllegalStateException if called before the engine creates the body (i.e. before the first physics tick).
  • applyBodyForce (object ForceVec, object PointVec)

    Applies a body-space linear force at a body-space point. Uses Vec2 wrappers (Box2D-space units), distinct from applyForce which uses Point2D pixels.

  • applyBodyForceToCenter (object ForceVec)

    Applies a body-space linear force at the center of mass.

  • applyBodyLinearImpulse (object ImpulseVec, object PointVec, bool Wake)

    Applies a body-space linear impulse at a body-space point.

    • @p ImpulseVec is a Vec2 wrapper.
    • @p PointVec is a Vec2 wrapper.
    • @p Wake is true to wake the body if asleep.
  • setBodyLinearVelocity (object VelVec)

    Sets the body-space linear velocity from a Vec2 wrapper.

  • setBodyDef (object DefObj)

    Sets the BodyDef used to create the underlying body. Must be called before the engine instantiates the body.

    • @p DefObj is a BodyDef wrapper.
  • setFixtureDef (object DefObj)

    Sets the FixtureDef used to create the body's primary fixture. Must be called before body creation.

    • @p DefObj is a FixtureDef wrapper.
  • setOnPhysicsInitialized (callback Cb)

    Registers a Runnable invoked once after the engine has created the underlying body. Use to safely access getBody().

    • @p Cb is a callback () -> any.
  • onGroundProperty ()

    Returns the on-ground ReadOnlyBooleanProperty for binding.

  • getSensorHandlers ()

    Returns the (HitBox -> SensorCollisionHandler) map as an AJO.