Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've started messing around with Game Maker Studio. The problem I'm having is trying to get a gravity code for orbiting.

Here's how i did it in XNA

        foreach (GravItem Item in StarSystem.ActiveItems.OfType<GravItem>())
            if (this != Item)
                Velocity += (10 * Vector2.Normalize(Item.Position - this.Position *
                    (this.Mass * Item.Mass) / (Vector2.DistanceSquared(this.Position, 
                    Item.Position)) / (this.Mass));

Simple and works well, things or bit and everything is nice.

but in Game maker i don't have the luxury of Vector2's or a For-each loop to loop threw all the objects that have a mass. I've tried a few different things but nothing seems to work

distance = distance_to_object(obj_moon);

hspeed += (0.5 * (distance) * (Mass * obj_moon.Mass) / (sqr(distance)) / Mass)
vspeed += (0.5 * (distance) * (Mass * obj_moon.Mass) / (sqr(distance)) / Mass)

thanks for the help

share|improve this question

I think that motion_add might use vector addition, which should produce an orbit effect. (you can use point_direction and the moon's coordinates as per the direction, force is your choice). That is assuming you are using vector addition for gravity. If you aren't, I am not entirely sure what you are attempting to achieve.

As per the foreach loop, Game Maker kind of does the loops for you. As such, you can implement it like... say, have objects for an asteroid and your other specific objects, and make an object that is the parent of all of them. Then use something like:

with (obj_parent)
    //your code here...

This should go through all obj_parent's (and thus,their descendants) and perform the code.

On a final note, hspeed and vspeed are... well, not my favorite. Note you can also use the per-object variables speed and direction, which work as you would expect. Might be more friendly than those depending on what your doing.

share|improve this answer
I'm still vary new to GM so I'll try this stuff out, The C# code i have works perfect for having things orbit. thanks for the help – Dusty Oct 13 '12 at 3:20

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.