Module org.dyn4j

Class ConstraintGraph<T extends PhysicsBody>

  • Type Parameters:
    T - the PhysicsBody type

    public final class ConstraintGraph<T extends PhysicsBody>
    extends Object
    Represents an undirected graph of constraints involving PhysicsBodys with the desire to split the simulation into smaller, solvable chunks.

    The graph is maintained by using the various add/remove methods. The PhysicsWorld is a consumer of this object, adding nodes and edges when bodies/joints are added. During the collision detection process, contact edges are cleared and recreated.

    Solving of the graph happens internally by performing depth-first traversal and the building of Islands separated by static PhysicsBodys.

    Since:
    4.0.0
    Version:
    4.0.0
    Author:
    William Bittle
    • Constructor Detail

      • ConstraintGraph

        public ConstraintGraph()
        Minimal constructor.
      • ConstraintGraph

        public ConstraintGraph​(int initialBodyCount,
                               int initialJointCount)
        Full constructor.
        Parameters:
        initialBodyCount - the initial body count
        initialJointCount - the initial joint count
    • Method Detail

      • addBody

        public void addBody​(T body)
        Adds an interaction graph node for the given body.
        Parameters:
        body - the body
      • addContactConstraint

        public void addContactConstraint​(ContactConstraint<T> contactConstraint)
        Adds an interaction graph edge for the given ContactConstraint.
        Parameters:
        contactConstraint - the contact constraint
      • addJoint

        public void addJoint​(Joint<T> joint)
        Adds an interaction graph edge for the given Joint.
        Parameters:
        joint - the joint
      • containsBody

        public boolean containsBody​(T body)
        Returns true if the given body is part of the interaction graph.
        Parameters:
        body - the body
        Returns:
        boolean
      • removeBody

        public ConstraintGraphNode<T> removeBody​(T body)
        Removes the given body from the graph.

        A body represents a node in the graph, therefore removing the node will remove the edges between the given node and other nodes as well.

        Parameters:
        body - the body to remove
        Returns:
        ConstraintGraphNode<T>
      • containsJoint

        public boolean containsJoint​(Joint<T> joint)
        Returns true if the given joint exists in this interaction graph.
        Parameters:
        joint - the joint
        Returns:
        boolean
      • containsContactConstraint

        public boolean containsContactConstraint​(ContactConstraint<T> contactConstraint)
        Returns true if the given contact constraint exists in this interaction graph.
        Parameters:
        contactConstraint - the contact constraint
        Returns:
        boolean
      • removeJoint

        public void removeJoint​(Joint<T> joint)
        Removes the given joint from the graph.

        A joint is an edge connecting body nodes. This method removes the edges related to the given joint.

        Parameters:
        joint - the joint to remove
      • removeContactConstraint

        public void removeContactConstraint​(ContactConstraint<T> contactConstraint)
        Removes the given contact constraint from the graph.

        A contact constraint is an edge connecting body nodes. This method removes the edges related to the given contact constraint.

        Parameters:
        contactConstraint - the contact constraint to remove
      • getNode

        public ConstraintGraphNode<T> getNode​(T body)
        Returns the node for the given body on the graph.

        Returns null if the body is not on the graph.

        Parameters:
        body - the body to remove
        Returns:
        ConstraintGraphNode<T>
      • removeAllContactConstraints

        public void removeAllContactConstraints()
        Removes all edges in the graph related to contact constraints.
      • removeAllJoints

        public void removeAllJoints()
        Removes all edges in the graph related to joints.
      • clear

        public void clear()
        Clears the graph of all nodes and edges.
      • size

        public int size()
        Returns the number of nodes in the graph.
        Returns:
        int
      • isInContact

        public 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

        public 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>
      • isJointCollisionAllowed

        public 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
      • isJoined

        public 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
      • getJoints

        public List<Joint<T>> getJoints​(T body)
        Returns the Joints the given PhysicsBody is a member of.
        Parameters:
        body - the body
        Returns:
        List<Joint>
      • solve

        public void solve​(ContactConstraintSolver<T> solver,
                          Vector2 gravity,
                          TimeStep step,
                          Settings settings)
        Solves the interation graph constraints (Joints/Contacts) by splitting the graph into Islands. Each Island represents a segment of the constraint graph that can be solved in isolation.
        Parameters:
        solver - the contact constraint solver
        gravity - the world gravity
        step - the time step information
        settings - the settings