问题描述
在我的场景我有地形,我想抢,然后让相机锅(其高度,查看载体,视野等方面都保持不变),因为我移动光标。
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.
我目前的解决方案是采取previous和当前屏幕分,unproject他们,减去从另一个,并翻译我的相机与载体。这是接近我想要的,但光标不完全停留在初始场景的位置,如果你开始地形的边缘附近,可能会产生问题。
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));
}
任何想法是AP preciated。
Any ideas are appreciated.
推荐答案
对于一些更多的睡眠:
- 让您的相交点的初始位置,在世界空间中的和的在模型空间(相对于模型的原点)
- 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)
- >回注的X NewPos_worldspace = ray.start + X * ray.dir
-> reinject x in NewPos_worldspace = ray.start + x * ray.dir
- substract initialpos_modelspace从重新中心的模式
最后一点似乎犯罪嫌疑人,但。
The last bit seems suspect, though.
这篇关于实现一个"抢"在3D场景摄像机平移工具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!