[SOLVED] How to make an object orbit around another moving object

So, I am attempting to make an object orbit in a circle around my player. I found this code by Simon Donkers online:
dir_now := point_direction(argument0,argument1,x,y);
dir_new := (dir_now + argument3 + 360)mod(360);
x := lengthdir_x(argument2,dir_new) + argument0;
y := lengthdir_y(argument2,dir_new) + argument1;

It works as long as the player is still, but when he starts moving, it messes with the circular movement of the objects I created get out of hand. I attempted to fix by making the object move together with the player like this, but it didn't work:
Px = obj_player.x
Pxi = obj_player.xprevious
Py = obj_player.y
Pyi = obj_player.yprevious

if(Px != Pxi) Dx = Px - Pxi
x += Dx;
>

if(Py != Pyi) Dy = Py - Pyi
y += Dy;
>

Is my code wrong? or does anyone knows any already made code that can make this kind of movement happen? Or in another case, should I just use paths instead?

obscene

Wizard of GML

It works as long as the player is still, but when he starts moving, it messes with the circular movement of the objects I created get out of hand

That could mean a lot of things so not sure how to help.

Besides you gotta show your code that you used and how you use it, what event, etc.

Gerald Tyler

Guest

So. here's what I use, and this may be a bit difficult to word correctly.

So you've got object A, say a Planet, then object B, which is say a Moon

Let's say you want the Moon to be 32x32, what I do is create a 96 width by 32 height sprite, which will be object C, which is just a helper object. I like to give it a semi-transparent color to test everything, and just toggle off the Visibility after everything is set.

So in the Planet's Create Event
my_orbit = instance_create(x,y,obj_C); ///This will create the space between the planet and the satellite

Planet's Step Event
my_orbit.x = x; ///Will likely have to perform adjustments to these since it'll be on a player instead of a planet.
my_orbit.y = y;


Then in Object C's create event
my_moon = instance_create(x+95,y,obj_moon);

Object C's step event
image_angle -= 2; ///Or use a positive if you want it to rotate the other way, and of course you can tie this to an alarm if you want
my_moon.x = x+95; ///These update the moon to follow object C as it moves
my_moon.y = y;


Lastly is the Moon's step event
image_angle -= 2; ///This will rotate the moon itself

*EDIT*
Actually trying it right now and not working, will post once I've finalized for you

Last edited by a moderator: Jan 8, 2017

signal

Guest
So, I am attempting to make an object orbit in a circle around my player
x = objPlayer.x + lengthdir_x(len, dir); y = objPlayer.y + lengthdir_y(len, dir);
..and increment the dir from 0 to 360 depending on how fast you want the orbit to be.

Qarak

Guest

I wanted to try this without using complicated points and directions. So, after 20 minutes or something of playing with sen and cosen I found this to be working.
Of course change the numbers or whatever. This should work because we are using both cos and sin on their respective axis., while the angle keeps constantly changing. The angle should get a mod to resets when he gets on 360 (not sure if it's important, probably it is?). The "+ 1" changes the speed, the 250 the radius (which is actually not the radius, we're just multiplying). You can easily make an ellipse if you use not equal values.

I'm no expert on this topic though and not sure if this works in every situation (probably?)

angle = angle + 1; x = object_0.x + (cos(angle) * 250); y = object_0.y + (sin(angle) * 250);

