Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- tick:
- Step one: loop through every spring and add the force of the spring.
- Step two: loop through every mass and compute the new position.
- Step three: loop through all springs and, given it's new end point, compute the new forces on the spring.
- */
- //Code for Spring. (p and q are the current positions for the mass on the endpoints of the spring.). (p1 and q1 are the positions for last tick)
- public float[] go(float p, float q) {
- float distance = p - q;
- float distanceDelta = (p - p1) - (q - q1);
- float a = springiness * (Math.abs(distance) - length);
- float b;
- float c;
- if (distance == 0) {
- c = 1;
- b = (damping * distanceDelta * distance) / .0000001f;
- } else {
- b = (damping * distanceDelta * distance) / Math.abs(distance);
- c = distance / Math.abs(distance);
- }
- float newForce = -(a + b) * c;
- p1 = p;
- q1 = q;
- return new float[] { newForce, -newForce };
- }
- //Code for Mass
- public float go() {
- if (fixed) {
- return defaultPos;
- }
- float newForce = runningForce * mass - n2 + 2f * n1;
- n2 = n1;
- n1 = newForce;
- runningForce = 0;
- return newForce;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement