• 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!

Asteroid Belt...Sort of...

C

Chris White

Guest
So I'm making a game like the old atari asteriods, but instead of having a bunch of asteriods randomly spawning and pissing off in every which way i want to keep them in a "belt" like on a planet, so they stay within a certain area of the map, and move and 1 direction, as an asteriod belt does. I thought about using views, like view_xview but it didn't work they still kept spawning everywhere. I don't know any other way of doing this so any help would be appreciated.

Thanks
-Chris
 
W

whale_cancer

Guest
If you want to make the asteroids orbit a planet, you will need to have the asteroids continually fall towards the planet. In your planet object, have it move all asteroids towards itself at whatever speed each step.

Then, for the asteroids themselves, have them move faster than they fall towards the planet but at right angle to the force of gravity (the movement the planet forces on the asteroids). This will create basic orbiting asteroids.

PS: This is a pretty neat twist on a classic.
 
C

Chris White

Guest
If you want to make the asteroids orbit a planet, you will need to have the asteroids continually fall towards the planet. In your planet object, have it move all asteroids towards itself at whatever speed each step.

Then, for the asteroids themselves, have them move faster than they fall towards the planet but at right angle to the force of gravity (the movement the planet forces on the asteroids). This will create basic orbiting asteroids.

PS: This is a pretty neat twist on a classic.
I don't want them to necessarily rotate around a planet (that would be cool, ill have to think about that) At the moment I want it a bit more centered just having them travel upwards(or downwards) in the center of the map. I used instance_create(random(view_xview[1]),random(view_yview[1]), obj_asteroid; i thought that would keep them spawning within the view i set up but they still spawned everywhere.
 
W

whale_cancer

Guest
Oh, I see.

random() will generate a number between 0 and n, so your code is off. Try...

Code:
instance_create(random_range(view_xview[1], view_xview[1]+view_wview[1]), random_range(view_yview[1], view_hview[1]), obj_asteroid);
 
C

Chris White

Guest
Oh, I see.

random() will generate a number between 0 and n, so your code is off. Try...

Code:
instance_create(random_range(view_xview[1], view_xview[1]+view_wview[1]), random_range(view_yview[1], view_hview[1]), obj_asteroid);
oh sick! that worked, awesome, thank you so much :D
 
W

whale_cancer

Guest
Incidentally, what does the math for asteroids orbiting a planet look like?
I explained the logic in my post. If you want to try and implement it and run into problems, post your code and I can take a look. I'm no going to just write it all up, though.
 
I explained the logic in my post. If you want to try and implement it and run into problems, post your code and I can take a look. I'm no going to just write it all up, though.
I decided to take a shot at this, and in doing so I discovered something interesting;
I tried this code (which ultimately was not the right way to solve this problem), but it did make all the differently shaped and angled asteroids (400+ of them) fly towards the point of attraction. Now, after they all arrived at their destination, what I saw created was a perfect sphere! I thought, this must be the way real planetary bodies are created when all the matter gets smashed together over and over... and here it was happening in a Game maker room! neat.
Code:
direction = point_direction(x, y, other.x, other.y)
Thanks for the journey!
 
Hi. I have a bunch of different solutions for you.

First, if you want to have realistic gravity in your game, this is the fastest method that I know of for doing it in 2d. The planet's mass can be any positive value.
Code:
  //asteroid step event
  var _dist = point_distance(x,y,obj_planet.x,obj_planet.y);
  motion_add(point_direction(x,y,obj_planet.x,obj_planet.y),obj_planet.mass / (_dist * _dist));
To insert an asteroid into a circular orbit around the planet, you can do this:
Code:
direction = point_direction(x,y,obj_planet.x,obj_planet.y) + 90; //or -90
speed = sqrt(obj_planet.mass / point_distance(x,y,obj_planet.x,obj_planet.y));
But suppose you don't want the orbits to be perfectly circular, well then all you have to do is add some randomness there:
Code:
direction = point_direction(x,y,obj_planet.x,obj_planet.y) + random_range(80,100);
speed = sqrt(obj_planet.mass / point_distance(x,y,obj_planet.x,obj_planet.y)) * random_range(0.8,1.2);
Say your planet is rotating, and you want the satellite to remain at a fixed point above the surface, then you need to calculate at what distance (from the planet's center) that the satellite must orbit above the planet. Create the satellite at a point that is this distance from the planet's center:
Code:
geosynchronous_height = power(obj_planet.mass*power(360/obj_planet.spin,2)/(4*pi*pi),1/3);
 
Last edited:
Top