鼠标旋转
Mouse for rotation
我想知道你们是如何处理鼠标控制的。
从很多游戏中了解到,如果我想旋转我的相机,鼠标通常是锁定的。例如射击游戏就是这样。
但是如何检测鼠标是否移动,是否被阻止移动?
我不明白这个逻辑。
希望有人能帮助我。非常感谢!
在我的场景中,我没有禁用鼠标,我只是禁用了光标。
glfwSetInputMode(renderer::get_window(), GLFW_CURSOR, GLFW_CURSOR_DISABLED);
这是在我初始化场景时完成的。
然后当我移动鼠标时,我会在我的更新部分(每帧调用)检查它在做什么
static double ratio_width = quarter_pi<float>() / static_cast<float>(renderer::get_screen_width());
static double ratio_height = (quarter_pi<float>() * (static_cast<float>(renderer::get_screen_height()) / static_cast<float>(renderer::get_screen_width()))) / static_cast<float>(renderer::get_screen_height());
double current_x = 0;
double current_y = 0;
glfwGetCursorPos(renderer::get_window(), ¤t_x, ¤t_y);
double delta_x = current_x - prev_x;
double delta_y = current_y - prev_y;
delta_x *= ratio_width;
delta_y *= ratio_height;
cam.rotate(delta_x, -delta_y);
prev_x = current_x;
prev_y = current_y;
当然你需要的不仅仅是身体运动,比如俯仰、偏航等。但这些是基础。
所以位置设置为 (0,0) - 每一帧的中心,我们测量鼠标远离它的移动,然后将相机移动该量。然后当一个新帧到来时它会再次重置,但此时我们不移动相机,只移动光标位置。或者更确切地说,这就是我们正在有效地做的事情。
抱歉,我想我没有很好地解释最后一点,希望我的代码更有用。它是 C++,带有一些 OpenGL 相关的库,如 glew32。
我想知道你们是如何处理鼠标控制的。
从很多游戏中了解到,如果我想旋转我的相机,鼠标通常是锁定的。例如射击游戏就是这样。
但是如何检测鼠标是否移动,是否被阻止移动?
我不明白这个逻辑。
希望有人能帮助我。非常感谢!
在我的场景中,我没有禁用鼠标,我只是禁用了光标。
glfwSetInputMode(renderer::get_window(), GLFW_CURSOR, GLFW_CURSOR_DISABLED);
这是在我初始化场景时完成的。
然后当我移动鼠标时,我会在我的更新部分(每帧调用)检查它在做什么
static double ratio_width = quarter_pi<float>() / static_cast<float>(renderer::get_screen_width());
static double ratio_height = (quarter_pi<float>() * (static_cast<float>(renderer::get_screen_height()) / static_cast<float>(renderer::get_screen_width()))) / static_cast<float>(renderer::get_screen_height());
double current_x = 0;
double current_y = 0;
glfwGetCursorPos(renderer::get_window(), ¤t_x, ¤t_y);
double delta_x = current_x - prev_x;
double delta_y = current_y - prev_y;
delta_x *= ratio_width;
delta_y *= ratio_height;
cam.rotate(delta_x, -delta_y);
prev_x = current_x;
prev_y = current_y;
当然你需要的不仅仅是身体运动,比如俯仰、偏航等。但这些是基础。
所以位置设置为 (0,0) - 每一帧的中心,我们测量鼠标远离它的移动,然后将相机移动该量。然后当一个新帧到来时它会再次重置,但此时我们不移动相机,只移动光标位置。或者更确切地说,这就是我们正在有效地做的事情。
抱歉,我想我没有很好地解释最后一点,希望我的代码更有用。它是 C++,带有一些 OpenGL 相关的库,如 glew32。