Module org.dyn4j

Interface PhysicsWorld<T extends PhysicsBody,​V extends ContactCollisionData<T>>

    • Field Detail

      • EARTH_GRAVITY

        static final Vector2 EARTH_GRAVITY
        Earths gravity constant
      • ZERO_GRAVITY

        static final Vector2 ZERO_GRAVITY
        Zero gravity constant
    • Method Detail

      • update

        boolean update​(double elapsedTime)
        Updates the PhysicsWorld.

        This method will only update the world given the step frequency contained in the Settings object. You can use the StepListener interface to listen for when a step is actually performed. In addition, this method will return true if a step was performed.

        This method performs, at maximum, one simulation step. Any remaining time from the previous call of this method is added to the given elapsed time to determine if a step needs to be performed. If the given elapsed time is usually greater than the step frequency, consider using the update(double, int) method instead.

        Alternatively you can call the updatev(double) method to use a variable time step.

        Parameters:
        elapsedTime - the elapsed time in seconds
        Returns:
        boolean true if the World performed a simulation step
        See Also:
        update(double, int), updatev(double), getAccumulatedTime()
      • update

        boolean update​(double elapsedTime,
                       int maximumSteps)
        Updates the World.

        This method will only update the world given the step frequency contained in the Settings object. You can use the StepListener interface to listen for when a step is actually performed.

        Unlike the update(double) method, this method will perform more than one step based on the given elapsed time. For example, if the given elapsed time + the remaining time from the last call of this method is 2 * step frequency, then 2 steps will be performed. Use the maximumSteps parameter to put an upper bound on the number of steps performed.

        Alternatively you can call the updatev(double) method to use a variable time step.

        Parameters:
        elapsedTime - the elapsed time in seconds
        maximumSteps - the maximum number of steps to perform
        Returns:
        boolean true if the World performed at least one simulation step
        Since:
        3.1.10
        See Also:
        update(double), updatev(double), getAccumulatedTime()
      • update

        boolean update​(double elapsedTime,
                       double stepElapsedTime)
        Updates the World.

        This method will only update the world given the step frequency contained in the Settings object. You can use the StepListener interface to listen for when a step is actually performed. In addition, this method will return true if a step was performed.

        This method performs, at maximum, one simulation step. Any remaining time from the previous call of this method is added to the given elapsed time to determine if a step needs to be performed. If the given elapsed time is usually greater than the step frequency, consider using the update(double, int) method instead.

        The stepElapsedTime parameter provides a way for the World to continue to update at the frequency defined in the Settings object, but advance the simulation by the given time.

        Alternatively you can call the updatev(double) method to use a variable time step.

        Parameters:
        elapsedTime - the elapsed time in seconds
        stepElapsedTime - the time, in seconds, that the simulation should be advanced
        Returns:
        boolean true if the World performed at least one simulation step
        Since:
        3.2.4
        See Also:
        update(double), updatev(double), getAccumulatedTime()
      • update

        boolean update​(double elapsedTime,
                       double stepElapsedTime,
                       int maximumSteps)
        Updates the World.

        This method will only update the world given the step frequency contained in the Settings object. You can use the StepListener interface to listen for when a step is actually performed.

        Unlike the update(double) method, this method will perform more than one step based on the given elapsed time. For example, if the given elapsed time + the remaining time from the last call of this method is 2 * step frequency, then 2 steps will be performed. Use the maximumSteps parameter to put an upper bound on the number of steps performed.

        The stepElapsedTime parameter provides a way for the World to continue to update at the frequency defined in the Settings object, but advance the simulation by the given time.

        Alternatively you can call the updatev(double) method to use a variable time step.

        Parameters:
        elapsedTime - the elapsed time in seconds
        stepElapsedTime - the time, in seconds, that the simulation should be advanced for each step; if less than or equal to zero Settings.getStepFrequency() will be used
        maximumSteps - the maximum number of steps to perform
        Returns:
        boolean true if the World performed at least one simulation step
        Since:
        3.2.4
        See Also:
        update(double), updatev(double), getAccumulatedTime()
      • updatev

        void updatev​(double elapsedTime)
        Updates the World.

        This method will update the world on every call. Unlike the update(double) method, this method uses the given elapsed time and does not attempt to update the world on a set interval.

        This method immediately returns if the given elapsedTime is less than or equal to zero.

        Parameters:
        elapsedTime - the elapsed time in seconds
        See Also:
        update(double), update(double, int)
      • step

        void step​(int steps)
        Performs the given number of simulation steps using the step frequency in Settings.

        This method immediately returns if the given step count is less than or equal to zero.

        Parameters:
        steps - the number of simulation steps to perform
      • step

        void step​(int steps,
                  double elapsedTime)
        Performs the given number of simulation steps using the given elapsed time for each step.

        This method immediately returns if the given elapsedTime or step count is less than or equal to zero.

        Parameters:
        steps - the number of simulation steps to perform
        elapsedTime - the elapsed time for each step
      • containsJoint

        boolean containsJoint​(Joint<T> joint)
        Returns true if this world contains the given joint.
        Parameters:
        joint - the Joint to test for
        Returns:
        boolean true if the joint is contained in this world
        Since:
        3.1.1
      • removeJoint

        boolean removeJoint​(int index)
        Removes the Joint at the given index from this World.

        No other objects are implicitly destroyed with joints are removed.

        Parameters:
        index - the index of the Joint to remove
        Returns:
        boolean true if the Joint was removed
        Since:
        3.2.0
      • removeJoint

        boolean removeJoint​(Joint<T> joint)
        Removes the given Joint from this World.

        No other objects are implicitly destroyed with joints are removed.

        Parameters:
        joint - the Joint to remove
        Returns:
        boolean true if the Joint was removed
      • removeAllBodiesAndJoints

        void removeAllBodiesAndJoints()
        Removes all the joints and bodies from this world.

        This method does not notify of destroyed objects.

        Since:
        3.1.1
        See Also:
        removeAllBodiesAndJoints(boolean)
      • removeAllBodiesAndJoints

        void removeAllBodiesAndJoints​(boolean notify)
        Removes all the joints and bodies from this world.
        Parameters:
        notify - true if destruction of joints and contacts should be notified of by the DestructionListener
        Since:
        3.1.1
      • removeBody

        boolean removeBody​(int index,
                           boolean notify)
        Removes the CollisionBody at the given index from this CollisionWorld.

        When a body is removed, joints and contacts may be implicitly destroyed. Pass true to the notify parameter to be notified of the destruction of these objects via the DestructionListeners.

        Parameters:
        index - the index of the body to remove.
        notify - true if implicit destruction should be notified
        Returns:
        boolean true if the body was removed
        Since:
        3.2.0
      • removeBody

        boolean removeBody​(T body,
                           boolean notify)
        Removes the given CollisionBody from this CollisionWorld.

        When a body is removed, joints and contacts may be implicitly destroyed. Pass true to the notify parameter to be notified of the destruction of these objects via the DestructionListeners.

        Parameters:
        body - the CollisionBody to remove
        notify - true if implicit destruction should be notified
        Returns:
        boolean true if the body was removed
        Since:
        3.1.1
      • removeAllBodies

        void removeAllBodies​(boolean notify)
        This is a convenience method for the removeAllBodiesAndJoints(boolean) method since all joints will be removed when all bodies are removed anyway.
        Parameters:
        notify - true if destruction of joints and contacts should be notified of by the DestructionListener
        Since:
        3.0.1
      • removeAllJoints

        void removeAllJoints()
        Removes all Joints from this World.

        This method does not notify of the joints removed.

        Since:
        3.0.1
        See Also:
        removeAllJoints(boolean)
      • removeAllJoints

        void removeAllJoints​(boolean notify)
        Removes all Joints from this World.
        Parameters:
        notify - true if destruction of joints should be notified of by the DestructionListener
        Since:
        3.0.1
      • getSettings

        Settings getSettings()
        Returns the settings for this world.
        Returns:
        Settings
        Since:
        3.0.3
      • setSettings

        void setSettings​(Settings settings)
        Sets the dynamics settings for this world.
        Parameters:
        settings - the desired settings
        Since:
        3.0.3
      • setGravity

        void setGravity​(Vector2 gravity)
        Sets the acceleration due to gravity.
        Parameters:
        gravity - the gravity in meters/second2
      • setGravity

        void setGravity​(double x,
                        double y)
        Sets the acceleration due to gravity.
        Parameters:
        x - the x component of gravity
        y - the y component of gravity
        Since:
        4.0.0
      • getGravity

        Vector2 getGravity()
        Returns the acceleration due to gravity.
        Returns:
        Vector2 the gravity in meters/second2
      • getContactListeners

        List<ContactListener<T>> getContactListeners()
        Returns an unmodifiable list of all the contact listeners registered to this world.
        Returns:
        List<ContactListener>
      • getStepListeners

        List<StepListener<T>> getStepListeners()
        Returns an unmodifiable list of all the step listeners registered to this world.
        Returns:
        List<StepListener>
      • removeAllContactListeners

        void removeAllContactListeners()
        Removes all contact listeners from this world.
      • removeAllDestructionListeners

        void removeAllDestructionListeners()
        Removes all destruction listeners from this world.
      • removeAllStepListeners

        void removeAllStepListeners()
        Removes all step listeners from this world.
      • removeAllTimeOfImpactListeners

        void removeAllTimeOfImpactListeners()
        Removes all time of impact listeners from this world.
      • removeContactListener

        boolean removeContactListener​(ContactListener<T> listener)
        Removes the given contact listener from this world and returns true if it was removed.

        This method will return false if the listener was not found in this world.

        Parameters:
        listener - the listener
        Returns:
        boolean
      • removeDestructionListener

        boolean removeDestructionListener​(DestructionListener<T> listener)
        Removes the given destruction listener from this world and returns true if it was removed.

        This method will return false if the listener was not found in this world.

        Parameters:
        listener - the listener
        Returns:
        boolean
      • removeStepListener

        boolean removeStepListener​(StepListener<T> listener)
        Removes the given step listener from this world and returns true if it was removed.

        This method will return false if the listener was not found in this world.

        Parameters:
        listener - the listener
        Returns:
        boolean
      • removeTimeOfImpactListener

        boolean removeTimeOfImpactListener​(TimeOfImpactListener<T> listener)
        Removes the given time of impact listener from this world and returns true if it was removed.

        This method will return false if the listener was not found in this world.

        Parameters:
        listener - the listener
        Returns:
        boolean
      • addContactListener

        boolean addContactListener​(ContactListener<T> listener)
        Adds the given ContactListener to this world.

        NOTE: No effort is made to prevent duplicate listeners from being added.

        Parameters:
        listener - the listener to add
        Returns:
        boolean
      • addDestructionListener

        boolean addDestructionListener​(DestructionListener<T> listener)
        Adds the given DestructionListener to this world.

        NOTE: No effort is made to prevent duplicate listeners from being added.

        Parameters:
        listener - the listener to add
        Returns:
        boolean
      • addStepListener

        boolean addStepListener​(StepListener<T> listener)
        Adds the given StepListener to this world.

        NOTE: No effort is made to prevent duplicate listeners from being added.

        Parameters:
        listener - the listener to add
        Returns:
        boolean
      • addTimeOfImpactListener

        boolean addTimeOfImpactListener​(TimeOfImpactListener<T> listener)
        Adds the given TimeOfImpactListener to this world.

        NOTE: No effort is made to prevent duplicate listeners from being added.

        Parameters:
        listener - the listener to add
        Returns:
        boolean
      • setValueMixer

        void setValueMixer​(ValueMixer valueMixer)
        Sets the ValueMixer.

        A ValueMixer is an implementation of mixing functions for various values used in contact solving. Common mixed values are restitution and friction. Since each BodyFixture can have it's own value for these metrics, the ValueMixer is used to mathematically combine them into one value to be used in contact resolution.

        ValueMixer.DEFAULT_MIXER is the default.

        Parameters:
        valueMixer - the value mixer
        Throws:
        NullPointerException - if valueMixer is null
        Since:
        4.2.0
        See Also:
        ValueMixer
      • setContinuousCollisionDetectionBroadphaseDetector

        void setContinuousCollisionDetectionBroadphaseDetector​(BroadphaseDetector<T> broadphaseDetector)
        Sets the CCD broad-phase collision detection algorithm.
        Parameters:
        broadphaseDetector - the broad-phase detection algorithm
        Throws:
        NullPointerException - if the given detector is null
        Since:
        4.1.0
      • getContinuousCollisionDetectionBroadphaseDetector

        BroadphaseDetector<T> getContinuousCollisionDetectionBroadphaseDetector()
        Returns the CCD broad-phase collision detection algorithm.
        Returns:
        BroadphaseDetector<T>
        Since:
        4.1.0
      • getJointCount

        int getJointCount()
        Returns the number of Joints in this World.
        Returns:
        int the number of joints
      • getJoint

        Joint<T> getJoint​(int index)
        Returns the Joint at the given index.
        Parameters:
        index - the index
        Returns:
        Joint
      • getJoints

        List<Joint<T>> getJoints()
        Returns an unmodifiable list containing all the joints in this world.

        The returned list is backed by the internal list, therefore adding or removing joints while iterating through the returned list is not permitted. Use the getJointIterator() method instead.

        Returns:
        List<Joint>
        Since:
        3.1.5
        See Also:
        getJointIterator()
      • getJointIterator

        Iterator<Joint<T>> getJointIterator()
        Returns an iterator for iterating over the joints in this world.

        The returned iterator supports the remove method.

        Returns:
        Iterator<Joint>
        Since:
        3.2.0
      • getTimeStep

        TimeStep getTimeStep()
        Returns the TimeStep object used to advance the simulation.

        The returned object contains the step information (elapsed time) for the last and the previous time step.

        Returns:
        TimeStep the current step object
        Since:
        4.0.0
      • getAccumulatedTime

        double getAccumulatedTime()
        Returns the current accumulated time.

        This is the time that has elapsed since the last step of the engine.

        This time is used and/or accumulated on each call of the update(double) and update(double, int) methods.

        This time is reduced by the step frequency for each step of the engine.

        Returns:
        double
        Since:
        3.1.10
      • setAccumulatedTime

        void setAccumulatedTime​(double elapsedTime)
        Sets the current accumulated time.

        A typical use case would be to throw away any remaining time that the update(double) or update(double, int) methods didn't use:

         boolean updated = world.update(elapsedTime);
         // the check if the world actually updated is crutial in this example
         if (updated) {
                // throw away any remaining time we didnt use
                world.setAccumulatedTime(0);
         }
         
        Or, in the case of reusing the same World object, you could use this method to clear any accumulated time.

        If elapsedTime is less than zero, this method immediately returns.

        Parameters:
        elapsedTime - the desired elapsed time
        Since:
        3.1.10
        See Also:
        getAccumulatedTime()
      • isUpdateRequired

        boolean isUpdateRequired()
        Returns true if upon the next time step the contacts must be updated.
        Returns:
        boolean
        See Also:
        setUpdateRequired(boolean)
      • setUpdateRequired

        void setUpdateRequired​(boolean flag)
        Sets the update required flag.

        This flag indicates that changes have been made to the bodies, fixtures, joints, etc. of this PhysicsWorld that require another collision detection cycle. The following conditions outline the common reasons a user would set this flag to true:

        • If a Body has been added or removed from the World
        • If a Body has been translated or rotated
        • If a Body's state has been manually changed via the Body.setActive(boolean) method
        • If a BodyFixture has been added or removed from a Body
        • If a BodyFixture's sensor flag has been manually changed via the BodyFixture.setSensor(boolean) method
        • If a BodyFixture's filter has been manually changed via the BodyFixture.setFilter(boolean) method
        • If a BodyFixture's restitution or friction coefficient has changed
        • If a BodyFixture's Shape has been translated or rotated
        • If a BodyFixture's Shape has been changed (vertices, radius, etc.)
        • If a Body's type has changed to or from Static (this is caused by the using setMassType(Mass.INFINITE/Mass.NORMAL) method)
        • If a Joint has been added or removed from the World in which the joined bodies should not be allowed to collide
        • If the World's CoefficientMixer has been changed
        In most cases this flag should not be set. Running another collision detection cycle is a time consuming process and any changes will be reflected in the next run. The intended use-case for this flag are situations where a significant portion of the objects in the world have been modified or the user cannot wait a cycle before their changes are reflected.
        Parameters:
        flag - the flag
      • isInContact

        boolean isInContact​(T body1,
                            T body2)
        Returns true if the given PhysicsBodys are currently in collision.

        Collision is defined as the two bodies interacting to the level of ContactConstraint generation and solving.

        Parameters:
        body1 - the first body
        body2 - the second body
        Returns:
        boolean
      • getInContactBodies

        List<T> getInContactBodies​(T body,
                                   boolean includeSensedContact)
        Returns a list of all PhysicsBodys that are in contact with the given PhysicsBody.
        Parameters:
        body - the body
        includeSensedContact - true if sensed contacts should be included in the results
        Returns:
        List<PhysicsBody>
      • isJoined

        boolean isJoined​(T body1,
                         T body2)
        Returns true if the two PhysicsBodys are joined via a Joint.
        Parameters:
        body1 - the first body
        body2 - the second body
        Returns:
        boolean
      • isJointCollisionAllowed

        boolean isJointCollisionAllowed​(T body1,
                                        T body2)
        Returns true if the two PhysicsBodys are joined by at least one Joint where the collision allowed property is true.
        Parameters:
        body1 - the first body
        body2 - the second body
        Returns:
        boolean