I want to know how can I accumulate the camera rotations so when I click the screen again the rotation doesn't reset to that click location but to follow the rotation from that point if that makes sense
Here is my code. This executes on click/touch drag event.
ofVec3f camPos = ofVec3f(0, 0, camDistance);
ofVec3f centerPos = ofVec3f(0, 0,开发者_JS百科 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 );
For starters, you need to store some rotation data someplace persistant, this would be best as members in one of your classes, but for the sake of just modifying the code you provided, they could be static local variables.
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 );
This might do what you want for mouse clicks, but it will not work well for touch events, or for dragging the mouse. For touch events you want to record the touch start location and use that as basis for rotation instead of using the center of the screen.
精彩评论