# [Solved] Physics Math question

Get help using Construct 2

### » Sat Feb 02, 2013 12:31 pm

Hello.

I'm building my first (space)game and its coming along great. I just added some physics to it which was surprisingly easy to do after a bit of reading. My planets now have gravity!

Atm i'm comparing 2 value's to see if the ship is in range for the force to take affect. Its working as intended however i would like the force to increase in strength as the ship gets closer to the planet.

I figured this could be done by dividing the force with the distance between the ship and the planet. That way the force would be lower if you are further away from the planet. The only thing is i have no idea how to set this up.

Here is a link to my test built
https://dl.dropbox.com/u/139739932/test/grafity%20test.capx

Semo2013-02-04 07:20:03
B
12
S
2
G
3
Posts: 59
Reputation: 2,628

### » Sat Feb 02, 2013 4:11 pm

Here is one way..

http://dl.dropbox.com/u/84974566/ship-planet.capx

there are lots of clever folks on here and maths is not my strong point ha, but it works.
B
52
S
16
G
9
Posts: 1,098
Reputation: 11,267

### » Sat Feb 02, 2013 4:27 pm

You are right. =)
You can get the distance between the two with distance(Player.X,Player.Y,Sun.X,Sun.Y)

If you want to make it more realistic and make the effect of distance stronger you can square that value.

You could also give the planet/star a mass variable and use that instead of directly specifying a force. Might make things easier if you have many planets/star and use families later.

http://en.wikipedia.org/wiki/Newton%27s_law_of_universal_gravitation

EDIT: Looks like spongehammer beat me to it! =DBenGIJOE2013-02-02 16:31:54
B
5
S
2
G
1
Posts: 12
Reputation: 1,049

### » Sat Feb 02, 2013 5:11 pm

gravity testwretchedshark2013-02-02 17:12:01
B
14
S
6
G
2
Posts: 136
Reputation: 3,210

### » Sun Feb 03, 2013 4:14 pm

Hey guys thanks for all the replies!

I'm not using the beta atm so i could not open your examples. Downloaded it today. Anyway I had a friend over last night and we figured it out.
Credit goes to my friend but here is what he came up with.

Apply physics force: (1-(distance(player.X,player.Y,sun.X,sun.Y)/512))/2

512 is just half of the layout size

Here's the example
https://dl.dropbox.com/u/139739932/test/gravity%20test%202.capxSemo2013-02-03 16:18:14
B
12
S
2
G
3
Posts: 59
Reputation: 2,628