EDIT: Basically if you think about it (I sure didn't back in school days lmao) if you add cos(a) to sin(a) and keep changing a, you're making a circle. Correct me if I'm wrong, I might be saying some ugly stuff

If someone knows a way to do this WITHOUT using x and y, but instead with direction and motions, I'd gladly listen

lmcpereira

Guest

Please try the following code (to be put in the orbiting object):

//Create Event
orbit_target=o_player; // adapt to the name of your player object's name
orbit_speed=5; // how fast the orbiting object travels within it's orbit
orbit_length=50; // how big the orbit circunference is
orbit_place=0; // starting position of the object - in this case at the right of the player (not much relevant as it will change by orbiting in the steps)

// Step Event
x=orbit_target.x + lengthdir_x(orbit_length, orbit_place); // these 3 instructions make sure that in each step the orbiting object adjusts to the proper position
y=orbit_target.y + lengthdir_y(orbit_length, orbit_place); // these 3 instructions make sure that in each step the orbiting object adjusts to the proper position
orbit_place+=orbit_speed; // these 3 instructions make sure that in each step the orbiting object adjusts to the proper position

I think the code above is self explanatory but if you need any help understanding some part of it please let me know.

Gerald Tyler

Guest

Yeah sorry buddy, just can't get it to work, and that other math stuff is above my understanding unfortunately =(

Arcana4th

Guest

That could mean a lot of things so not sure how to help.

Besides you gotta show your code that you used and how you use it, what event, etc.

Sorry, I don't post here a lot, I'm still new to this. Anyway, I don't know enough english to properly explain what happens, so I made a small gif of what happens to the objects. and posted it on my twitter account, here.

This is how the code is. It should work by taking the difference of the x and y of the player each frame and adding that difference to the object before the actual rotational movement happens, the code is in each one of the objects orbiting the player. However, there's no difference when I use the piece of code I added and when I don't, so I'm guessing it isn't working.

Px = obj_player.x Pxi = obj_player.xprevious Py = obj_player.y Pyi = obj_player.yprevious if(Px != Pxi) < Dx = Px - Pxi x += Dx; >if(Py != Pyi) < Dy = Py - Pyi y += Dy; >dir_now = point_direction(Px, Py, x, y); dir_new = (dir_now + 1.5 + 360)mod(360); x = lengthdir_x(150, dir_new) + Px; y = lengthdir_y(150, dir_new) + Py;

Arcana4th

Guest

Yeah sorry buddy, just can't get it to work, and that other math stuff is above my understanding unfortunately =(

Tis okay, thanks for trying anyway. I'll pick your code and see if I can do something with it myself.

Qarak

Guest

Hey Arcana, did my code work on your project?
I didn't specify that you can change the starting position by changing how you declare the angle. Let's say you have two objects circling around: one will have angle = 0, the other angle = 180, so they have different starting positions.

EDIT: Made a small video, just in case it's the same thing you're looking for.
https://webmshare.com/play/9ny14

Last edited by a moderator: Jan 8, 2017

Arcana4th

Guest

Please try the following code (to be put in the orbiting object):

//Create Event
orbit_target=o_player; // adapt to the name of your player object's name
orbit_speed=5; // how fast the orbiting object travels within it's orbit
orbit_length=50; // how big the orbit circunference is
orbit_place=0; // starting position of the object - in this case at the right of the player (not much relevant as it will change by orbiting in the steps)

// Step Event
x=orbit_target.x + lengthdir_x(orbit_length, orbit_place); // these 3 instructions make sure that in each step the orbiting object adjusts to the proper position
y=orbit_target.y + lengthdir_y(orbit_length, orbit_place); // these 3 instructions make sure that in each step the orbiting object adjusts to the proper position
orbit_place+=orbit_speed; // these 3 instructions make sure that in each step the orbiting object adjusts to the proper position

I think the code above is self explanatory but if you need any help understanding some part of it please let me know.

The code worked perfectly, I could upload a video if you want to see it working.

Thanks a lot for the help^^

Arcana4th

Guest

Hey Arcana, did my code work on your project?
I didn't specify that you can change the starting position by changing how you declare the angle. Let's say you have two objects circling around: one will have angle = 0, the other angle = 180, so they have different starting positions.

EDIT: Made a small video, just in case it's the same thing you're looking for.
https://webmshare.com/play/9ny14

Yeah, the code didn't worked for some reason. I guess it's about the placing of the orbs I made, here's a video if you want to see it, outside of that, the movement itself worked perfectly, if I re arranjed the points where each orb appears the code would probably work finely. Thanks for the help^^

lmcpereira

Guest

The code worked perfectly, I could upload a video if you want to see it working.

Thanks a lot for the help^^

Hello, I'm happy it is working. Perhaps you could change the title of this thread to include the word [SOLVED].
regards.

Enes

Guest

Please try the following code (to be put in the orbiting object):

//Create Event
orbit_target=o_player; // adapt to the name of your player object's name
orbit_speed=5; // how fast the orbiting object travels within it's orbit
orbit_length=50; // how big the orbit circunference is
orbit_place=0; // starting position of the object - in this case at the right of the player (not much relevant as it will change by orbiting in the steps)

// Step Event
x=orbit_target.x + lengthdir_x(orbit_length, orbit_place); // these 3 instructions make sure that in each step the orbiting object adjusts to the proper position
y=orbit_target.y + lengthdir_y(orbit_length, orbit_place); // these 3 instructions make sure that in each step the orbiting object adjusts to the proper position
orbit_place+=orbit_speed; // these 3 instructions make sure that in each step the orbiting object adjusts to the proper position

I think the code above is self explanatory but if you need any help understanding some part of it please let me know.

can you also tell me, how you can actually make this code like "relative". so if player presses vk_anykey then this code will repeat itself till you stop pressing anykey. PLEASE HELP ME.