Last active
August 29, 2015 14:21
-
-
Save Jacoby6000/16db08802925a1dee060 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ArrayList<Body> bodies = new ArrayList<Body>; | |
Float gravitationalConstant = 0.0001f; | |
public void gravitate() { | |
int length = bodies.length; | |
for (int i=0; i < length; i++){ | |
Body bodyA = bodies.get(i); | |
for (int j=i+1; j < length; j++) { | |
Body bodyB = bodies.get(j); | |
Vector2 posA = bodyA.getPosition.cpy(); | |
Vector2 posB = bodyB.getPosition.cpy(); | |
Vector2 dirFromBToA = posA.cpy().sub(posB).nor(); | |
float distSqr = posA.cpy().dst2(posB); | |
float massCoefficient = bodyA.getMass * bodyB.getMass; | |
val force = massCoefficient / distSqr * gravitationalConstant; | |
bodyA.applyForceToCenter(dirFromBToA.cpy().scl(-force), true); | |
bodyB.applyForceToCenter(dirFromBToA.cpy().scl(force), true); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val bodies: ListBuffer[Body] | |
val gravitationalConstant = 0.0001f | |
def gravitate(): Unit = { | |
val length = bodies.length | |
for ( | |
i <- 0 until length; | |
bodyA = bodies(i); | |
j <- (i + 1) until length; | |
bodyB = bodies(j) | |
) yield { | |
val posA = bodyA.getPosition.cpy() | |
val posB = bodyB.getPosition.cpy() | |
val distSqr = posA.cpy().dst2(posB) | |
val dir = posA.cpy().sub(posB).nor() | |
val massCoefficient = bodyA.getMass * bodyB.getMass | |
val force = massCoefficient / distSqr * gravitationalConstant | |
bodyA.applyForceToCenter(dir.cpy().scl(-force), true) | |
bodyB.applyForceToCenter(dir.cpy().scl(force), true) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment