Orbiting

Ever wanted some thing to orbit around a object?
Like a moon orbiting around a planet?

Illustration

Well, thats what we're going to make in this tutorial.
Now, fire up Game Maker and creat a new object and a room.
Thats all we need. No sprites or more objects.

Add a creation event to the object and put this code inside a script:

//The moon coordinates: xx = 0; yy = 0; //Distance between moon and planet: distance = 128; //Orbiting speed: orbspeed = 2; //In what direction from the planet the moon should be drawn: orbstep = 0;

You can change the distance and orbspeed to whatever you want, but you ought to keep it like this untill we are done.

Add the following code to the step event:

orbstep += orbspeed; xx = x + lengthdir_x(distance, orbstep); yy = y + lengthdir_y(distance, orbstep);

In this script we simply add the orbspeed to the orbstep.

lengthdir_x calculates the x position the moon should be drawn at.
lengthdir_y is of course the same as lengthdir_x. but returns y.

How it works

And finally, put this in the draw event:

//The planet: draw_circle(x, y, 24, 0); //The moon: draw_circle(xx, yy, 8, 0);

You can of course draw sprites or whatever at those positions.

Now, put the object in the middle of the room and hit F5.
You see ?

That wasnt very hard, right?

Download .gm6

Want to get in touch? Got some feedback for me? or a couple of requests? Follow me at twitter:
@ThomasPedersen

Need a reliable webhost? Take my word for it and go with Hostgator Green Web Hosting by HostGator



Warning: fread(): Length parameter must be greater than 0 in /customers/f/2/7/n1studios.net/httpd.www/minicounter.php on line 17