I'm pulling my hair out with this problem. I have two bodies and I'm trying to calculate gravity between them. This is for a game, and there are actually three bodies currently, each frame, I check the distance each one moves due to gravity and store it in a cache (to simulate gravity acting on all objects simultaneously), then apply each vector to the body. This is the code I have:(adsbygoogle = window.adsbygoogle || []).push({});

The angles are off though, and I'm convinced it has something to do with the coordinate system Java uses for drawing but I can't get it to work properly. Is there anything obvious I've been overlooking?Code (Text):public void calculateGravity(GravitationalBody planet) {

if(isFixed || gravityIndex.contains(planet.hashCode()) || planet.getGravityIndex().contains(this.hashCode())) {

return;

}

double radiusGravity = this.getCenter().distance(planet.getCenter());

double force = (Constants.G * this.mass * planet.mass)/(radiusGravity*radiusGravity); // F=GMm/r^2

double frameRateMs = (Constants.FrameRate/1000.0);

double gravityAngle = Math.atan2(Math.max(location.y,planet.location.y) - Math.min(location.y,planet.location.y),

Math.max(location.x,planet.location.x) - Math.min(location.x,planet.location.x)) + (Math.PI/2);

double gravityDistance = ((force*frameRateMs)/this.mass)*Constants.SpriteGravityMultiplier;

if(gravityAngle < 0){ // keep angle positive

gravityAngle += MaxAngle;

}

// System.out.println("--"+name+"<->"+planet.getName()+"-"+gravityDistance+"px @ "+Math.round(gravityAngle)+"rad");

if(planet.IsFixed()) { // gravity acts only on this body, pulling it towards the planet

gravityCache.add(new Vector2D(gravityAngle, gravityDistance));

gravityIndex.add(planet.hashCode());

} else { // gravity acts on both, pulling them toward each other

double resultantGravAngle = (gravityAngle/2),

resultantGravDistance = (gravityDistance /2);

gravityCache.add(new Vector2D(resultantGravAngle, resultantGravDistance));

gravityIndex.add(planet.hashCode());

planet.getGravityCache().add(new Vector2D(resultantGravAngle, resultantGravDistance));

planet.getGravityIndex().add(planet.hashCode());

}

}

Assuming Constants.Framerate = 30, Constants.G = 6.667, Constants.SpriteGravityMultiplier = 1e1

**Physics Forums - The Fusion of Science and Community**

Dismiss Notice

Join Physics Forums Today!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# Gravity with Java

Loading...

Similar Threads for Gravity Java | Date |
---|---|

Python (VPython) Can someone help me with my orbit simulator? | Monday at 12:43 AM |

Java Java 10 is out! | Mar 23, 2018 |

JavaScript Gravity Simulator | Jun 7, 2016 |

Python Help designing an n-body gravity simulator in Python | Nov 29, 2015 |

Can you please help me understand drag and gravity? | May 21, 2015 |

**Physics Forums - The Fusion of Science and Community**