• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
bolche

Problem with scrolling and interpolation.

1 post in this topic

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);
    }
}

Thanks in advance, if something else is needed please ask for it.

 

0

Share this post


Link to post
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.

 

0

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  
Followers 0