Download

Grab Dragging Camera view with mouse

I’m trying to setup a camera system where the player drags themselves around the world (similar to games like Black and White). I can use GetMousePosition(lastMouseDragX, lastMouseDragY); when they click and find the difference on tick to move the camera, however this doesn’t work like grabbing a piece of the world and dragging around.

What I would like is the mouse cursor to stay in the same position relative to the world while the camera moves around. This is a can be a bit more complex than getting mouse position because it depends on how far the object they clicked on in the world is from the camera and possibly resolutions/fov settings.

Anyone have some advice on how to accomplish this?

My first pass would be:

  • mouse down -> save location
  • mouse drag -> move camera based on mouse move direction relative to camera
  • update mouse position on screen -> cast ray through screen to the saved location when the mouse went down

Something like that.

Thanks for the idea, however that isn’t going to work. I want to drag the world around consistently with the mouse. Moving the mouse pointer to the location you would like doesn’t accomplish this. If things you grab are closer to the camera they will still move too fast and moving the users mouse around will just feel inconstant and bad.