Hello Guest

Author Topic: Get the mouse coordinates based on the camera zoom  (Read 3145 times)

0 Members and 1 Guest are viewing this topic.

BitteWenden

  • Member
  • Posts: 13
    • View Profile
Get the mouse coordinates based on the camera zoom
« on: April 08, 2015, 07:12:29 AM »
I have an entity with an rectangle and I'm checking if the mouse is inside that rectangle and if the rectangle is inside the camera (to remove it if it's not). This works good until I zoom out / in.

How to get the mouse coordinates in the games coordiante system when the camera is zoomed in or out / How can I realise these examples with zoom?

« Last Edit: April 08, 2015, 08:43:28 AM by BitteWenden »

Kyle

  • Administrator
  • Member
  • Posts: 258
    • View Profile
Re: Get the mouse coordinates based on the camera zoom
« Reply #1 on: April 10, 2015, 05:56:16 AM »
A good question!  Right now I don't know the math, but somewhere I'm sure if you multiply by the zoom factor, you might get what you want.  I should probably at some point put in functions for getting a position on a surface with zoom and rotation factored in.  I'll look into this more soon.

BitteWenden

  • Member
  • Posts: 13
    • View Profile
Re: Get the mouse coordinates based on the camera zoom
« Reply #2 on: April 10, 2015, 10:41:39 AM »
Found out than SFML has a function for this. http://www.sfml-dev.org/tutorials/2.0/graphics-view.php (last section).
I tried it but the coordinate that this function returns is the same I that I give this function.
« Last Edit: April 10, 2015, 11:20:50 AM by BitteWenden »

BitteWenden

  • Member
  • Posts: 13
    • View Profile
Re: Get the mouse coordinates based on the camera zoom
« Reply #3 on: April 11, 2015, 08:47:12 AM »
I also noticed that when the zoomfactor get's below -1 the zoom "direction" get's reversed.

EDIT: Also, why is it that the zoomfactor goes up if you zoom in and goes out if you zoom out?
« Last Edit: April 14, 2015, 09:39:12 AM by BitteWenden »

Kyle

  • Administrator
  • Member
  • Posts: 258
    • View Profile
Re: Get the mouse coordinates based on the camera zoom
« Reply #4 on: April 14, 2015, 03:18:19 PM »
Hmm I dont think I ever planned on the zoom being below 0 so I have no clue what's going to happen there.  The zoom is the way it is because that's what made sense to me, like 2x zoom should be zoomed in, where as sfml treats that as zoomed out, so I switched it for Otter.

BitteWenden

  • Member
  • Posts: 13
    • View Profile
Re: Get the mouse coordinates based on the camera zoom
« Reply #5 on: April 14, 2015, 10:39:45 PM »
Maybe you could make two alternative ways of scaleing the camera. At the one side with the old way, and at the other side with something like camera.scale(int scale) which scales the camera as SFML does.

BitteWenden

  • Member
  • Posts: 13
    • View Profile
Re: Get the mouse coordinates based on the camera zoom
« Reply #6 on: April 16, 2015, 09:24:38 AM »
I managed to make a custom view with changing the Games UpdateView() method.

Code: [Select]
public void UpdateView() {
            if (CustomView == null)
            {
                View = new View(new FloatRect(0, 0, WindowWidth, WindowHeight));
                View.Zoom(1f / cameraZoom);
                View.Rotation = cameraAngle;
                Window.SetView(View);
            }
            else
            {
                View = CustomView;               
                Window.SetView(View);
               
            }
        }

I have a public View CustomView now.  If it's set to something the game will use that.

It works but I noticed its not as smooth as the normal Otter camera.
« Last Edit: April 16, 2015, 10:06:55 AM by BitteWenden »

TP

  • Member
  • Posts: 3
    • View Profile
Re: Get the mouse coordinates based on the camera zoom
« Reply #7 on: June 19, 2015, 05:36:46 AM »
Im sure theres an easier way but I´m using the following math to get the absolute position of the mouse including camera movement and zoom (Not camera angle though, so whatever you do don´t turn it ;) ).

float CameraXScaled = CameraX + (Width - CameraWidth) / 2;
float CameraYScaled = CameraY + (Height - CameraHeight) / 2;
float mouseX = CameraXScaled + (Input.MouseX / CameraZoom);
float mouseY = CameraYScaled + (Input.MouseY / CameraZoom);

As I said: Theres probably a simpler way to calculate it (If somebody finds one please let me know)