WhatsDonIsDon Posted September 13, 2014 Share Posted September 13, 2014 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 More sharing options...
Arcanorum Posted September 14, 2014 Share Posted September 14, 2014 Look at some of the different examples here http://gamemechanicexplorer.com/#homingmissiles-1 Also please don't use SO like a Q&A site. Link to comment Share on other sites More sharing options...
Dumtard Posted September 14, 2014 Share Posted September 14, 2014 Why would you not use it as a Q&A site? Taken from the top of Stack Overflow.Stack Overflow is a question and answer site for professional and enthusiast programmers. Link to comment Share on other sites More sharing options...
xerver Posted September 14, 2014 Share Posted September 14, 2014 Also please don't use SO like a Q&A site. lol wut? Link to comment Share on other sites More sharing options...
pixelpathos Posted September 14, 2014 Share Posted September 14, 2014 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! Nepoxx 1 Link to comment Share on other sites More sharing options...
Arcanorum Posted September 15, 2014 Share Posted September 15, 2014 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. Nepoxx 1 Link to comment Share on other sites More sharing options...
Recommended Posts