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

SOLVED Spacing Multiple Objects Along The Same Orbit

McJazzhands

Member
Heya folks! I'm working on my first solo project outside of some tutorials and I've decided to make a Gradius III clone. As such, I want to create some helper ships that orbit the player at a set distance without overlap.

What I'm trying to achieve:

Think the R. Options from Gradius III. As the player adds more (max 4), they begin to literally orbit the player at a fixed distance from one another. Pictured below.

orbit-1.pngorbit-2.pngorbit-3.pngorbit-4.png

Unfortunately, I can't seem to figure out how to space my helper ships out properly. I'm testing power ups at the moment so they'll spawn when the player collects a capsule, but they all spawn at the exact same location as they're added.

Orbit-problem.png

Is there a way to maintain a spacing between my helper ships?

What I've tried:

- Switch statement using a list of previous x and y positions updated by first helper ship once created. Game crashes once second instance is created, citing "REAL argument is undefined." Most likely because I had it in the create event rather than initializing the list in the step event. I'll try that after a break and if it fixes the issue I'll post an update accordingly.
- Modifying the starting angle for the helper ships as the instance count updates. Didn't change anything.

Other information

Version: 2.2.5.378

I'm still a novice in regards to coding, but I do have some basic familiarity with gml from attempts in previous years. I'm not looking for the exact code I should use so much as a nudge in the right direction or suggestions if that makes sense. Like am I on the right track with the switch statement or would you suggest another method? I'm open to ideas and learning how to use more of GameMaker's tools.

Thanks in advance!
 

TheouAegis

Member
How many orbiters are you going to have? Look closely at your screenshots. Options aren't spaced out relative to the number of Options present. Is that the style y ou want? Or do you want them spaced out evenly?

Gradius just used an array of relative positions. In GML, you can just use relative directions.
orbits[0,180,90,270]
When an option is created, assign it a value based on how many options already exist. So the first option is 0, the second is 1, the third is 2, the fourth is 3. Then you can position them relative to the ship.
Code:
var dir =  orbits[orbiter]+oPlayer.orbit_angle;
x = oPlayer.x + lengthdir_x(16, dir);
y = oPlayer.y + lengthdir_y(16, dir);
In this example, orbits is our array, orbiter is the value assigned to the option, orbit_angle is a variable in the player that increments each step in order to spin the orbiters.

Alternatively, you can calculate the angles relative to the number of orbiters.
Code:
var dir = orbiter * 360 / instance_number(oOrbiter) + oPlayer.orbit_angle;
x = oPlayer.x + lengthdir_x(16, dir);
y = oPlayer.y + lengthdir_y(16, dir);

Edit: You could speed things up by generating the array of all possible positions across a limited number of angles in an array at the start of the game and then just use that instead of lengthdir functions.
 

McJazzhands

Member
Alternatively, you can calculate the angles relative to the number of orbiters.
Code:
var dir = orbiter * 360 / instance_number(oOrbiter) + oPlayer.orbit_angle;
x = oPlayer.x + lengthdir_x(16, dir);
y = oPlayer.y + lengthdir_y(16, dir);

Edit: You could speed things up by generating the array of all possible positions across a limited number of angles in an array at the start of the game and then just use that instead of lengthdir functions.
Thank you! My plan is to keep it to four orbiters to keep it simple-ish. I ended up going with the option to calculate the angle relative to the number of orbiters and it works perfectly!



orbit-solution.png

For real, I don't know how to properly express how much I appreciate your help. It feels so good just having my little helper ships behave properly now!

EDIT: For posterity, I should leave the relevant code here in case someone else needs it.

Player create
GML:
orbAngle = 0;
Player Step
GML:
orbAngle += 10; //Speed at which orbiter revolves around ship

if (orbAngle > 360)
{
    orbAngle -= 360;
}
Orbiter Create
GML:
myID = instance_number(o_Orbiter);
Orbit = 50; //Orbit Distance
Orbiter Step
GML:
Center_X = o_luna.x; // x of orbital center
Center_Y = o_luna.y; // y of orbital center

// Update position
var dir = myID * 360 / instance_number(o_Orbiter) + o_luna.orbAngle;
x = lengthdir_x(Orbit, dir) + Center_X;
y = lengthdir_y(Orbit, dir) + Center_Y;
Messy? Most likely. Functional? Yes. I'll take it. Sorry for the long response post, but once again thank you so much for your help! To think all I had to do was move a few variables around.
 
Last edited:
Top