object is jumping back and forth between the correct location and incorrect

Xer0botXer0

Senpai
Greetings!

xpos = device_mouse_x_to_gui(0) - (xpos - device_mouse_x_to_gui(0));

When dragging an object with the mouse it rapidly jumps between the expected location and the distance "(xpos - device_mouse_x_to_gui(0)".

I'm not sure why it's making that jump ?
 
Last edited:

TheouAegis

Member
a = 64 - (0 - 64) [128]
a = 64 - (128 - 64) [0]

Do the math and you will see your formula is wrong. (We've all been there.)

What are you trying to make happen?
 
Last edited:

FrostyCat

Redemption Seeker
Your line is functionally identical to this if you cared to evaluate it:
GML:
xpos = 2*device_mouse_x_to_gui(0)-xpos;
Is that what you are really after? What is the mathematical reasoning behind it?

Before you put down another line of code, make sure there is solid mathematical and logical reasoning behind it. Don't get into the habit of infinite monkey programming.
 

TheouAegis

Member
Is this the logic you are going for?

roomX += (mouseX - screenX) * guiscale;
screenX = mouseX;

Set screenX to mouseX AND adjust roomX accordingly?
 

Xer0botXer0

Senpai
Good lord, alright.

In other words, I'm trying to get object A to imitate the movements of object B while maintaining it's relative position.

I just looked at that that math again and had a good chuckle. it's back and forth xD
I'll figure it out after a cuppa coffee but don't take my word for it.
 
Last edited:

TheouAegis

Member
Okay, but you only have 1 object in your code. Do you mean the mouse is to be treated as an object? In that case, you need to save the mouse position during one event (I think End Step, maybe Begin Step suffices), and adjust xpos during another by how much the mouse moved.

mouse_previous = device_mouse_x_to_gui(0);

xpos += device_mouse_x_to_gui(0) - mouse_previous
 
Top