Sign in to follow this  

Have one "body" attract all other "bodies". Basically have the earth with gravity.

Recommended Posts

I've added a body that should have gravity to my game, so picture a big empty screen with a circle for "Earth" in the middle.

Now when other bodies are added to the field, like an asteroid, I want them to be "attracted" to Earth, but not just immediately travel in a straight line to it. 

What methods would let me have any other body added to the game be "accelerated" or "attracted" to Earth?

X-Post from StackOverflow, since this is a Phaser.js specific question.

Share this post

Link to post
Share on other sites

Hi WhatsDonIsDon,


Without knowing the specifics of your game and Phaser, it sounds like the implementation of gravity is correct (a force applied to each body/asteroid in the direction of Earth, perhaps inversely proportional to the square of the distance between asteroid and Earth).


Perhaps all you need to do is to ensure that the asteroid has some velocity perpendicular to Earth. This should allow the asteroid to orbit in some fashion (I assume this is what you are aiming for). For example, if your Earth is in the centre of the screen, and you have an asteroid directly above at the top of the screen: if the asteroid has some initial sideways motion, you should get some kind of orbit.


I've implemented gravity in my game, Luminarium, which you might find a useful example (see this thread, or direct link in my signature). Have a look at scene 3 (locked levels are playable in the demo), which introduces an "orbit" command, analogous to your Earth.


When I first implemented the orbit, I found that the Lumins (aliens) would be "sling-shotted" by the orbit command i.e. they would be pulled closer towards it, and accelerate, but then would escape out the other side of the orbit. To try an guarantee continuous orbit, I implemented slight damping (reduction of the aliens' velocity over time). Also, to reduce the number of calculations required, especially if you're going to create many asteroids, I limited the radius over which the orbit/gravity operates, as indicated by the circle drawn around my orbit.


Let me know if I can provide any more details!



Share this post

Link to post
Share on other sites

What I mean by don't use SO like a Q&A site is that people shouldn't just dump their own specific, situational problems there expecting someone to come along and work it out for them. The Q&A aspect of SO is meant to serve as a way to grow a repository of useful knowledge, not just to be someones own personal helpdesk.

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.