Mathy type gravity angle thingy hahah topic :)

Monkey Forums/Monkey Programming/Mathy type gravity angle thingy hahah topic :)

Paul - Taiphoz(Posted 2015) [#1]
Not looked at any code for a while and some of my own code looks a little alien to me so please bare with me while I warm my brain up.

Ok so I have asteroids when travel along a given vector at a random but set speed, when the object is free moving it will continue on its course until hit by something or it exits the screen and I wrap it around and add a little randomness to its new vector.

I have gravity in my game and the way I have opted to deal with it is that each time an object feels the pull of gravity I alter its current vector to turn it a little more toward the gravity well or planet which works nice, I do this gravity pull only once every 100 ms or there abouts.

What I am now trying to do is when when I spawn some asteroids at their random x,y is to turn their gravity on, set their speed and then calculate the amount per 100 ms that I need to turn/gravity the object so that it orbits the planet at px,py

Having a little trouble calculating this tho so before I give us and move onto something else I thought I would see if anyone here has any ideas.


abakobo(Posted 2015) [#2]
What math are you using? Formulas would be needed to understand your approach.

To have realistic orbits you have to use Newton's mecanics:

F=(G*Ma*Mb)/(D*D) and F=M.A

F is the intensity of the force (Newton)
G is the Gravity constant (optional for a game, but it's easier to work with "real" values then scale the image, it may need some change because we pull each xxms)
Ma is the mass of your asteroid a (Kg)
Mb is the mass of your asteroid b (the planet!) (Kg)
D is the distance between the center of the two asteoids (meter)
A is acceleration (meter/sec*sec)

The rotation of asteroids arenot gravity dependent but you can use the distance vector to make it turn "like the moon" or add the rotating speed if you want full simulation.

here's a code with my gravity example but the velocity for the orbit of rnd satelite is not yet calculated, I don't remeber the formula but will check... Do you want circular orbit or any orbits?




Paul - Taiphoz(Posted 2015) [#3]
I was trying to avoid fully calculating the gravity like that, what I am trying to solve in the most efficient way possible is Angle of change A from the vectors current angle so that I know how much to turn the object so that it maintains a circle around the central point, all my objects motions are dictated by the angle of its vector which then sets it's actual x and y velocities based on speed.

I might be trying to over simplify or possibly make life hard on myself lol ...




muddy_shoes(Posted 2015) [#4]
If you literally just want the asteroids to go in a circular orbit then you can just set the velocity vector to be perpendicular to the line between the asteroid and the thing it's orbiting. Alternatively, if you want the motion to be stable across long periods and varying framerates etc., you could just treat speed*deltatime as an orbital arclength and calculate the new position exactly.


abakobo(Posted 2015) [#5]
Yep if you just want circular orbits and you don't want it to interact with other gravity sensitive asteroids than your planet you don't really need physics. Just make it turn around when gravity is on and don't bother with forces...


Paul - Taiphoz(Posted 2015) [#6]
Muddy I fkn love you :), I was already calculating that once on spawn it never once occurred to me to just reset the angle over time, that just removed 20 lines of code and replaced it with a new orbiting boolean and a single line to just reset the objects angle thanks dude.


abakobo(Posted 2015) [#7]
Though you're not willing to use physics, i give the complement to the code in order to get circular orbit.

the speed for circular orbit is Sqrt(G*Mplanet/Distance) and is not dependent to the satelite Mass..

Here's the code. You can press Spacebar to launch a rnd satelite at circular orbit!




Why0Why(Posted 2015) [#8]
Hey abakobo, just wanted to say thanks for chiming in and I appreciate the example. It is great to have samples like that on the board!


Paul - Taiphoz(Posted 2015) [#9]
Yeah Examples are amazing for some of us, its not that I am un willing to use real physics its more like its not needed and the rest of the code is'nt really setup to work that way.


abakobo(Posted 2015) [#10]
more gravity fun for people who like simple BASIC but effective code example... (notice that function or class is not used (except for mojo and "Function Main" reqs...)

press space to add (several) satelite(s)!

;P



k.o.g.(Posted 2015) [#11]
@abakobo

is there any chance to contact you? i've done something similar, but have one question? please


abakobo(Posted 2015) [#12]
Feel free to ask the question here or on a new topic.... i'd rather like to answer publicly so every one can get info's.. (as it is the philosophy of formus!)
If you want it to keep secret!? i've sent you an email...

ako


abakobo(Posted 2015) [#13]
The email adress of your profile failed...


golomp(Posted 2015) [#14]
Hi Paul,

Thank you a lot for the work you have done.

Golomp