## Planet Gravity for Space games

• Topic Archived
1. Boards
2. Game Design and Programming
3. Planet Gravity for Space games

#### User Info: chrisjrogers

chrisjrogers
5 years ago#1
I'm trying to setup some gravity in my space game for a blackhole but the issue is I have no clue how to setup the equation for gravity like this.

void gravity(double Posx, double Posy, double massA, double massB, double GForce){}

Not sure what else I'm missing or how to set this up.
I've got most of the parts but no clue how to get this setup.

I've been looking for tutorials on the subject but it's like looking for a needle in a hay stack. Found a lot of simulations though so I must be in the right area.
Reality can throw you some curve balls that statistics can't predict. Best be prepared for them. :)

#### User Info: OrangeWizard

OrangeWizard
5 years ago#2
This looks like it's in 2D.

Just off the top of my head, it should go like this:
The farther away you are from the planet, the smaller the force should be exerted on you, and vice versa.

So you'd need to get the distance between the object and the planet, and exert a force towards the planet.

Probably something like:

`dist = (Math.sqr)((Player.x - Planet.x)^2 +(Player.y - Planet.y)^2);`

Then use that to calculate the force
`force = 9.8(Planet.mass/Player.mass / dist)^2 `

or something.

Then have it move towards the center of the planet by that amount.
I don't currently know a more sophisticated way of doing this, so I'll just use if-statements.

`if(Player.x > Planet.X){Player.velX -= force;}`
And so on and so forth for each possibility.

I'm assuming that you have a velocity on your player, though.
Sai La Vie - EricDent1 trying to type "C'est la vie"

#### User Info: PTP2009

PTP2009
5 years ago#3
The force of gravity = G*m1*m2 / r^2. One of those masses is the planet which I assume you're keeping constant, and the other is the object that you're applying the gravity to, so its mass cancels when you set that equal to m*a, so your acceleration is G*M / r^2 in the direction of the planet. ("G" is a constant with a specific value in the real world, but would be whatever makes the game work right in your system.)

From there it's just fiddling with whatever vector system you have and ensuring that signs are right.

#### User Info: OrangeWizard

OrangeWizard
5 years ago#4
I edited my post to make it more correct.

I keep getting errors when trying to edit my post for the umpteenth time.
But where it says

`Planet.mass/Player.mass`?

Change that to `Planet.mass * Player.mass`
Sai La Vie - EricDent1 trying to type "C'est la vie"

neoX
5 years ago#5
import gravity
gravity.start()

#### User Info: chrisjrogers

chrisjrogers
5 years ago#6
OK was completely stumped on this since I saw a lot of use of it but no one seems to think of making a tutorial around this type of thing for some reason. :) Guess not many want to try and wright a Physics tutorial. :)
Reality can throw you some curve balls that statistics can't predict. Best be prepared for them. :)

#### User Info: chrisjrogers

chrisjrogers
5 years ago#7
Got to testing and I'm stumped. What did I mess up exactly?
http://pastebin.com/Zc6N93U0

Also been getting around to fixing some issues in my game, noticed that I wasn't using the baseGameObject system as much as I should have been and I'm getting around to fixing that issue. :)

In any event it seems there were a few errors either on my part or something I'm not sure. I'll see about fixing it but right now the gravity repels and doesn't work like it should.
Reality can throw you some curve balls that statistics can't predict. Best be prepared for them. :)

#### User Info: PTP2009

PTP2009
5 years ago#8
First, that's the wrong formula. The product of the masses is not squared, just the radius.

Second, that's not the right way to apply the force. `Force = mass * acceleration`, so solving for acceleration you get `acceleration = force / mass`. The mass of the object subject to gravity cancels.

Third, that's not the way to work with the vector. You need to do some trigonometry to find the component of the vector that's in the x and y directions, and add those.

#### User Info: chrisjrogers

chrisjrogers
5 years ago#9
PTP2009 posted...
First, that's the wrong formula. The product of the masses is not squared, just the radius.

Second, that's not the right way to apply the force. `Force = mass * acceleration`, so solving for acceleration you get `acceleration = force / mass`. The mass of the object subject to gravity cancels.

Third, that's not the way to work with the vector. You need to do some trigonometry to find the component of the vector that's in the x and y directions, and add those.

Been working on that right now and got it mostly working just need to add the other directions. Thought something was off about that formula but wasn't sure. Yes my Physics math and Vector math need work I know that. Trying to setup my game's code so when I use the code I learn something rather than shoot myself in the head. :)
Reality can throw you some curve balls that statistics can't predict. Best be prepared for them. :)

#### User Info: chrisjrogers

chrisjrogers
5 years ago#10
http://pastebin.com/nrjmgUit
Works now but not sure if I have to fix anything just yet. ^_^
Reality can throw you some curve balls that statistics can't predict. Best be prepared for them. :)
1. Boards
2. Game Design and Programming
3. Planet Gravity for Space games