算法 3d 轨道相机控制与鼠标拖动 [英] Algorithm 3d orbiting camera control with mouse drag

查看:27
本文介绍了算法 3d 轨道相机控制与鼠标拖动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮忙,

我找不到关于此的详细解释,这不是特定于语言的,也不是依赖于库的,因为出于某种原因我想自己控制数学.

I couldn't find detailed explanation about this which is not language specific and not library dependent, because I want to control the math myself for some reason.

如何使用鼠标创建轨道相机控件,例如在 Google SketchUp 中单击鼠标中键拖动?

How to create orbiting camera control with mouse, like middle-click drag in Google SketchUp?

* 在 Google SketchUp 中,相机可以通过拖动鼠标在平面中间(始终为 0,0,0)以圆形轨道移动假想对象.它可以水平、垂直、甚至对角、所有方向运行.

* In Google SketchUp, the camera can move circularly orbiting imaginary object in the middle of the plane (not always 0,0,0) by dragging the mouse. It can orbit horizontally, vertically, even diagonally, all directions.

推荐答案

最简单的解决方案是存储 X/Y 方向角度并最终存储缩放级别.然后,您在某个 MouseMove 事件中修改角度,并使用它们来计算相机方向.

The simplest solution is to store X/Y direction angles and eventually a zoom level. Then, you modify the angles in some MouseMove event, and use them to compute camera direction.

这是一些鼠标拖动的伪代码:

Here's some pseudo-code for mouse drags:

OnMouseDown:
    mouseDragging = true;
    lastX = mouse.x;
    lastY = mouse.y;

OnMouseUp:
    mouseDragging = false;

OnMouseMove:
    if( mouseDragging ) {
        angleX += (mouse.x-lastX) * speedX;
        angleY += (mouse.y-lastY) * speedY;
    }

OnMouseWheel:
    zoom += mouse.wheelDelta;

根据这些数据,您可以构建相机位置.我不知道你用什么来做这个,但这里有一个来自 OpenGL 的例子:

Given those data you can build a camera position. I don't know what are you using for this but here's an example from OpenGL:

glLoadIdentity();
glTranslatef( 0.0f, 0.0f, -zoom );
glRotatef( angleY, 1.0f, 0.0f, 0.0f );
glRotatef( angleX, 0.0f, 1.0f, 0.0f );
glTranslatef( -orbitCenterX, -orbitCenterY, -orbitCenterZ );

这篇关于算法 3d 轨道相机控制与鼠标拖动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