GML [SOLVED]Help with Orbital Movement that follows the mouse?

F

FadedSketch

Guest
I've been working on this little prototype this past week, and I've been quite stumped at the start of it. Looks like I have much to learn, lol. :confused:

I followed the code from this post:https://forum.yoyogames.com/index.p...ovement-around-a-given-point.8030/#post-56644 for the general circular movement, but have been having a difficult time getting it to follow the mouse. (Like the player will follow the mouse, but is snapped to a circle path around the planet/center.) And the sprite (which right now is an Asteroid-esque triangle) needs to always be pointing to the mouse.
Here's my code:
Code:
CREATE EVENT:
Orbit = 64; // Orbit distance
Angle = 0; // Current orbital angle
Speed = 1; // Orbital speed
Center_X = room_width / 2; // x of orbital center
Center_Y = room_height / 2; // y of orbital center
---
STEP EVENT:

Speed = sign(mouse_x);

// Orbital motion
Angle += Speed;
image_xscale = -1;
image_angle += Speed;
if(Angle >= 360) Angle -= 360;
if (image_angle >= 360) image_angle -= 360;


// Update position
x = lengthdir_x(64, Angle) + Planet.x;
y = lengthdir_y(64, Angle) + Planet.y;
---
The step event is a bit messy, I've just been trying all sorts of things that I can think of to get it to work.

So, any help? I'd much prefer you guys could point me in the right direction, vs. just showing the answer. I really want to learn as much as I can. :p But of course anything would be appreciated. ^^
 

YellowAfterlife

ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ
Forum Staff
Moderator
I suppose a relatively simple approach would be to find target direction (from planet to mouse), current direction (from planet to ship), find angle_difference between them, clamp it to -Speed..Speed range, and add that instead of Speed to Angle.

Updated step event:
Code:
var cdir = point_direction(Center_X, Center_Y, x, y);
var tdir = point_direction(Center_X, Center_Y, mouse_x, mouse_y);
// Orbital motion
Angle += clamp(angle_difference(tdir, cdir), -Speed, Speed);
image_xscale = -1;
// Update position
x = lengthdir_x(64, Angle) + Center_X;
y = lengthdir_y(64, Angle) + Center_Y;
image_angle = point_direction(x, y, mouse_x, mouse_y);
Live demo
 
F

FadedSketch

Guest
I suppose a relatively simple approach would be to find target direction (from planet to mouse), current direction (from planet to ship), find angle_difference between them, clamp it to -Speed..Speed range, and add that instead of Speed to Angle.

Updated step event:
Code:
var cdir = point_direction(Center_X, Center_Y, x, y);
var tdir = point_direction(Center_X, Center_Y, mouse_x, mouse_y);
// Orbital motion
Angle += clamp(angle_difference(tdir, cdir), -Speed, Speed);
image_xscale = -1;
// Update position
x = lengthdir_x(64, Angle) + Center_X;
y = lengthdir_y(64, Angle) + Center_Y;
image_angle = point_direction(x, y, mouse_x, mouse_y);
Live demo
This is just what I needed! :) Thank you very much!
 
Top