LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C++/SDL: Images moving too slowly. Images loaded once, etc. Why? (https://www.linuxquestions.org/questions/programming-9/c-sdl-images-moving-too-slowly-images-loaded-once-etc-why-556659/)

RHLinuxGUY 05-25-2007 11:46 AM

C++/SDL: Images moving too slowly. Images loaded once, etc. Why?
 
I am looking at games on the Super Nintendo, NES and etc., and I noticed how smooth their backgrounds and other objects move across the screen. I'm trying to recreate that after a couple unsuccessful attempts. I have made a small program that just moves a stick man across the screen everywhere, except where some blocks are at.

http://www.yousendit.com/transfer.ph...E52A1E3A0EF883

Just execute sc_game and you can see what I mean.

I am trying to get speed variation in my objects. So far, to get that smoothness I see in 2D games, moving images one pixel at a time across the screen using a specific time frame for each image sounds like the best way to achieve this.

The problem with the above application is that it does not use a time frame. And strangely, it is running strangely slow. Why?

Am I supposed to move images across the screen by the pixels (instead of one) along with a time frame?

Thanks in advance.

dmail 05-26-2007 08:21 AM

Currently your object "blob" just updates positions using a defined offset
Code:

void scobject::move ()
{
        switch ( move_x )
        {
                case LEFT : xywh.x -= offsetX; break;
                case RIGHT: xywh.x += offsetY; break;
                case STOP :                    break;
...

The simplest method to use is linear interpolation with each frame having a time step.
Using a fixed time step can help in making a game run the same no matter what the machine specs are, where as a variable time step gives differing results.

Code:

struct Object
{
        float x,y;//position
        float vx,vy;//velocity

        update(float time_step)
        {
                //v = u + at
                //position = initial + velocity * time
                x += vx * time_step;
                y += vy * time_step;

        }
};


Here is two links from Glenn Fiedler that you should read:
http://www.gaffer.org/game-physics/fix-your-timestep/
http://www.gaffer.org/game-physics/integration-basics/

RHLinuxGUY 05-26-2007 05:11 PM

Thank you, I am implemented time based movement into my game.


All times are GMT -5. The time now is 05:57 AM.