Jump to content

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


WhatsDonIsDon
 Share

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.
http://stackoverflow.com/questions/25827696/phaser-gravity-originating-from-a-body

Link to comment
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!

 

post-9827-0-45118500-1410724281.jpg

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...