I am making a game library using SFML.Net
, and I want to be able to place a 50x50 tile onto the screen.
I have the Window width, the Window height, the MouseX/MouseY, and an offset based on a custom camera. When WASD is pressed, the offset is potentially added/subtracted 0.5 pixels.
So, I need to know how to get the mouseX, relative to all of these variables, so I can place a tile with the illusion of it being where the user mouse-pressed.
I have tried the code below.
Note: Game.Instance.Scenes[Game.Instance.CurrentScene].Camera.Offset
is the camera offset.
public static float MouseX = 0;
public static float MouseY = 0;
public static float MouseXRelative = 0;
public static float MouseYRelative = 0;
public static void UpdateRelative()
{
MouseXRelative = MouseX + Game.Instance.Scenes[Game.Instance.CurrentScene].Camera.Offset.X;
MouseYRelative = MouseY + Game.Instance.Scenes[Game.Instance.CurrentScene].Camera.Offset.Y;
}
And this code below.
Note: this detects when the left mouse button is pressed, and then creates a GameObject, MyGO
, **and positions it based on the MouseX/MouseY and offset. Also, RoundTo()
is a simple extension method I made to allow the ability to round to a certain number.
if (MouseHandle.LeftButtonDown)
{
float x = MouseHandle.MouseX.RoundTo(50) + Game.Instance.Scenes[Game.Instance.CurrentScene].Camera.Offset.X;
float y = MouseHandle.MouseY.RoundTo(50) + Game.Instance.Scenes[Game.Instance.CurrentScene].Camera.Offset.Y;
MyGO gm3 = new MyGO();
gm3.SetPosition(x, y);
Game.Instance.Scenes[Game.Instance.CurrentScene].Add(gm3);
}