0

我想知道如何累积相机旋转,因此当我再次单击屏幕时,旋转不会重置到该单击位置,而是从该点开始跟随旋转,如果这有意义的话

这是我的代码。这在单击/触摸拖动事件时执行。

ofVec3f camPos      = ofVec3f(0, 0, camDistance);
ofVec3f centerPos   = ofVec3f(0, 0, 0);

static float halfWidth  = ofGetWidth()/2;
static float halfHeight = ofGetHeight()/2;

float rotX = (touch.x-halfHeight) / (halfWidth);
float rotY = (touch.y-halfHeight) / (halfHeight);

camPos.rotate( rotY * 90, ofVec3f(1, 0, 0) );
camPos.rotate( rotX * 180, ofVec3f(0, 1, 0) );
camPos += centerPos;

cam.setPosition( camPos );
cam.lookAt( centerPos );
4

1 回答 1

0

对于初学者,您需要在某个地方持久存储一些旋转数据,这最好作为您的一个类中的成员,但为了仅修改您提供的代码,它们可以是静态局部变量。

ofVec3f camPos      = ofVec3f(0, 0, camDistance);
ofVec3f centerPos   = ofVec3f(0, 0, 0);

//there is no need for these to be static unless ofGetWidth
//and ofGetHeight are expensive
float halfWidth  = ofGetWidth()/2;
float halfHeight = ofGetHeight()/2;

//these accumlate all change
static float camRotX=0.0f;
static float camRotY=0.0f;

float rotX = (touch.x-halfHeight) / (halfWidth);
float rotY = (touch.y-halfHeight) / (halfHeight);

camRotX+=rotX;
camRotY+=rotY;

camPos.rotate( camRotY * 90, ofVec3f(1, 0, 0) );
camPos.rotate( camRotX * 180, ofVec3f(0, 1, 0) );
camPos += centerPos;

cam.setPosition( camPos );
cam.lookAt( centerPos );

这可能会满足您对鼠标单击的要求,但它不适用于触摸事件或拖动鼠标。对于触摸事件,您要记录触摸开始位置并将其用作旋转的基础,而不是使用屏幕中心。

于 2011-05-14T21:15:53.210 回答