问题描述
在我的场景中,我想抓取"地形,然后在移动光标时让相机平移(其高度、视图矢量、视野等都保持不变).
In my scene I have terrain that I want to "grab" and then have the camera pan (with its height, view vector, field of view, etc. all remaining the same) as I move the cursor.
所以最初的抓取"点将是世界空间中的工作点,我希望该点在我拖动时保持在光标下.
So the initial "grab" point will be the working point in world space, and I'd like that point to remain under the cursor as I drag.
我当前的解决方案是获取前一个和当前屏幕点,取消投影它们,从另一个中减去一个,然后用该向量转换我的相机.这与我想要的很接近,但光标并没有完全停留在初始场景位置上,如果您从地形边缘附近开始,这可能会出现问题.
My current solution is to take the previous and current screen points, unproject them, subtract one from the other, and translate my camera with that vector. This is close to what I want, but the cursor doesn't stay exactly over the initial scene position, which can be problematic if you start near the edge of the terrain.
// Calculate scene points
MthPoint3D current_scene_point =
camera->screenToScene(current_point.x, current_point.y);
MthPoint3D previous_scene_point =
camera->screenToScene(previous_point.x, previous_point.y);
// Make sure the cursor didn't go off the terrain
if (current_scene_point.x != MAX_FLOAT &&
previous_scene_point.x != MAX_FLOAT)
{
// Move the camera to match the distance
// covered by the cursor in the scene
camera->translate(
MthVector3D(
previous_scene_point.x - current_scene_point.x,
previous_scene_point.y - current_scene_point.y,
0.0));
}
感谢任何想法.
推荐答案
多睡一会儿:
- 获取相交点在世界空间和在模型空间(相对于模型的原点)的初始位置
- Get the initial position of your intersected point, in world space and in model space ( relative to the model's origin)
即使用 screenToScene()
i.e use screenToScene()
- 创建一条从相机穿过鼠标位置的光线:{ray.start, ray.dir}
ray.start 是 camera.pos,ray.dir 是 (screenToScene() - camera.pos)
ray.start is camera.pos, ray.dir is (screenToScene() - camera.pos)
- 解决NewPos = ray.start + x * ray.dir 知道NewPos.y = initialpos_worldspace.y;
-> ray.start.y + x*ray.dir.y = initialpos_worldspace.y
-> ray.start.y + x*ray.dir.y = initialpos_worldspace.y
-> x = ( initialpos_worldspace.y - ray.start.y)/rad.dir.y (注意dividebyzeroexception)
-> x = ( initialpos_worldspace.y - ray.start.y)/rad.dir.y (beware of dividebyzeroexception)
-> 在 NewPos_worldspace 中重新注入 x = ray.start + x * ray.dir
-> reinject x in NewPos_worldspace = ray.start + x * ray.dir
- 从中减去 initialpos_modelspace 以重新居中"模型
不过,最后一点似乎很可疑.
The last bit seems suspect, though.
这篇关于实施“抢夺"3D 场景中的相机平移工具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!