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);
}
I have found an answer. What you need to do is use this code I wrote:
public static void UpdateRelative(int rt, bool doRound = false)
{
float hw = Game.Instance.Width / 2;
float hh = Game.Instance.Height / 2;
Vector2f CameraCenter = new Vector2f();
CameraCenter.X = hw;
CameraCenter.Y = hh;
float diffx;
float diffy;
MouseXRelative = CameraCenter.X - MouseX;
MouseYRelative = CameraCenter.Y - MouseY;
if (doRound)
{
MouseXRelative.RoundTo(rt);
MouseYRelative.RoundTo(rt);
}
diffx = MouseXRelative - MouseX;
diffy = MouseYRelative - MouseY;
MouseXRelative = MouseXRelative - diffx;
MouseYRelative = MouseYRelative - diffy;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.