SFML 3D 鼠标外观
编辑2: 我解决了大部分问题,但有一点让我烦恼。当光标到达屏幕边缘并被拉到另一侧时,相机会抖动,这将不起作用。有人能看出如何阻止这种情况吗?
bool attention = true;
Vector2 p, mousePos;
private float MOUSE_SENSITIVITY = 4.0f;
private void OnMouseMove(object sender, MouseMoveEventArgs e)
{
float DeltX = 0, DeltY = 0;
int border = 2;
Console.WriteLine(attention + "");
if (attention == true)
{
p.X = e.X;
p.Y = e.Y;
DeltX = (float)(mousePos.X - e.X) / MOUSE_SENSITIVITY;
DeltY = (float)(mousePos.Y - e.Y) / MOUSE_SENSITIVITY;
}
else
{
mousePos = p;
}
attention = true;
if (e.X > App.Width - border)
{
attention = false;
App.SetCursorPosition((uint)border, (uint)e.Y);
DeltX = 0;
DeltY = 0;
}
else if (e.X < border)
{
attention = false;
App.SetCursorPosition((uint)(App.Width - border), (uint)e.Y);
DeltX = 0;
DeltY = 0;
}
if (e.Y > App.Height - border)
{
attention = false;
App.SetCursorPosition((uint)e.X, (uint)border);
DeltX = 0;
DeltY = 0;
}
else if (e.Y < border)
{
attention = false;
App.SetCursorPosition((uint)e.X, (uint)(App.Height - border));
DeltX = 0;
DeltY = 0;
}
Cam.RotateY(DeltX);
Cam.RotateX(DeltY);
mousePos = p;
}
Edit2:
I figured out most of the problem, but there is one annoyance that I have. When the cursor reaches the edge of the screen and is pulled to the other side, the camera jerks, which won't work. Can someone see how that could be stopped?
bool attention = true;
Vector2 p, mousePos;
private float MOUSE_SENSITIVITY = 4.0f;
private void OnMouseMove(object sender, MouseMoveEventArgs e)
{
float DeltX = 0, DeltY = 0;
int border = 2;
Console.WriteLine(attention + "");
if (attention == true)
{
p.X = e.X;
p.Y = e.Y;
DeltX = (float)(mousePos.X - e.X) / MOUSE_SENSITIVITY;
DeltY = (float)(mousePos.Y - e.Y) / MOUSE_SENSITIVITY;
}
else
{
mousePos = p;
}
attention = true;
if (e.X > App.Width - border)
{
attention = false;
App.SetCursorPosition((uint)border, (uint)e.Y);
DeltX = 0;
DeltY = 0;
}
else if (e.X < border)
{
attention = false;
App.SetCursorPosition((uint)(App.Width - border), (uint)e.Y);
DeltX = 0;
DeltY = 0;
}
if (e.Y > App.Height - border)
{
attention = false;
App.SetCursorPosition((uint)e.X, (uint)border);
DeltX = 0;
DeltY = 0;
}
else if (e.Y < border)
{
attention = false;
App.SetCursorPosition((uint)e.X, (uint)(App.Height - border));
DeltX = 0;
DeltY = 0;
}
Cam.RotateY(DeltX);
Cam.RotateX(DeltY);
mousePos = p;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通常,您将每帧的鼠标位置设置为窗口的中心。之前您读取鼠标位置并减去窗口的中心。这样您就可以轻松地获得每一帧的鼠标移动,而不必担心窗口边框。
Typically you set the mouse position to the center of the window each frame. Previously you read to mouse position and subtract the center of the window. This way you can easily get the mouse movement each frame without having to worry about window borders.
我自己还在起步阶段,所以请对此持保留态度。 (我正在尝试!)
我认为你的鼠标移动是以像素和单位来测量的。这意味着相机的完整旋转。通过除以 0.4 (MOUSE_MOVMENT,),您将影响“0.4 完整转”的某个倍数,(例如 152 像素 / .04 = 380 转,让您面朝与开始时相同的方向。)
尝试除以 256 0.4&看看是否效果更好。
I'm still getting up to speed myself, so please take this with a grain of salt. (I'm trying!)
I think your mouse movement is measured in pixels & this translates to complete revolutions of the camera. By dividing by 0.4, (MOUSE_MOVEMENT,) you're affecting some multiple of "0.4 complete revolutions", (e.g. 152 pixels / .04 = 380 revolutions, leaving you facing the same direction as you started in.)
Try dividing by 256 instead of 0.4 & see if that works better.