[solved] - Focus camera on a character

hey,
I creat a video game in 2d and i would like too use a camera,
I want to focus the camera in my character on 2D, but when i draw my character and i use a camera my window don’t display anything.
I draw my character on (0;0;0) and my camera lookAt(0;0;0)
I do wrong ?
thanks you in advance.
Theo

Hi,

Could you try using the lookAt method using both eyePoint and target ? For eg:

mCam.lookAt( vec3( camPos, dist ), vec3( camPos, 0.0 ) );
mCam.setPerspective( 60.0f, aspectRatio, 1.0f, 3.0f * dist );

Bala.

Hi,

when doing 2D games and graphics, it usually is far easier to simply translate and scale, instead of using a camera. Let your hero wander around freely and just shift and scale the world:

void YourApp::update()
{
    mHeroPosition += mHeroVelocity;   // both are ci::vec2
    mEnemyPosition += mEnemyVelocity; // both are ci::vec2
}

void YourApp::draw()
{
    // store the current model matrix, it will automatically get restored
    gl::ScopedModelMatrix scpModel; 

    // move the world so our hero is always in the center
    // (this will alter the model matrix)
    gl::translate( getWindowCenter() );
    gl::scale( vec2( mZoom ) ); // zoom is a float, defaults to 1
    gl::translate( -mHeroPosition ); // also try mEnemyPosition :)

    // draw our actors
    drawActor( mHeroTexture, mHeroPosition );
    drawActor( mEnemyTexture, mEnemyPosition );

    // scpModel will go out of scope and the model matrix 
    // will be restored
}

void YourApp::drawActor( const ci::gl::Texture2dRef &tex, 
                         const ci::vec2 &pos )
{
    gl::ScopedModelMatrix scpModel;
    gl::translate( pos );
    gl::draw( tex );
}

-Paul

2 Likes

Thanks Bala, that what i’ve aleready try to do.

Thanks paul, i will try with your technical it looks nice.
i’ll keep you informed.

Here is the result,

51332175_415075042367549_1748199054094893056_n

the problem is that the local landmark of our character is on the top left of this one. How to make it at its center (0,0,0)?
thanks

Lots of different ways. One potential way:

void YourApp::drawActor( const ci::gl::Texture2dRef &tex, 
                         const ci::vec2 &pos )
{
    gl::ScopedModelMatrix scpModel;
    gl::translate( pos - ci::vec2 ( tex->getSize() / 2 ) );
    gl::draw( tex );
}

Though it would be best to pass the centred position to drawActor() rather than modifying it internally, just for flexibility reasons.

1 Like

thanks so much to all of you for your help. That works !