Sign in to follow this  
newera

Camera woes

Recommended Posts

newera    157
Hello all, I am having some trouble with my 2D camera motion. I have the case of the jitters. The problem lies with the frame time, some code would explain it better:
/** Update the camera to another location if needed */
void Camera::Update(float dt)
{
    Vector2f nextPosition;
    if ( !m_pathToFollow.empty() )
    {
        nextPosition = m_pathToFollow.back();
        if ( nextPosition.Equals(m_position, 0.1f) )
        {
            m_pathToFollow.pop_back();
        }
    }
    else
    {
        nextPosition = m_dest;  // will automatically go to the preset followed entity
    }

    m_velocity.Set( nextPosition - m_position );
    m_velocity.Normalize();
        
    // LOOK HERE:: m_friction is a scalar, and dt is the FrameTime
    m_position = m_position + ( m_velocity * m_baseVel ) * m_friction * dt;
 
  
    if ( m_cameraShake )
    {
        int vertdir = rand()/10000;
        int hortdir = rand()/10000;

        if ( vertdir >= 2 )
            vertdir = -1;

        if ( hortdir >= 2 )
            hortdir = -1;

        Vector2f shakeVelocity( vertdir, hortdir );
        m_position = (m_position + (shakeVelocity * m_shakeIntensity) * dt);
    } 
}
So my question is, how do I make the camera motion frame independent without receiving the 'jitters'. Thanks, Tony S.

Share this post


Link to post
Share on other sites
newera    157
Silly me, sorry to waste peoples time, after a little thinking I found the solution. Here is the code if anyone is inquiring:


Vector2f destination( nextPosition - m_position );
m_velocity.Set( destination );
m_velocity.Normalize();

Vector2f moveTo( ( m_velocity * m_baseVel ) * m_friction * dt );
if ( moveTo.Length() > destination.Length() )
{
moveTo = destination;
}
m_position = m_position + moveTo;

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this