I have tried searching on here but hardly any results?
Here is the latest code I've tried:
Code: Select all
void
FollowCamera::Update()
{
if(!cam || !m_pTargetNode) return;
irr::core::vector3df currTargetPos = m_pTargetNode->getPosition();
//if too far away, move camera closer
irr::core::vector3df camToTarg = currTargetPos - cam->getPosition() ;
//leash is only in the X-Z plane, so only count distance using X and Z
irr::core::vector2df xzDist( camToTarg.X, camToTarg.Z );
if(xzDist.getLength() > m_leash) { //need to move closer
//camToTarg = irr::core::vector3df( xzDist.X, 0, xzDist.Y);
//camToTarg = camToTarg.normalize() * m_speed;
irr::core::vector3df camToTargOrig = irr::core::vector3df( xzDist.X, 0, xzDist.Y);
camToTarg = camToTargOrig.normalize() * (camToTargOrig.getLength() - m_leash);
//set X-Z position
cam->setPosition( cam->getPosition() + camToTarg ); //move closer
cam->updateAbsolutePosition();
//set Y position
irr::f32 h_tolerance = m_height / 10; //the ammount of leway given to how close we need to be to the right height
irr::f32 h_delta = cam->getPosition().Y - currTargetPos.Y + m_height; //distance from prefered height position
if( cam->getPosition().Y < ( currTargetPos.Y + m_height) - h_tolerance) {
cam->setPosition( irr::core::vector3df( cam->getPosition().X, cam->getPosition().Y + h_delta/2, cam->getPosition().Z) );
cam->updateAbsolutePosition();
}else if( cam->getPosition().Y > ( currTargetPos.Y + m_height) + h_tolerance ) {
cam->setPosition( irr::core::vector3df( cam->getPosition().X, cam->getPosition().Y - h_delta/2, cam->getPosition().Z) );
cam->updateAbsolutePosition();
}
}
cam->setTarget( currTargetPos ); //look at Target position
lastTargetPos = currTargetPos;
}