Module org.dyn4j

Class Body

    • Field Detail

      • mass

        protected Mass mass
        The Mass information
      • velocity

        protected Vector2 velocity
        The current linear velocity
      • angularVelocity

        protected double angularVelocity
        The current angular velocity
      • linearDamping

        protected double linearDamping
        The Body's linear damping
      • angularDamping

        protected double angularDamping
        The Body's angular damping
      • gravityScale

        protected double gravityScale
        The per body gravity scale factor
      • transform0

        Transform transform0
        The beginning transform for CCD
      • world

        World world
        The world this body belongs to
      • sleepTime

        double sleepTime
        The time that the Body has been waiting to be put sleep
      • force

        Vector2 force
        The current force
      • torque

        double torque
        The current torque
      • forces

        final java.util.List<Force> forces
        The force accumulator
      • torques

        final java.util.List<Torque> torques
        The torque accumulator
      • joints

        final java.util.List<JointEdge> joints
        The Body's joints
    • Constructor Detail

      • Body

        public Body()
        Default constructor.
      • Body

        public Body​(int fixtureCount)
        Optional constructor.

        Creates a new Body using the given estimated fixture count. Assignment of the initial fixture count allows sizing of internal structures for optimal memory/performance. This estimated fixture count is not a limit on the number of fixtures.

        Parameters:
        fixtureCount - the estimated number of fixtures
        Throws:
        java.lang.IllegalArgumentException - if fixtureCount less than zero
        Since:
        3.1.1
    • Method Detail

      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • setMass

        @Deprecated
        public Body setMass()
        Deprecated.
        removed in 3.2.0 use setMass(MassType) instead
        This method should be called after fixture modification is complete.

        This method will calculate a total mass for the body given the masses of the fixtures.

        This method will always set this body's mass type to Normal.

        Returns:
        Body this body
      • setMass

        public Body setMass​(MassType type)
        This method should be called after fixture modification is complete.

        This method will calculate a total mass for the body given the masses of the attached fixtures.

        A MassType can be used to create special mass types.

        Parameters:
        type - the mass type
        Returns:
        Body this body
      • setMass

        public Body setMass​(Mass mass)
        Explicitly sets this Body's mass information.
        Parameters:
        mass - the new Mass
        Returns:
        Body this body
        Throws:
        java.lang.NullPointerException - if the given mass is null
      • setMassType

        public Body setMassType​(MassType type)
        Sets the MassType of this Body.

        This method does not compute/recompute the mass of the body but solely sets the mass type to one of the special types.

        Since its possible to create a Mass object with zero mass and/or zero inertia (Mass m = new Mass(new Vector2(), 0, 0); for example), setting the type to something other than MassType.INFINITE can have undefined results.

        Parameters:
        type - the desired type
        Returns:
        Body this body
        Throws:
        java.lang.NullPointerException - if the given mass type is null
        Since:
        2.2.3
      • setRotationDiscRadius

        protected void setRotationDiscRadius()
        Computes the rotation disc for this Body.

        This method requires that the center of mass be computed first.

        The rotation disc radius is the radius, from the center of mass, of the disc that encompasses the entire body as if it was rotated 360 degrees.

        Since:
        2.0.0
        See Also:
        AbstractCollidable.getRotationDiscRadius()
      • getMass

        public Mass getMass()
        Returns this Body's mass information.
        Returns:
        Mass
      • applyForce

        public Body applyForce​(Vector2 force)
        Applies the given force to this Body.

        This method will wake-up the body if its sleeping.

        This method does not apply the force if this body returns zero from the Mass.getMass() method.

        The force is not applied immediately, but instead stored in the force accumulator (getAccumulatedForce()). This is to preserve the last time step's computed force (getForce().

        The force is assumed to be in world space coordinates.

        Parameters:
        force - the force
        Returns:
        Body this body
        Throws:
        java.lang.NullPointerException - if force is null
        Since:
        3.1.1
      • applyForce

        public Body applyForce​(Force force)
        Applies the given Force to this Body.

        This method will wake-up the body if its sleeping.

        This method does not apply the force if this body returns zero from the Mass.getMass() method.

        The force is not applied immediately, but instead stored in the force accumulator (getAccumulatedForce()). This is to preserve the last time step's computed force (getForce().

        The force is assumed to be in world space coordinates.

        Parameters:
        force - the force
        Returns:
        Body this body
        Throws:
        java.lang.NullPointerException - if force is null
        Since:
        3.1.1
      • applyTorque

        public Body applyTorque​(double torque)
        Applies the given torque about the center of this Body.

        This method will wake-up the body if its sleeping.

        This method does not apply the torque if this body returns zero from the Mass.getInertia() method.

        The torque is not applied immediately, but instead stored in the torque accumulator (getAccumulatedTorque()). This is to preserve the last time step's computed torque (getTorque().

        Parameters:
        torque - the torque about the center
        Returns:
        Body this body
        Since:
        3.1.1
      • applyTorque

        public Body applyTorque​(Torque torque)
        Applies the given Torque to this Body.

        This method will wake-up the body if its sleeping.

        This method does not apply the torque if this body returns zero from the Mass.getInertia() method.

        The torque is not applied immediately, but instead stored in the torque accumulator (getAccumulatedTorque()). This is to preserve the last time step's computed torque (getTorque().

        Parameters:
        torque - the torque
        Returns:
        Body this body
        Throws:
        java.lang.NullPointerException - if torque is null
        Since:
        3.1.1
      • applyForce

        public Body applyForce​(Vector2 force,
                               Vector2 point)
        Applies the given force to this Body at the given point (torque).

        This method will wake-up the body if its sleeping.

        This method does not apply the force if this body returns zero from the Mass.getMass() method nor will it apply the torque if this body returns zero from the Mass.getInertia() method.

        The force/torque is not applied immediately, but instead stored in the force/torque accumulators (getAccumulatedForce() and getAccumulatedTorque()). This is to preserve the last time step's computed force (getForce() and torque (getTorque()).

        The force and point are assumed to be in world space coordinates.

        Parameters:
        force - the force
        point - the application point in world coordinates
        Returns:
        Body this body
        Throws:
        java.lang.NullPointerException - if force or point is null
        Since:
        3.1.1
      • applyImpulse

        public Body applyImpulse​(Vector2 impulse)
        Applies a linear impulse to this Body at its center of mass.

        This method will wake-up the body if its sleeping.

        This method does not apply the impulse if this body's mass returns zero from the Mass.getInertia() method.

        NOTE: Applying an impulse differs from applying a force and/or torque. Forces and torques are stored in accumulators, but impulses are applied to the velocities of the body immediately.

        The impulse is assumed to be in world space coordinates.

        Parameters:
        impulse - the impulse to apply
        Returns:
        Body this body
        Throws:
        java.lang.NullPointerException - if impulse is null
        Since:
        3.1.1
      • applyImpulse

        public Body applyImpulse​(double impulse)
        Applies an angular impulse to this Body about its center of mass.

        This method will wake-up the body if its sleeping.

        This method does not apply the impulse if this body's inertia returns zero from the Mass.getInertia() method.

        NOTE: Applying an impulse differs from applying a force and/or torque. Forces and torques are stored in accumulators, but impulses are applied to the velocities of the body immediately.

        Parameters:
        impulse - the impulse to apply
        Returns:
        Body this body
        Since:
        3.1.1
      • applyImpulse

        public Body applyImpulse​(Vector2 impulse,
                                 Vector2 point)
        Applies an impulse to this Body at the given point.

        This method will wake-up the body if its sleeping.

        This method does not apply the linear impulse if this body returns zero from the Mass.getMass() method nor will it apply the angular impulse if this body returns zero from the Mass.getInertia() method.

        NOTE: Applying an impulse differs from applying a force and/or torque. Forces and torques are stored in accumulators, but impulses are applied to the velocities of the body immediately.

        The impulse and point are assumed to be in world space coordinates.

        Parameters:
        impulse - the impulse to apply
        point - the world space point to apply the impulse
        Returns:
        Body this body
        Throws:
        java.lang.NullPointerException - if impulse or point is null
        Since:
        3.1.1
      • clearForce

        public void clearForce()
        Clears the last time step's force on the Body.
      • clearAccumulatedForce

        public void clearAccumulatedForce()
        Clears the forces stored in the force accumulator.

        Renamed from clearForces (3.0.0 and below).

        Since:
        3.0.1
      • clearTorque

        public void clearTorque()
        Clears the last time step's torque on the Body.
      • clearAccumulatedTorque

        public void clearAccumulatedTorque()
        Clears the torques stored in the torque accumulator.

        Renamed from clearTorques (3.0.0 and below).

        Since:
        3.0.1
      • accumulate

        void accumulate​(double elapsedTime)
        Accumulates the forces and torques.
        Parameters:
        elapsedTime - the elapsed time since the last call
        Since:
        3.1.0
      • isStatic

        public boolean isStatic()
        Returns true if this body has infinite mass and the velocity and angular velocity is zero.
        Returns:
        boolean
      • isKinematic

        public boolean isKinematic()
        Returns true if this body has infinite mass and the velocity or angular velocity are NOT zero.
        Returns:
        boolean
      • isDynamic

        public boolean isDynamic()
        Returns true if this body does not have infinite mass.
        Returns:
        boolean
      • getState

        boolean getState​(int mask)
        Returns whether 'state' has all the bits indicated by the binary mask 'mask' set.
        Parameters:
        mask - The binary mask to select bits
        Returns:
        boolean
      • setState

        void setState​(int mask,
                      boolean flag)
        Sets (if flag == true) or clears (if flag == false) the bits indicated by the binary mask 'mask' in the 'state' field.
        Parameters:
        mask - the binary mask to select bits
        flag - true to set, false to clear bits
      • setAutoSleepingEnabled

        public void setAutoSleepingEnabled​(boolean flag)
        Sets the Body to allow or disallow automatic sleeping.
        Parameters:
        flag - true if the Body is allowed to sleep
        Since:
        1.2.0
      • isAutoSleepingEnabled

        public boolean isAutoSleepingEnabled()
        Returns true if this Body is allowed to be put to sleep automatically.
        Returns:
        boolean
        Since:
        1.2.0
      • isAsleep

        public boolean isAsleep()
        Returns true if this Body is sleeping.
        Returns:
        boolean
      • setAsleep

        public void setAsleep​(boolean flag)
        Sets whether this Body is awake or not.

        If flag is true, this body's velocity, angular velocity, force, torque, and accumulators are cleared.

        Parameters:
        flag - true if the body should be put to sleep
      • isActive

        public boolean isActive()
        Returns true if this Body is active.
        Returns:
        boolean
      • setActive

        public void setActive​(boolean flag)
        Sets whether this Body is active or not.
        Parameters:
        flag - true if this Body should be active
      • isOnIsland

        boolean isOnIsland()
        Returns true if this Body has been added to an Island.
        Returns:
        boolean true if this Body has been added to an Island
      • setOnIsland

        void setOnIsland​(boolean flag)
        Sets the flag indicating that the Body has been added to an Island.
        Parameters:
        flag - true if the Body has been added to an Island
      • isBullet

        public boolean isBullet()
        Returns true if this Body is a bullet.
        Returns:
        boolean
        Since:
        1.2.0
        See Also:
        setBullet(boolean)
      • setBullet

        public void setBullet​(boolean flag)
        Sets the bullet flag for this Body.

        A bullet is a very fast moving body that requires continuous collision detection with all other Bodys to ensure that no collisions are missed.

        Parameters:
        flag - true if this Body is a bullet
        Since:
        1.2.0
      • isConnected

        public boolean isConnected​(Body body)
        Returns true if the given Body is connected to this Body by a Joint.

        Returns false if the given body is null.

        Parameters:
        body - the suspect connected body
        Returns:
        boolean
      • isConnected

        public boolean isConnected​(Body body,
                                   boolean collisionAllowed)
        Returns true if the given Body is connected to this Body, given the collision flag, via a Joint.

        If the given collision flag is true, this method will return true only if collision is allowed between the two joined Bodys.

        If the given collision flag is false, this method will return true only if collision is NOT allowed between the two joined Bodys.

        If the Bodys are connected by more than one joint, if any allows collision, then the bodies are considered connected AND allowing collision.

        Returns false if the given body is null.

        Parameters:
        body - the suspect connected body
        collisionAllowed - the collision allowed flag
        Returns:
        boolean
      • isInContact

        public boolean isInContact​(Body body)
        Returns true if the given Body is in collision with this Body.

        Returns false if the given body is null.

        Parameters:
        body - the Body to test
        Returns:
        boolean true if the given Body is in collision with this Body
        Since:
        1.2.0
      • getInitialTransform

        public Transform getInitialTransform()
        Returns the transform of the last iteration.

        This transform represents the last frame's position and orientation.

        Returns:
        Transform
      • createSweptAABB

        public AABB createSweptAABB()
        Returns an AABB that contains the maximal space in which the Collidable exists from the initial transform to the final transform.

        This method takes the bounding circle, using the world center and rotation disc radius, at the initial and final transforms and creates an AABB containing both.

        Returns:
        AABB
        Since:
        3.1.1
      • createSweptAABB

        public AABB createSweptAABB​(Transform initialTransform,
                                    Transform finalTransform)
        Creates a swept AABB from the given start and end Transforms for this Body.

        This method may return a degenerate AABB, where the min == max, if the body has not moved and does not have any fixtures. If this body does have fixtures, but didn't move, an AABB encompassing the initial and final center points is returned.

        Parameters:
        initialTransform - the initial Transform
        finalTransform - the final Transform
        Returns:
        AABB
        Since:
        3.1.1
      • getChangeInPosition

        public Vector2 getChangeInPosition()
        Returns the change in position computed from last frame's transform and this frame's transform.
        Returns:
        Vector2
        Since:
        3.1.5
      • getChangeInOrientation

        public double getChangeInOrientation()
        Returns the change in orientation computed from last frame's transform and this frame's transform.

        This method will return a change in the range [0, 2π). This isn't as useful if the angular velocity is greater than 2π per time step. Since we don't have the timestep here, we can't compute the exact change in this case.

        Returns:
        double
        Since:
        3.1.5
      • getLinearVelocity

        public Vector2 getLinearVelocity()
        Returns the linear velocity.
        Returns:
        Vector2
        Since:
        3.1.5
      • getLinearVelocity

        public Vector2 getLinearVelocity​(Vector2 point)
        Returns the velocity of this body at the given world space point.
        Parameters:
        point - the point in world space
        Returns:
        Vector2
        Since:
        3.1.5
      • setLinearVelocity

        public void setLinearVelocity​(Vector2 velocity)
        Sets the linear velocity.

        Call the setAsleep(boolean) method to wake up the Body if the Body is asleep and the velocity is not zero.

        Parameters:
        velocity - the desired velocity
        Throws:
        java.lang.NullPointerException - if velocity is null
        Since:
        3.1.5
      • setLinearVelocity

        public void setLinearVelocity​(double x,
                                      double y)
        Sets the linear velocity.

        Call the setAsleep(boolean) method to wake up the Body if the Body is asleep and the velocity is not zero.

        Parameters:
        x - the linear velocity along the x-axis
        y - the linear velocity along the y-axis
        Since:
        3.1.5
      • getAngularVelocity

        public double getAngularVelocity()
        Returns the angular velocity.
        Returns:
        double
      • setAngularVelocity

        public void setAngularVelocity​(double angularVelocity)
        Sets the angular velocity in radians per second

        Call the setAsleep(boolean) method to wake up the Body if the Body is asleep and the velocity is not zero.

        Parameters:
        angularVelocity - the angular velocity in radians per second
      • getForce

        public Vector2 getForce()
        Returns the force applied in the last iteration.

        This is the accumulated force from the last iteration.

        Returns:
        Vector2
      • getAccumulatedForce

        public Vector2 getAccumulatedForce()
        Returns the total force currently stored in the force accumulator.
        Returns:
        Vector2
        Since:
        3.0.1
      • getTorque

        public double getTorque()
        Returns the torque applied in the last iteration.

        This is the accumulated torque from the last iteration.

        Returns:
        double
      • getAccumulatedTorque

        public double getAccumulatedTorque()
        Returns the total torque currently stored in the torque accumulator.
        Returns:
        double
        Since:
        3.0.1
      • getLinearDamping

        public double getLinearDamping()
        Returns the linear damping.
        Returns:
        double
        See Also:
        setLinearDamping(double)
      • setLinearDamping

        public void setLinearDamping​(double linearDamping)
        Sets the linear damping.

        Linear damping is used to reduce the linear velocity over time. The default is zero and larger values will cause the linear velocity to reduce faster.

        Parameters:
        linearDamping - the linear damping; must be greater than or equal to zero
        Throws:
        java.lang.IllegalArgumentException - if linearDamping is less than zero
      • getAngularDamping

        public double getAngularDamping()
        Returns the angular damping.
        Returns:
        double
        See Also:
        setAngularDamping(double)
      • setAngularDamping

        public void setAngularDamping​(double angularDamping)
        Sets the angular damping.

        Angular damping is used to reduce the angular velocity over time. The default is zero and larger values will cause the angular velocity to reduce faster.

        Parameters:
        angularDamping - the angular damping; must be greater than or equal to zero
        Throws:
        java.lang.IllegalArgumentException - if angularDamping is less than zero
      • getGravityScale

        public double getGravityScale()
        Returns the gravity scale.
        Returns:
        double
        Since:
        3.0.0
        See Also:
        setGravityScale(double)
      • setGravityScale

        public void setGravityScale​(double scale)
        Sets the gravity scale.

        The gravity scale is a multiplier applied to the acceleration due to gravity before applying the force of gravity to the body. This allows bodies to be affected differently under the same gravity.

        Parameters:
        scale - the gravity scale for this body
        Since:
        3.0.0
      • getJoinedBodies

        public java.util.List<Body> getJoinedBodies()
        Returns a list of Bodys connected by Joints.

        If a body is connected to another body with more than one joint, this method will return just one entry for the connected body.

        Returns:
        List<Body>
        Since:
        1.0.1
      • getJoints

        public java.util.List<Joint> getJoints()
        Returns a list of Joints that this Body is connected with.
        Returns:
        List<Joint>
        Since:
        1.0.1
      • getInContactBodies

        public java.util.List<Body> getInContactBodies​(boolean sensed)
        Returns a list of Bodys that are in contact with this Body.

        Passing a value of true results in a list containing only the sensed contacts for this body. Passing a value of false results in a list containing only normal contacts.

        Calling this method from any of the CollisionListener methods may produce incorrect results.

        If this body has multiple contact constraints with another body (which can happen when either body has multiple fixtures), this method will only return one entry for the in contact body.

        Parameters:
        sensed - true for only sensed contacts; false for only normal contacts
        Returns:
        List<Body>
        Since:
        1.0.1
      • getContacts

        public java.util.List<ContactPoint> getContacts​(boolean sensed)
        Returns a list of ContactPoints

        Passing a value of true results in a list containing only the sensed contacts for this body. Passing a value of false results in a list containing only normal contacts.

        Calling this method from any of the CollisionListener methods may produce incorrect results.

        Modifying the ContactPoints returned is not advised. Use the ContactListener methods instead.

        Parameters:
        sensed - true for only sensed contacts; false for only normal contacts
        Returns:
        List<ContactPoint>
        Since:
        1.0.1