使用DirectXTK Mouse时对角线鼠标移动为何卡顿严重?
解决DirectXTK11 Mouse类相对模式下对角线旋转卡顿问题
问题根源
低速对角线移动时,Raw Input返回的X/Y delta总有一个为0,这是鼠标硬件采样精度限制或Windows Raw Input默认处理逻辑导致的——极小的轴移动会被截断为0。直接放大增益(比如乘0.05f)会把单轴无数据的问题放大,最终导致旋转只能沿单轴跳变,无法平滑完成对角线移动。
具体解决办法
累积delta值,避免丢弃微小移动
维护两个累积变量存储未处理的delta,每次将Raw Input的delta累加进去,当累积值达到可处理阈值(比如1.0)时,提取对应数值用于旋转,剩余部分继续保留:float accumulatedX = 0.0f; float accumulatedY = 0.0f; const float threshold = 1.0f; // 每帧更新时 auto state = mouse->GetState(); accumulatedX += state.x; accumulatedY += state.y; // 处理X轴累积值 while (abs(accumulatedX) >= threshold) { float delta = copysign(threshold, accumulatedX); cameraYaw += delta * 0.05f; accumulatedX -= delta; } // 处理Y轴累积值 while (abs(accumulatedY) >= threshold) { float delta = copysign(threshold, accumulatedY); cameraPitch += delta * 0.05f; accumulatedY -= delta; }对delta做平滑滤波
通过加权平均混合多帧的delta值,抵消单帧某轴为0的异常情况,保留移动趋势:float smoothedDeltaX = 0.0f; float smoothedDeltaY = 0.0f; const float smoothFactor = 0.6f; // 数值越大平滑程度越高,可自行调整 auto state = mouse->GetState(); smoothedDeltaX = smoothedDeltaX * smoothFactor + state.x * (1.0f - smoothFactor); smoothedDeltaY = smoothedDeltaY * smoothFactor + state.y * (1.0f - smoothFactor); // 使用平滑后的delta旋转相机 cameraYaw += smoothedDeltaX * 0.05f; cameraPitch += smoothedDeltaY * 0.05f;调整系统和初始化设置
- 禁用Windows控制面板中的「提高指针精确度」选项,该功能会干扰低速移动时的Raw Input采样。
- 确保Mouse类的相对模式正确启用,初始化代码示例:
std::unique_ptr<Mouse> mouse = std::make_unique<Mouse>(); mouse->SetWindow(hwnd); mouse->SetMode(Mouse::MODE_RELATIVE);
优化增益应用时机
不要直接放大原始delta,先处理完精度问题(累积/平滑)后再乘以增益,避免放大单轴为0的误差。
内容的提问来源于stack exchange,提问作者jkinz3
相关产品推荐
相关产品推荐

