Dragging a 2D object with mouse

Here’s the situation:

I need to be able to translate a 2D object (a triangle) around the screen based on where I am dragging the mouse. I can do this just fine, but there is one part that I want to change that I have been having trouble with.

When I click on the object to begin dragging it, the object immediately snaps to a position where the mouse cursor is in the center of the object. I want the object to be dragged from the point I click, not from the center of the object.

Hi,

it all depends on how you implemented that. By what you said, looks like you are updating the position of the object to the position of the mouse click/motion.

One possible solution would be to calculate the difference between the click point and the motion point, and apply that same difference to the position center of the object.

on click:
get referencePosition;

on move:
get movingPosition;
positionDifference = movingPosition - referencePosition;
objectCenter += positionDifference;
referencePosition = movingPosition;
repeat it

something like that.

Hi kram87,

can u send me the code to drag the object with mouse click?

as i alreadyasked this question in the forum…as i have to implement this in my code.

thanks in advance
santos

Hi kram87,

can u send me the code to drag the object with mouse click?

as i alreadyasked this question in the forum…as i have to implement this in my code.

thanks in advance
santos