Jump to content
  • Advertisement
Sign in to follow this  
newera

Camera woes

This topic is 4141 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!