-
Notifications
You must be signed in to change notification settings - Fork 2
Example: Nodes, Edges and Physics application
This framework uses a system of nodes and edges as its primary method of applying physics. All forces are eventually applied to nodes, but edges can be used as a layer of abstraction.
Both edges and nodes implement IRigidbody. As such you can apply forces by using the addForceVector(Vector vector) or addForceVector(String name, Vector vector) method. The simplest, cleanest method of implementing the "name" parameter in larger projects is by using the simple name of the force class like "MyForce.getClass().getSimpleName(). This name may be used in managing save data via the save system.
Nodes are broken into two subclasses: Node2D for 2D simulations and Node3D for 3D simulations. A resultant force is calculated for each node, which is used to move the objects during simulation.
To add a force to a node:
Vector myForceVector = new Vector2f(1,2); // Create a 2D vector where x = 1, y = 2
Node node = new Node2D(0,0); // Create a 2D Node with a position x = 0, y = 0
node.addForceVector("my_force", myForceVector) // add force vector (name optional)
Note we need to explicitly declare whether our node is 2D or 3D at some point before force calculation.
Edges do not require unique subclasses for 2D and 3D states, instead using references to either 2D or 3D (not yet implemented) nodes. This means interfacing with functions for edges in 2D and 3D systems is identical. Primary functions that may be used with edges are getLength() and getNormal(), which return the current length and normal of the edges, respectively. getRestingLength() returns the initial length of the edge by default; this value may be overridden in the case of active springs.
Since edges also inherit from the IRigidbody interface, they can also have forces added to them using the same addForceVector(Vector vec) command. This moves both nodes in the same direction. To move nodes in the opposite direction, use the addConstrictionForceVector(Vector vec) command. The input arguments remain identical, but the nodes are moved in the opposite direction.
Node2D a, b; // Create two Node2D nodes
Vector myForceVector = new Vector2f(1,2); // Create a 2D vector where x = 1, y = 2
Edge edge = new Edge(a,b); // Create a new Edge
edge.addForceVector("my_force", myForceVector) // add force vector (name optional), moving nodes
// in the same direction.
edge.addConstrictionForceVector("my_force", myForceVector) // add force vector (name optional),
// moving nodes in the opposite direction.