Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

[SFML 2.1] Kolizja działa, jednak nie można poruszać obiektu

Ostatnio zmodyfikowano 2014-04-10 12:18
Autor Wiadomość
colorgreen19
Temat założony przez niniejszego użytkownika
» 2014-04-08 19:11:47
A to moze inaczej, czy stosujac te petle stalokrokowa w ogole powinienem przekazywac jakikolwiek czas do Game_Update?
P-107921
RazzorFlame
» 2014-04-09 07:30:21
Tak, pętla stałokrokowa tak nie działa, chodzi o wyrównanie prędkości.
P-107943
colorgreen19
Temat założony przez niniejszego użytkownika
» 2014-04-09 12:41:23
Tak nie działą, czyli jak? co powinienem przekazywac go Game_Update( ??? ) ?
P-107946
pekfos
» 2014-04-09 13:00:38
Czas od ostatniej klatki? To najbardziej logiczne i użyteczne.
P-107947
OSA_PL
» 2014-04-09 15:08:48
Jak przekazujesz czas od ostatniej klatki to jest to pętla zmiennokrokowa. Stałokrokowa wygląda mniej więcej tak:
C/C++
const int TIME_STEP = 10; //10ms
int accumulator = 0;

while( true )
{
    accumulator += GetDeltaDime();
   
    //Tu trzeba zabezpieczyć przed "spiralą śmierci"
   
    for(; accumulator > TIME_STEP; accumulator -= TIME_STEP )
    {
        GameUpdate( TIME_STEP );
    }
   
}
 
P-107954
colorgreen19
Temat założony przez niniejszego użytkownika
» 2014-04-09 20:56:36
Dobra, dzięki waszej pomocy i objasnieniu troche tematu kod nieznacznie rozni sie od poprzedniego jednak go podam, bo moze cos w nim jest zle //logic i draw nie potrzebne//
C/C++
Game::Game_GameState Game::Game_Run( sf::RenderWindow * okno )
{
    cout << "\tGame_Run();" << endl;
    Clock clock;
    Time elapsed;
    Vector2i mouse;
    Event event;
    float accumulator = 0;
   
    while( true )
    {
        mouse = Mouse::getPosition( * okno );
       
        while( okno->pollEvent( event ) )
        {
            if( event.type == sf::Event::Closed )
            {
                return Exit;
            }
            else if( event.type == Event::KeyPressed && event.key.code == Keyboard::Space )
            {
                if( archer->GetAllowOnJump() )
                {
                    archer->SetJump( true );
                }
            }
        }
        accumulator += elapsed.asSeconds();
       
        Game_Input();
       
        if( accumulator >= 0.02f )
        {
            Game_Logic();
           
            Game_Collision();
           
            Game_Update( seconds( 0.02f ) );
           
            accumulator -= 0.02f;
        }
       
        Game_Draw( okno );
        //cout<<"\t\tFPS: "<<1/ elapsed.asSeconds() <<endl;
        elapsed = clock.restart();
    }
}
///////////INPUT/////////////////////////////////////////////////////
void Game::Game_Input()
{
    if( Keyboard::isKeyPressed( Keyboard::Left ) )
    {
        if( archer->GetAllowToGoLeft() )
        {
            archer->SetVelocity( - 250, archer->GetVelocity().y );
        }
        else archer->SetVelocity( 0, archer->GetVelocity().y );
       
    }
    else if( Keyboard::isKeyPressed( Keyboard::Right ) )
    {
        if( archer->GetAllowToGoRight() )
        {
            archer->SetVelocity( 250, archer->GetVelocity().y );
        }
        else archer->SetVelocity( 0, archer->GetVelocity().y );
       
    }
    else archer->SetVelocity( 0, archer->GetVelocity().y );
   
}
///////////COLISSION/////////////////////////////////////////////////////
void Game::Game_Collision()
{
    archer->SetGravity( true );
    archer->SetAllowOnJump( false );
    archer->SetAllowToGoLeft( true );
    archer->SetAllowToGoRight( true );
   
    for( int i = 0; i < mapa.collisionMap.size(); i++ )
    {
        for( int j = 0; j < mapa.collisionMap[ i ].size(); j++ )
        {
            if( mapa.collisionMap[ i ][ j ] != 0 )
            {
                int bottom = i * 32 + 32, top = i * 32, left = j * 32, right = j * 32 + 32;
                Rect < int > rect( left, top, 32, 32 );
               
                archer->Collision( rect );
            }
        }
    }
}
/////////UPDATE//////////////////////////////////////////////////
void Game::Game_Update( sf::Time elapsed )
{
    archer->Update( elapsed );
}
i kolicje z player.cpp  //dobrze sa rozpoznawane
C/C++
bool Player::Collision( sf::Rect < int > rect )
{
    if( GetBounds().intersects( static_cast < FloatRect >( rect ) ) )
    {
        if( CollisionOnTop( rect ) )
        {
            SetGravity( false );
            SetVelocity( GetVelocity().x, 0 );
            SetAllowOnJump( true );
            //SetPosition( GetPosition().x, rect.top - GetSize().y );
        }
        else if( CollisionOnBottom( rect ) )
        {
            SetVelocity( GetVelocity().x, 0 );
            SetPosition( GetPosition().x, rect.top + rect.height );
        }
        else if( CollisionOnLeft( rect ) )
        {
            SetAllowToGoRight( false );
        }
        else if( CollisionOnRight( rect ) )
        {
            SetAllowToGoLeft( false );
        }
    }
}
i update
C/C++
void Archer::Update( sf::Time elapsed )
{
    if( m_gravity )
    {
        velocity.y += 1000 * elapsed.asSeconds(); ///IN DIFFRENT COMPUTERS PHYSIC trolling ;d   (1000)
    }
    //else velocity.y = 0;
   
    if( GetJump() )
    {
        SetJump( false );
        SetVelocity( GetVelocity().x, - 500 ); ///IF CHANGE UP, There must be update to  (-500)
    }
    Move( elapsed );
}
Na kazdym kompie gra dziala rowno, czyli mam to o co mi chodzilo. Ale mam nadal te bledy ze postac wpada pod plansze, albo wchodzi w boczna krawedz, mimo ze kolizja jest wykrywana. tak jakby petla nie nazdazala albo cos. Jakies pomysły?
  
