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\$
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\$
  • \$\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\$ – sukoshijon Jan 27 '18 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\$ – sukoshijon Feb 2 '18 at 23:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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