0
\$\begingroup\$

I've been trying to get sprites to stay on a set point on another rotating sprite. At the moment, it's very inaccurate, the original position is wrong, and when the rotation is applied the sprite drifts away from the point its intended to stay in. The code I'm currently using is as follows, and I'm pretty sure there's something wrong with it, I just don't know what it is.

rad = sqrt((x-originx)^2 + (y-originy)^2)
x = originx + cos(cameraangle+asin(x-originx)) * rad)
y = originy + (sin(cameraangle+acos(y-originy)) * rad)
\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

Based on the notation i'm assuming this is in GameMaker. If you're not using Gamemaker, you could derive a similar solution from this code. Anyways, a much simpler way to do this would be to use lengthdir. I'm not sure about your exact situation, but if obj_a is your rotating object, then run this code in object b:

var len = 10;//length in pixels

x = obj_a.x + lengthdir_x(obj.x,obj.y,len);

y = obh_a.y + lengthdir_y(obj.x,obj.y,len);

\$\endgroup\$
2
  • \$\begingroup\$ I'm actually using an implementation of the Ruby Game Scripting System that was used in the RPG Maker series, but outside of RPG Maker (for a number of reasons). And I probably should have mentioned that I need multiple objects at different positions, hence why I've not been using a fixed length. \$\endgroup\$
    – rainefall
    Commented Jan 27, 2018 at 10:32
  • \$\begingroup\$ Well, I was able to figure out how the Gamemaker specific functions worked and added them to the engine I was using, and it works now. This also helped me figure out how to do another thing I was stuck on as well... \$\endgroup\$
    – rainefall
    Commented Feb 2, 2018 at 23:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .