• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Programming orbital paths

S

SMFBirdman

Guest
Basically, I am trying to make a game where you shoot a missile around several planets, and you have to use the planet's gravity to get your trajectory right.

I need to know how to program a system where I can simulate planetary gravity while allowing the player enough leeway that they don't need to be a rocket scientist to get the trajectory right.

How can I make a system that will affect an object's direction without using the embedded gravity system?
 

chamaeleon

Member
Basically, I am trying to make a game where you shoot a missile around several planets, and you have to use the planet's gravity to get your trajectory right.

I need to know how to program a system where I can simulate planetary gravity while allowing the player enough leeway that they don't need to be a rocket scientist to get the trajectory right.

How can I make a system that will affect an object's direction without using the embedded gravity system?
Direction is a vector. Gravity pulls in a direction (towards the center of your gravity object for the sake of simplicity). Adding a movement vector with a gravity vector (scaled to give a pleasing result rather than aiming for realism perhaps) will give you a new direction. Linear algebra is the source of knowledge for this and any resource at your disposal on the internet will cover it in detail. And just like the physics system it is conceivable you need to simulate more than one direction and position change per step in order to smooth out the movement instead of doing it just once. Shorter movement allows your algorithm to approximate a curve movement better but of course takes longer to compute (not necessarily a big problem, just something to keep in mind, as you would be performing the same computation code as many times as needed to get the result you want).
 
S

SMFBirdman

Guest
Direction is a vector. Gravity pulls in a direction (towards the center of your gravity object for the sake of simplicity). Adding a movement vector with a gravity vector (scaled to give a pleasing result rather than aiming for realism perhaps) will give you a new direction. Linear algebra is the source of knowledge for this and any resource at your disposal on the internet will cover it in detail. And just like the physics system it is conceivable you need to simulate more than one direction and position change per step in order to smooth out the movement instead of doing it just once. Shorter movement allows your algorithm to approximate a curve movement better but of course takes longer to compute (not necessarily a big problem, just something to keep in mind, as you would be performing the same computation code as many times as needed to get the result you want).
That was good, but I'm mostly looking for what keywords and variable names I'm looking for. I'm fairly new to Game Maker Language, so I need help navigating the programming
 
It'll all be pretty basic stuff. There aren't really keywords or variable names specific to this problem, that you wouldn't already be familiar with if you've got a pretty firm grasp of the basics of gamemaker.
 

samspade

Member
While not in GML, I would recommend these two playlists as they use a variant of Java and are close enough to GML that it translates pretty easily (once you add some type of vector script of which there are a couple on the marketplace). If it is simple enough you can even use the built in speed vector.

https://www.youtube.com/playlist?list=PLRqwX-V7Uu6ZwSmtE13iJBcoI-r4y7iEc

https://www.youtube.com/playlist?list=PLRqwX-V7Uu6ZRrqLcQ5BkBKmBLiGD8n4O

This is how I learned to do what you're asking about.
 
S

SMFBirdman

Guest
While not in GML, I would recommend these two playlists as they use a variant of Java and are close enough to GML that it translates pretty easily (once you add some type of vector script of which there are a couple on the marketplace). If it is simple enough you can even use the built in speed vector.

https://www.youtube.com/playlist?list=PLRqwX-V7Uu6ZwSmtE13iJBcoI-r4y7iEc

https://www.youtube.com/playlist?list=PLRqwX-V7Uu6ZRrqLcQ5BkBKmBLiGD8n4O

This is how I learned to do what you're asking about.
Thanks.
 

SoVes

Member
I'm not a physicist, but what I did for this was to get accurate enough results was to use Newton's law of universal gravitation https://en.wikipedia.org/wiki/Newton's_law_of_universal_gravitation

in gml it would be something like this. called in the object that the satellites orbit around
Code:
with(satellite_object){
     
        var r = point_distance(x,y,other.x,other.y);
        F = G*m*other.m/(r*r);


        var dir = point_direction(x,y,other.x,other.y);
 
        hspd+= lengthdir_x(F,dir);
        yspd += lengthdir_y(F,dir);
    }
}
this would be called in the planet object to affect all of the satelites

so G would be any gravity speed. In my case I used 0.01
F is just a variable to hold the force that basically is the force the planet puts on the satellite
m is the mass of the object. can be any number but larger mass means it pulls the satellite harder and for the satellite it means it is harder to accelerate. I used 1 for the satellites and 10000 for the planet

hspd and yspd you understand. they're just the velocities of the satellites. remember that you need to also add them to the x and y in their step event
 

Yal

šŸ§ *penguin noises*
GMC Elder
Look into motion_add(), and remember that the formula for gravity between two particles (or astral bodies) is
upload_2019-11-17_0-45-18.png

where F is the total gravity, G is a random constant, m1 and m2 are the two bodies' masses (basically "weight" if you don't know what a mass is), and r is the distance between them.


Basically, multiply the weight of the player with the weight of every planet, divide by the distance between them in pixels to the power of two, and multiply the result with a random constant (that you tweak until it feels right). Use motion_add() to add this value to the speed vector every step (in the direction from the spaceship to the planet). Since you do this for every planet, every planet has an orbital gravity impact on the ship. Huzzah, done!
 
S

SMFBirdman

Guest
I'm not a physicist, but what I did for this was to get accurate enough results was to use Newton's law of universal gravitation https://en.wikipedia.org/wiki/Newton's_law_of_universal_gravitation

in gml it would be something like this. called in the object that the satellites orbit around
Code:
with(satellite_object){
    
        var r = point_distance(x,y,other.x,other.y);
        F = G*m*other.m/(r*r);


        var dir = point_direction(x,y,other.x,other.y);
 
        hspd+= lengthdir_x(F,dir);
        yspd += lengthdir_y(F,dir);
    }
}
this would be called in the planet object to affect all of the satelites

so G would be any gravity speed. In my case I used 0.01
F is just a variable to hold the force that basically is the force the planet puts on the satellite
m is the mass of the object. can be any number but larger mass means it pulls the satellite harder and for the satellite it means it is harder to accelerate. I used 1 for the satellites and 10000 for the planet

hspd and yspd you understand. they're just the velocities of the satellites. remember that you need to also add them to the x and y in their step event
Awesome! This is exactly what I was looking for. Thanks
 
S

SMFBirdman

Guest
Look into motion_add(), and remember that the formula for gravity between two particles (or astral bodies) is
View attachment 27568

where F is the total gravity, G is a random constant, m1 and m2 are the two bodies' masses (basically "weight" if you don't know what a mass is), and r is the distance between them.


Basically, multiply the weight of the player with the weight of every planet, divide by the distance between them in pixels to the power of two, and multiply the result with a random constant (that you tweak until it feels right). Use motion_add() to add this value to the speed vector every step (in the direction from the spaceship to the planet). Since you do this for every planet, every planet has an orbital gravity impact on the ship. Huzzah, done!
Thank you, too.
 
Top