PDA

View Full Version : Dragging a 2D object with mouse



kram87
04-24-2009, 04:26 PM
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.

enunes
04-24-2009, 07:38 PM
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.

brett01
04-24-2009, 08:16 PM
If you want to rotate using a mouse????

SANTOS_GL
04-26-2009, 04:28 AM
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

SANTOS_GL
04-26-2009, 04:53 AM
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