Implementing Circular Gravity


Hi All

So i released this video online and received a question from youtuber anic99a asking how i achieved this cool effect. Let me begin to elaborate the demo was produced using game maker studio for rapid prototyping i’m still deciding if i should finish it up and release it as some kind of game.

The basics of the algorithm are not complected at all while the effect is very intriguing i have a player object and a planet object each planet has a mass value which determines the size and gravity pull.

In the players Step Event (Per Frame)
we loop through all the planets and apply force based on the players distance and angle to that planet


Firstly we get the distance between the player and the planet, then we get the direction of the player relative to the planet in question.

If the distance is less than 250 (arbitrary number) then we set dx and dy (direction x, direction y) 150 pixels away from the player in the direction of the planet.

Finally we apply physics to the player in that direction. I’m cheating using Box2D to handle collisions between the player and the planet but you can implement this into any engine.

The code is a bit messy and not optimized at all but for a quick 15 minute demo it works well. You can optimize this by making a generic script to be applied to any objects to handle planet gravity.


Liked it? Take a second to support rm2kdev on Patreon!