moving 3d camera

hello all

My goal is to move a 3d camera in space, i have been looking at James George ofxCameraTrack but there is not an example of how to use it, does any one have an example of implementation?, or any other pointers to animating a camera?
i am on mac with of 0073

ofxCameraTrack
http://github.com/obviousjim/ofxCameraTrack/zipball/master-
best
Ben

Hey Ben,

when moving a camera, you have to consider two things, 1) the camera’s position in space 2) the camera’s orientation in space. In oF, the camera’s “orientation” is also called its “rotation”.

Positions are usually described as 3D vectors, whereas orientations/rotations are described in quaternions.

Every ofCamera is derived from ofNode, which should give you access to the position and the rotation as follows:

  
  
ofVec3f myCamGlobalPosition = myCam.getGlobalPosition();  
ofQuaternion myCamRotation = myCam.getGlobalOrientation();  
  

You can move the camera’s position by tweening its position and simultaneously tweening its orientation over time, like so, assuming that your camera is called “myCam”:

  
  
	// ofCamera myCam;   
	float tweenvalue = (ofGetElapsedTimeMillis() % 2000) /2000.f; // this will slowly change from 0.0f to 1.0f, resetting every 2 seconds  
  
	ofQuaternion startQuat;  
	ofQuaternion targetQuat;  
	ofVec3f startPos;  
	ofVec3f targetPos;  
  
	// we define the camer's start and end orientation here:  
	startQuat.makeRotate(0, 0, 1, 0);			// zero rotation.  
	targetQuat.makeRotate(90, 0, 1, 0);			// rotation 90 degrees around y-axis.  
				  
	// we define the camer's start and end-position here:  
	startPos.set(0,0,0);  
	targetPos.set(0,0,600);  
  
  
	ofQuaternion tweenedCameraQuaternion;	// this will be the camera's new rotation.  
	  
	// calculate the interpolated orientation  
	tweenedCameraQuaternion.slerp(tweenvalue, startQuat, targetQuat);  
	  
	ofVec3f lerpPos;					//this will hold our tweened position.  
  
	// calculate the interpolated values.  
	lerpPos.x = ofLerp(tweenvalue, startPos.x, targetPos.x);  
	lerpPos.y = ofLerp(tweenvalue, startPos.y, targetPos.y);  
	lerpPos.z = ofLerp(tweenvalue, startPos.z, targetPos.z);  
	  
	// alternative way to calculate interpolated values:  
	// lerpPos = startPos + ((targetPos-startPos) * tweenvalue);  
	  
	// now update the camera with the calculated orientation and position.  
	myCam.setOrientation(tweenedCameraQuaternion);  
	myCam.setGlobalPosition(lerpPos);  
  

Note that we use lerp to tween positions and *slerp* for rotations.

“Lerp” is short for linear interpolation, in which a value is scaled linearly, a bit like the in the classic formula y = k*x . If you’d plot the interpolation function as a graph, you’d get a straight line.

“Slerp” on the other hand, is short for ‘spherical linear interpolation’, meaning the quaternion is interpolated along a great circle so that the interpolation graph is following the shortest path between two points on a sphere…

Cheers
#

Tim

3 Likes

thank you Tim, this is perfect!

i found the way the rotate works confusing but understand it now
targetQuat.makeRotate(90, 0, 1, 0);

best
Ben

Hello, you can try to use my ofxFEngine3DCore addon, it includes ofxFEngineFreeCamera class which allows you to simply bind buttons for your camera movements like you would do it in a game. Code may also give you a hint on how to control ofCamera for your project. Good luck.

Cheers
Matas

this do the thing

thanks matas.

  
if(args.key=='a')  
    {  
        speed = speedin;  
        ofCamera::truck(-speed);  
  
    }  
  
    if(args.key=='d')  
    {  
        speed = speedin;  
        ofCamera::truck(speed);  
  
    }  
  
    if(args.key=='w')  
    {  
        speed = speedin;  
        ofCamera::boom(speed);  
    }  
  
    if(args.key=='s')  
    {  
        speed = speedin;  
        ofCamera::boom(-speed);  
    }  

1 Like

Spotted a typo here should be >

Cheers,
Dave