Dragging Objects in 2d

How can we drag an object in 2d space with a mouse with the given coordinates:

top left of window = (0, 0)
top right of window = (window.width, 0)
bottom left of window = (0, window.height)
bottom right of window = (window.width, window.height)

this can be an Object class :
class Object {
public:
float x;
float y;
float width;
float height;



}

You need to have control of the drawing space, and control of where things are drawn, and to know where they are drawn, and to be able to detect a mouse click, and to be able to match up the mouse click with an object location, and be able to detect mouse drag, and redraw the object you have control of in the drawing space you have control of.

I suspect this is beyond you. Start by working out how to open a window and draw in it.
I have done all of that, I have access to mouse, window, and all of framework, actually I have created a 2d game already with OpenGL :) Just dragging with mouse is a bit of a problem.
You need to intercept the mouse-down, mouse-up and mouse-move events.
You also need a var dragging of type bool.
In the mouse-down event you set dragging to true if the mouse is inside the object.
In the mouse-move event if dragging is true then you move the object according to the mouse pos.
In the mouse-up event you set dragging to false.
hassanAman, what Windowing and Event framework are you using?

SFML? Qt? wxWidgets? Windows-specific?
I can help with SFML or wxWidgets.
GLFW is what I am using. Its written in C.
Well one way you could do it (and this may be a bit advanced as it requires a solid understanding of computer graphics coordinate systems) is to cast a ray from the mouse pointer that's usually in screen space into the game world that's in world space and then check for a ray-plane intersection provided that your quad is enclosed in a plane collider. Should the ray intersect the collider around the quad, you can set the quad's position equal to the mouse's position per frame until the user clicks again. You will need to intercept the different mouse states and keep track of a boolean like Thomas said. GLFW does support this so you should look into their documentations.
Topic archived. No new replies allowed.