Interactive demo. Mouseover to activate, drag planets around.
This is an example of making an instance orbit around other instance in GameMaker.
In contrast to other similar examples (and commonly given advice of the kind), it allows to define orbiting speed in a common way (pixels/frame instead of degrees/frame).
This is accomplished by calculating the distance to orbit' center (from current position), calculating the orbit' length, and thus converting pixel speed to degree speed.
New position is then calculated and instance' speed is adjusted for it to reach it the next frame.
This allows to use direction in collision calculations as per usual.
Overall, the code is simple enough but documented regardless.
var cx, cy, crad, clen, cdir, cnext, nx, ny; // retrieve variables for later use: cx = obj_sun.x; cy = obj_sun.y; crad = point_distance(cx, cy, x, y); // find "length" of circle: clen = crad * pi * 2; // find current angle on circle: cdir = point_direction(cx, cy, x, y); // find next angle on circle: cnext = cdir + vel * 360 / clen; // find coordinates of next point: nx = cx + lengthdir_x(crad, cnext); ny = cy + lengthdir_y(crad, cnext); // set speed vector to reach that point in next step: hspeed = nx - x; vspeed = ny - y;
Where vel is the object's velocity (px/step) and obj_sun is the thing to orbit around.
Code is to be included in Step event.