Problem animating camera glm::lerp

Hi all,

I was trying to animate an ofEasyCam using the code proposed by @roymacdonald in the following post. The only difference is that I have set the start position and end position according to the actual position of the camera, so once triggering the startAnimation function the camera jumps to the startposition and the animation begins. However I have got an error with the lerp interpolation function (which is present even without my intervention in the code) since the function expect a glm::quat type and not glm::vec3, which is the type of the positions. I have tried using glm::slerp , which accept the glm::vec3 type but the animation does not work. I am missing something and the most probably thing is my lack of understanding of what a quat really is or what represents. The simplifying version of the code:

ofApp.h

  ofEasyCam cam;

  glm::vec3 startPosition, endPosition;
  bool bTweening = false;
  uint64_t startTime, tweenDuration;

ofApp.cpp

void ofApp::draw(){
	if(bTweening){
		auto currentTime =ofGetElapsedTimeMillis();
		if(currentTime > startTime + tweenDuration){
			bTweening = false;
			cam.enableMouseInput();
		}else{
			float t = ofMap(currentTime, startTime, startTime+tweenDuration, 0, 1);
			cam.setPosition(glm::lerp(startPosition, endPosition, t)); //HERE IS THE ERROR
			// the slerp function is in charge of creating the interpolated 
		}
	}
	
	cam.begin();
	
	ofDrawGrid(10,100);
	
	cam.end();

void ofApp::camAnimation(){
  
  //startPosition = cam.getGlobalPosition();

  //	endPosition = {ofRandom(-100,100), ofRandom(-100,100), ofRandom(-100,100)};// just choosing a random new position

  cam.setPosition(startPosition);

  startTime = ofGetElapsedTimeMillis();
	
  tweenDuration = 4000; //this is where you set how much you want the animation to take.

  bTweening = true;

  cam.disableMouseInput();// this is to avoid the mouse interaction while animating	
}





//--------------------------------------------------------------
void ofApp::keyPressed(int key){

  if (key == 's') {

    startPosition = cam.getGlobalPosition();
    std::cout << startPosition << std::endl;
    
      }

  if (key == 'e') {

    endPosition = cam.getGlobalPosition();
    std::cout << endPosition << std::endl;
    
      }

}

//--------------------------------------------------------------
void ofApp::keyReleased(int key){

if (key == 'b') {

  camAnimation();
  
    }
  
}

Thanks in advance

Hey @pemb how about glm::mix() instead of glm::lerp()? Then you don’t have to goof around with quaternions.

thanks, It works perfectly. I will try to set a start and end rotation also, because at the moment it is just position. I reckon that it is with quats. thanks

Hey glad it worked! The rotation can be either Euler angles or a quaternion. I’m awful with quaternions, but they do avoid the gimbal lock that can happen with the Euler angles.