# Problem with scrolling and interpolation.

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

## Recommended Posts

Hello, this is my first post but i've been reading this forum for a long time, and ussually i find the answers i need searching here, but this time i didn't.

I started several games using SDL and c++, but at the end the code was always a mess so i decided to use OOP, so now i'm writing sort of an 'engine' for my future games (or at least several classes that i can recycle, like inputManager, that kind of thing).

Anyway, I succesfully wrote the input manager, window manager and a player that loads an animates his sprites. I also made an FPS manager, i update the game logic at 25fps, the rendering is not capped, so i need to use interpolation. At this point everything works just fine, including the rendering with interpolation (like this: playerRenderPositionX = playerX + (playerVelocity*interpolation) ).

Now i'm adding scrolling backgrounds, and a camera (centered in the player position). The "set_center" method of my camera class is the same from the LazyFoo's tutorial on scrolling (http://lazyfoo.net/SDL_tutorials/lesson21/index.php). This works, but if i leave the player rendering as it was before, the camera moves "slower" than the player, so i have to render the player taking the camera into consideration.

So i subtract camera.x to the ecuation i said before and...It works fine till the camera moves, if the camera is moving the player image stutters (i don't know if that's the correct expresion, it kind of goes back and forth very fast). If the camera is locked (as in reaching the level border) it works fine.

Now i have this: playerRenderPositionX = playerX + (playerVelocity*interpolation) - camera.x;

If i don't use interpolation the animation is ugly when the camera is locked, but when it is moving it's smooth.

So the question is: How should i use cameras, scrolling backgrounds and interpolation togheter?

PD.: Sorry if i didn't make myself clear, i'm from Argentina so maybe my english is not so good. I will paste some code here trying to translate it because it's in spanish.

Game Loop:

while(gameRunning)
{
fps.resetLoop(); //loop=0, if it reaches the frameskip limit, go to render
while (SDL_GetTicks() > fps.nextLogicalTick()) //25fps
{
do
{
if (e.type ==SDL_QUIT)
{
gameRunning=false;
}
window.update(&e); //windows events
input.update(&e); //input
p.update(&input); //player
fps.updateNextTickAndLoop();
}while(SDL_PollEvent(&e) && fps.frameskipOK());

}
fps.updateInterpolation(SDL_GetTicks());
cam.setCamera(&p);

//RENDER
apply_surface(0,0,background,window.screen(), cam.cameraRect());
p.draw(window.screen(), fps.interpolation(), cam.cameraRect());
SDL_Flip(window.screen());



PLAYER:

void player::draw(SDL_Surface* destino, float interpolation, SDL_Rect* cam)
{
//This part is for clipping the sprite and animating, i think it's not important in this problem

SDL_Rect clip;
clip.x=(this->clipeo_[0])*(this->indiceSprite_); //arranca en 0 y avanza de a "clipeo_[0]"
clip.y=this->clipeo_[1]*(this->estado_); //arranca en 0 para el primer estado y se mueve segun clipeo[1]
clip.h=this->clipeo_[0]; //la altura del sprite
clip.w=this->clipeo_[1];//el ancho del sprite

if(this->indiceSprite_<2 && cicloActualAnimacion_==delayEntreAnimaciones_-1) //ponele que 3
{
this->indiceSprite_++;
this->cicloActualAnimacion_=0;
}
else if(cicloActualAnimacion_ < delayEntreAnimaciones_-1)
cicloActualAnimacion_++;
else
{
this->indiceSprite_=0;
this->cicloActualAnimacion_=0;
}
//Heres the drawing:

if(cam==NULL)
apply_surface( x_+(velx_*interpolation), y_+(vely_*interpolation), this->loadedImage, destino, &clip);
else
apply_surface( (x_+(velx_*interpolation))- cam->x, (y_+(vely_*interpolation)) - cam->y, this->loadedImage, destino, &clip);

}


CAMERA:

void camara::setCamera(player* p)
{
camara_->x = (( p->x() + (p->w() / 2) ) - (this->screenW_) / 2);
camara_->y = (( p->y() + (p->h() / 2) ) - (this->screenH_) / 2);

if( camara_->x < 0 )
{
camara_->x = 0;
}
if( camara_->y < 0 )
{
camara_->y = 0;
}
if( (camara_->x) > (this->levelW_ - camara_->w) )
{
(camara_->x) = (this->levelW_ - camara_->w);
}
if( (camara_->y) > (this->levelH_ - camara_->h) )
{
(camara_->y) = (this->levelH_ - camara_->h);
}
}

##### Share on other sites

Well, i found the solution to my problem, i'll write it here in case someone has the same problem.

if the camera is moving the player image stutters (i don't know if that's the correct expresion, it kind of goes back and forth very fast). If the camera is locked (as in reaching the level border) it works fine.
Now i have this: playerRenderPositionX = playerX + (playerVelocity*interpolation) - camera.x;
If i don't use interpolation the animation is ugly when the camera is locked, but when it is moving it's smooth.

The thing is i was "interpolating" the player's velocity every frame, but not the camera, that was causing the "stuttering". The camera is being centered around the players position, so when the camera is moving, it moves at the exact same velocity as the player. Taking that into consideration, the new ecuation is (for the x position, same for y):

(x_+(velx_*interpolation)) - (cam->x+(velx_*interpolation) = (x+velx_)-cam->x

So that's why without interpolation and with the camera moving it was working fine.

So the solution is: I need to interpolate only when the camera is locked. So i added a boolean in the camera class to tell if it was moving and use it in the animation code. voila.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 28
• 16
• 10
• 10
• 11
• ### Forum Statistics

• Total Topics
634111
• Total Posts
3015573
×