Jump to content

  • Log In with Google      Sign In   
  • Create Account

Second entity moves really slow!


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Kain5056   Members   -  Reputation: 456

Like
0Likes
Like

Posted 06 February 2013 - 02:30 AM

Hello everybody.

I'm trying to learn how to make a simple game with SDL using mainly LazyFoo's tutorials, and I have a problem I have know idea how to solve.

 

When I move the player around with nothing else on the screen except the tiles, the player moves fine.

But when I add another entity ( an enemy ), the ntity that calls its moving function last moves really slowly.

So, if I call the player's moving function first, the enemy moves slowly, and if I call the enemy's moving function first, the player moves slowly.

 

This is the code:

 

Player movement:

 

void player::control()
{
    if( event.type == SDL_KEYDOWN )
    {
        switch( event.key.keysym.sym )
        {
            case SDLK_d:
            xvel += VELOCITY;
            break;

            case SDLK_a:
            xvel -= VELOCITY;
            break;

            default: {}
        }
    }

    if( event.type == SDL_KEYUP )
    {
        switch( event.key.keysym.sym )
        {
            case SDLK_d:
            xvel -= VELOCITY;
            break;

            case SDLK_a:
            xvel += VELOCITY;
            break;

            default: {}
        }
    }
}

void player::jump()
{
    Uint8 * keystate = SDL_GetKeyState( NULL );

    if( keystate[ SDLK_SPACE ] && jumping == false )
    {
        jumping = true;
        yvel = -( VELOCITY * 3 );
    }
}

void player::move( Uint32 delta_ticks , tile * tiles[] )
{
    double lastx = box.x;
    double lasty = box.y;

    box.x += xvel * ( delta_ticks / 1000.f );

    if( box.x < 0 ) box.x = 0;
    if( box.x + PLAYER_WIDTH > LEVEL_WIDTH ) box.x = LEVEL_WIDTH - PLAYER_WIDTH;

    if( collision( box , tiles ) ) box.x = lastx;

    box.y += yvel * ( delta_ticks / 1000.f );

    if( collision( box , tiles ) )
    {
        if( yvel >= 0 )
        {
            box.y = lasty;
            jumping = false;
        }
        else
        {
            box.y = lasty;
            jumping = true;
        }

        yvel = 0;
    }
    else jumping = true;

    yvel += gravity;

    if( yvel >= VELOCITY * 8 ) yvel = VELOCITY * 8;

    if( box.y > LEVEL_HEIGHT + 50 )
    {
        box.y = 0;
        box.x = 50;
        status = FACING_RIGHT;
        yvel = 0;
    }

    x = box.x;
    y = box.y;
}

 

Enemy movement ( the enemy is a completely different class than the player.):

 

void enemy::move_enemy( Uint32 delta_ticks , tile * tiles[] )
{
    double lastx = box.x;
    double lasty = box.y;

    xvel = -VELOCITY;

    box.x += xvel * ( delta_ticks / 1000.f );

    if( collision( box , tiles ) )
    {
        box.x = lastx;
        xvel = -xvel;
    }

    box.y += yvel * ( delta_ticks / 1000.f );

    if( collision( box , tiles ) )
    {
        box.y = lasty;
        yvel = 0;
    }

    yvel += gravity;

    if( yvel >= VELOCITY * 8 ) yvel = VELOCITY * 8;

    x = box.x;
    y = box.y;
}

 

The main function:

 

bool quit = false;
    bool tileedit = false;

    if( init() == false ) return 1;
    if( load_files() == false ) return 2;

    timer delta;
    player player1( 50 , 50 );
    enemy enemy1( 600 , 100 , BUBBLE );
    tile * tiles[ TOTAL_TILES ];

    if( set_tiles( tiles ) == false ) return 4;

    delta.start();

    while( quit == false )
    {
        scrolling_background();
        if( tileedit ) editor_show( current_tile );

        while( SDL_PollEvent( &event ) )
        {
            if( event.type == SDL_KEYDOWN )
            {
                if( event.key.keysym.sym == SDLK_t ) tileedit = !tileedit;
            }

            if( event.type == SDL_QUIT ) quit = true;

            if( tileedit == false ) player1.control();
            else if( tileedit ) editor_control( tiles );
        }

        if( tileedit == false )
        {
            player1.jump();
            scrolling_background();
            player1.move( delta.get_ticks() , tiles );
            enemy1.move_enemy( delta.get_ticks() , tiles );
            player1.player_camera();
        }
        else editor_camera();

        for( int t = 0 ; t < TOTAL_TILES ; t++ )
        {
            tiles[t]->show();
        }

        if( tileedit == false ) player1.show();

        enemy1.show();

        if( SDL_Flip( screen ) == -1 ) return 5;
    }

    editor_save( tiles );
    cleanup( tiles );

    return 0;

 

Could it be that I don't use frame independent movement correctly? I have no idea, and I'm very new to programming in general, so if someone more experienced could help me understand where is the problem, I would really appreciate it.

 

Thank you in advance.


Edited by Kain5056, 06 February 2013 - 02:38 AM.


Sponsor:

#2 Ralara   Members   -  Reputation: 310

Like
5Likes
Like

Posted 06 February 2013 - 03:18 AM

I'm not 100% sure on how your timer works, but I would imagine the get_ticks() function returns how many ticks there have been since the last time it was called?

 

If so, then you should only call get_ticks() once per cycle (at the beginning of the main while loop in your case) and store this in a variable. Then use this value for both the player and the enemy.

 

As you are calling get_ticks() for each entity, there are less ticks happening between the call for the first entity and the second entity than there are between the second and the first, because of all the other stuff happening in between,

 

Let me know if this works or not!



#3 Kain5056   Members   -  Reputation: 456

Like
1Likes
Like

Posted 06 February 2013 - 03:40 AM

It works perfectly! Thank you so much for the help! smile.png

 

Here is the new game loop:

 

while( quit == false )
    {
        Uint32 ticks = delta.get_ticks();

        scrolling_background();
        if( tileedit ) editor_show( current_tile );

        while( SDL_PollEvent( &event ) )
        {
            if( event.type == SDL_KEYDOWN )
            {
                if( event.key.keysym.sym == SDLK_t ) tileedit = !tileedit;
            }

            if( event.type == SDL_QUIT ) quit = true;

            if( tileedit == false ) player1.control();
            else if( tileedit ) editor_control( tiles );
        }

        if( tileedit == false )
        {
            player1.jump();
            scrolling_background();
            player1.move( ticks , tiles );
            enemy1.move_enemy( ticks , tiles );
            player1.player_camera();
        }
        else editor_camera();

        for( int t = 0 ; t < TOTAL_TILES ; t++ )
        {
            tiles[t]->show();
        }

        if( tileedit == false ) player1.show();

        enemy1.show();

        if( SDL_Flip( screen ) == -1 ) return 5;
    }

 

 

Thanks again. smile.png






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS