[英]Placing object based on different variables
I am making a game library using SFML.Net
, and I want to be able to place a 50x50 tile onto the screen.我正在使用
SFML.Net
制作一个游戏库,并且我希望能够在屏幕上放置一个 50x50 的图块。
I have the Window width, the Window height, the MouseX/MouseY, and an offset based on a custom camera .我有 Window 宽度、Window 高度、MouseX/MouseY 和基于自定义相机的偏移量。 When WASD is pressed, the offset is potentially added/subtracted 0.5 pixels.
按下 WASD 时,偏移量可能会增加/减少 0.5 个像素。
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.因此,我需要知道如何获取相对于所有这些变量的 mouseX,这样我就可以放置一个图块,使其错觉是用户按下鼠标的位置。
I have tried the code below.我已经尝试了下面的代码。
Note: Game.Instance.Scenes[Game.Instance.CurrentScene].Camera.Offset
is the camera offset.注意:
Game.Instance.Scenes[Game.Instance.CurrentScene].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.注意:这会检测何时按下鼠标左键,然后创建一个 GameObject、
MyGO
、**并根据 MouseX/MouseY 和偏移量定位它。 Also, RoundTo()
is a simple extension method I made to allow the ability to round to a certain number.此外,
RoundTo()
是我制作的一种简单扩展方法,允许四舍五入到某个数字。
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.