- Object
-
- ConstraintGraph<T>
-
- Type Parameters:
T
- thePhysicsBody
type
public final class ConstraintGraph<T extends PhysicsBody> extends Object
Represents an undirected graph of constraints involvingPhysicsBody
s 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
Island
s separated by staticPhysicsBody
s.- Since:
- 4.0.0
- Version:
- 4.0.0
- Author:
- William Bittle
-
-
Constructor Summary
Constructors Constructor Description ConstraintGraph()
Minimal constructor.ConstraintGraph(int initialBodyCount, int initialJointCount)
Full constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addBody(T body)
Adds an interaction graph node for the given body.void
addContactConstraint(ContactConstraint<T> contactConstraint)
Adds an interaction graph edge for the givenContactConstraint
.void
addJoint(Joint<T> joint)
Adds an interaction graph edge for the givenJoint
.void
clear()
Clears the graph of all nodes and edges.boolean
containsBody(T body)
Returns true if the given body is part of the interaction graph.boolean
containsContactConstraint(ContactConstraint<T> contactConstraint)
Returns true if the given contact constraint exists in this interaction graph.boolean
containsJoint(Joint<T> joint)
Returns true if the given joint exists in this interaction graph.List<ContactConstraint<T>>
getContacts(T body)
Returns theContactConstraint
s for the givenPhysicsBody
.List<T>
getInContactBodies(T body, boolean includeSensedContact)
Returns a list of allPhysicsBody
s that are in contact with the givenPhysicsBody
.List<T>
getJoinedBodies(T body)
List<Joint<T>>
getJoints(T body)
Returns theJoint
s the givenPhysicsBody
is a member of.ConstraintGraphNode<T>
getNode(T body)
Returns the node for the given body on the graph.boolean
isInContact(T body1, T body2)
Returns true if the givenPhysicsBody
s are currently in collision.boolean
isJoined(T body1, T body2)
Returns true if the twoPhysicsBody
s are joined via aJoint
.boolean
isJointCollisionAllowed(T body1, T body2)
Returns true if the twoPhysicsBody
s are joined by at least oneJoint
where the collision allowed property is true.void
removeAllContactConstraints()
Removes all edges in the graph related to contact constraints.void
removeAllJoints()
Removes all edges in the graph related to joints.ConstraintGraphNode<T>
removeBody(T body)
Removes the given body from the graph.void
removeContactConstraint(ContactConstraint<T> contactConstraint)
Removes the given contact constraint from the graph.void
removeJoint(Joint<T> joint)
Removes the given joint from the graph.int
size()
Returns the number of nodes in the graph.void
solve(ContactConstraintSolver<T> solver, Vector2 gravity, TimeStep step, Settings settings)
Solves the interation graph constraints (Joints/Contacts) by splitting the graph intoIsland
s.
-
-
-
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 givenContactConstraint
.- Parameters:
contactConstraint
- the contact constraint
-
addJoint
public void addJoint(Joint<T> joint)
Adds an interaction graph edge for the givenJoint
.- 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 givenPhysicsBody
s are currently in collision.Collision is defined as the two bodies interacting to the level of
ContactConstraint
generation and solving.- Parameters:
body1
- the first bodybody2
- the second body- Returns:
- boolean
-
getContacts
public List<ContactConstraint<T>> getContacts(T body)
Returns theContactConstraint
s for the givenPhysicsBody
.These represent the contact pairs between the given body and the others it's colliding with. Each
ContactConstraint
could have 1 or 2 contacts associated with it.- Parameters:
body
- the body- Returns:
- List<
ContactConstraint
>
-
getInContactBodies
public List<T> getInContactBodies(T body, boolean includeSensedContact)
Returns a list of allPhysicsBody
s that are in contact with the givenPhysicsBody
.- Parameters:
body
- the bodyincludeSensedContact
- 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 twoPhysicsBody
s are joined by at least oneJoint
where the collision allowed property is true.- Parameters:
body1
- the first bodybody2
- the second body- Returns:
- boolean
-
isJoined
public boolean isJoined(T body1, T body2)
Returns true if the twoPhysicsBody
s are joined via aJoint
.- Parameters:
body1
- the first bodybody2
- the second body- Returns:
- boolean
-
getJoints
public List<Joint<T>> getJoints(T body)
Returns theJoint
s the givenPhysicsBody
is a member of.- Parameters:
body
- the body- Returns:
- List<
Joint
>
-
getJoinedBodies
public List<T> getJoinedBodies(T body)
- Parameters:
body
- the body- Returns:
- List<
PhysicsBody
>
-
solve
public void solve(ContactConstraintSolver<T> solver, Vector2 gravity, TimeStep step, Settings settings)
Solves the interation graph constraints (Joints/Contacts) by splitting the graph intoIsland
s. EachIsland
represents a segment of the constraint graph that can be solved in isolation.- Parameters:
solver
- the contact constraint solvergravity
- the world gravitystep
- the time step informationsettings
- the settings
-
-