Skip to main content
gamedev.net
🔒 Locked

Camera woes

Started by newera Mar 20, 2007 at 3:33 PM 0 replies 600+ views
Original Post
newera
newera
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.
newera
newera
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;

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.