This site requires JavaScript, please enable it in your browser!
Greenfoot back

Report as inappropriate.

Krendil presents ...



A framework that simulates gravity in space. Everything with gravity should be a subclass of Matter.
To make something affected by gravity, add gravitate() in its act method.
In this example, the sun has a mass of 300 and the planets of 0.5.

Update 20/10/08: Adjusted the orbits of planets, caused the sun to be affected by gravity, and fixed a bug in the getBearing function

8195 views / 989 in the last 7 days

6 votes | 0 in the last 7 days

Tags: space simulation physics with-source orbit

This scenario is a member of: Space Simulations

open in greenfoot
Your browser does not support the canvas tag.
Finn KerrFinn Kerr


HAHA This is SWEET! I love it ^^ It just gets a bit buggy when the planets get too close to each other and around the edges


Wow, very nice, nice concept! But yeah, it does act a little funny when you let it run for a while (I would hate to live in that solar system XD, because the planets go near the sun, and then springs out of orbit after a while), but other than that, nice simulation of gravity.


The problem with the funny display around the edges is a Greenfoot restriction: You cannot place actors outside of the world area. (Maybe we should allow that?) And if you remove them from the world while they are out of view, they wouldn't get their act method called. There are two possible solutions to this: either you (temporarily) remove them from the world and enter them in an 'off-screen-actor' list of your own, where you call the objects' act method yourself (from the World act method); or you leave them in the world, but give them (temporarily) an empty image.


This particular scenario is just an example, and wasn't meant to have particularly stable orbits. In the game I'm making with it, I made everything bounce off the edges of the screen. If I were to allow things to go off the edge, I would probably use your second solution.
A new version of this scenario was uploaded on Mon Oct 20 09:36:53 UTC 2008


I thought of doing something similar as a learning exercise for me trying to figure out how to get objects to read varaibles from each other this will help alot - Thanks for shairing it


Darn! I was just making a simulation just like this, and wanted to see if someone had already made one before I posted it, so I searched Greenfoot for gravity and got this. Really cool, although why did you use two different classes for matter, Heavy and Light? All matter uses the same gravity, so there shouldn't be a need to make different classes. Still, nice job!


Sorry! On second look I saw that there's no difference between Heavy and Light except for their mass variables and images. I just did that inside of a constructor, along with the colorizing. Also, something I found interesting. You have almost the exact same code for applying gravity on all objects as I did. List bodies = getWorld().getObjects(Matter.class); for (Object body : bodies){ if (body != this){ //Do Gravity Stuff } } Once again, I think this is pretty cool!

Want to leave a comment? You must first log in.

Who likes this?

00swinter data17 qnanqing kenshinakh Finn Kerr