P-107970
OSA_PL
» 2014-04-09 21:22:05
Spróbuj tak i zobacz czy jest jakaś poprawa.

C/C++
Game::Game_GameState Game::Game_Run( sf::RenderWindow * okno )
{
    cout << "\tGame_Run();" << endl;
    Clock clock;
    Time elapsed;
    Vector2i mouse;
    Event event;
    float accumulator = 0;
   
    while( true )
    {
        mouse = Mouse::getPosition( * okno );
       
        while( okno->pollEvent( event ) )
        {
            if( event.type == sf::Event::Closed )
            {
                return Exit;
            }
            else if( event.type == Event::KeyPressed && event.key.code == Keyboard::Space )
            {
                if( archer->GetAllowOnJump() )
                {
                    archer->SetJump( true );
                }
            }
        }
        accumulator += elapsed.asSeconds();
       
        if( accumulator > 0.04f )
             accumulatot = 0.04f;
       
        Game_Input();
       
        for(; accumulator >= 0.005f; accumulator -= 0.005f )
        {
            Game_Logic();
           
            Game_Collision();
           
            Game_Update( seconds( 0.005f ) );
        }
       
        Game_Draw( okno );
        //cout<<"\t\tFPS: "<<1/ elapsed.asSeconds() <<endl;
        elapsed = clock.restart();
    }
}
C/C++
bool Player::Collision( sf::Rect < int > rect )
{
    if( GetBounds().intersects( static_cast < FloatRect >( rect ) ) )
    {
        if( CollisionOnTop( rect ) )
        {
            SetGravity( false );
            SetVelocity( GetVelocity().x, 0 );
            SetAllowOnJump( true );
            //SetPosition( GetPosition().x, rect.top - GetSize().y );
        }
        else if( CollisionOnBottom( rect ) )
        {
            SetVelocity( GetVelocity().x, 0 );
            SetPosition( GetPosition().x, rect.top + rect.height );
        }
        else if( CollisionOnLeft( rect ) )
        {
            SetAllowToGoRight( false );
            SetVelocity( GetVelocity().y, 0 );
        }
        else if( CollisionOnRight( rect ) )
        {
            SetAllowToGoLeft( false );
            SetVelocity( GetVelocity().y, 0 );
        }
    }
}

P-107971
colorgreen19
Temat założony przez niniejszego użytkownika
» 2014-04-10 12:18:40
pierwszy kod wykorzystałem tylko zamias 0.005f musialem dac 0.004f. Jeszcze są na mapie dziwne miejsca gdzie czasami postac wpada w mape ale dając ograniczenie prędkości spadania i inne pierdołki jakoś to niweluje. Dzieki bardzo z pomoc
P-107991
1 « 2 »
Poprzednia strona Strona 2 z 2