It is currently Wed Sep 08, 2010 6:15 am

All times are UTC - 5 hours [ DST ]




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: tiled movement
PostPosted: Tue Jul 13, 2010 3:34 pm 
Joined: Thu Apr 01, 2010 4:25 am
Posts: 8

hello i need a little help with moving a character in a tiled system, my character should move 50 pixels after i press right button, it does but sometimes it moves all the time and i cant stop it sometimes it moves 53 pixels..

ok my Update function:

Code:
void Player::Update( float deltaTime )
{
   switch( m_playerState )
   {
   case PLAYER_IDLE:
      break;

   case PLAYER_WALK:
      position_x += ( velocity_x * deltaTime );
      position_y += ( velocity_y * deltaTime );

      break;
   }
}


a think function:

Code:
void Player::Think( void )
{
   if( std::abs( position_x - destination_x ) < 0.001f && std::abs( position_y - destination_y ) < 0.001f )
   {
      m_playerState = PLAYER_IDLE;
   }

}


OnWalk function:

Code:
void Player::Update( float deltaTime )
{
   switch( m_playerState )
   {
   case PLAYER_IDLE:
      break;

   case PLAYER_WALK:
      position_x += ( velocity_x * deltaTime );
      position_y += ( velocity_y * deltaTime );

      break;
   }
}

void Player::InputEvents( void )
{
   switch( m_playerState )
   {
   case PLAYER_IDLE:
      switch( Globals::event.key.keysym.sym )
      {
      case SDLK_UP:
         OnWalk( NORTH );
         break;

      case SDLK_DOWN:
         OnWalk( SOUTH );
         break;

      case SDLK_RIGHT:
         OnWalk( EAST );
         break;

      case SDLK_LEFT:
         OnWalk( WEST );
         break;
      }

   case PLAYER_WALK:
      break;
   }
}

void Player::Think( void )
{
   if( std::abs( position_x - destination_x ) < 0.001f && std::abs( position_y - destination_y ) < 0.001f )
   {
      m_playerState = PLAYER_IDLE;
   }

}

void Player::Draw( void )
{
   m_sprite->Draw( static_cast<int>( position_x ), static_cast<int>( position_y ) );
}

bool Player::OnWalk( direction_t direction )
{
   if( PlayerIsStanding() )
   {
      m_playerState = PLAYER_WALK;

      switch( direction )
      {
      case NORTH:
         destination_x = position_x;
         destination_y = position_y - 50.0f;

         velocity_x = 0;
         velocity_y = -m_movementSpeed;
         break;

      case SOUTH:
         destination_x = position_x;
         destination_y = position_y + 50.0f;

         velocity_x = 0;
         velocity_y = m_movementSpeed;
         break;

      case EAST:
         destination_x = position_x + 50.0f;
         destination_y = position_y;

         velocity_x = m_movementSpeed;
         velocity_y = 0;
         break;

      case WEST:
         destination_x = position_x - 50.0f;
         destination_y = position_y;

         velocity_x = -m_movementSpeed;
         velocity_y = 0;
         break;

         return true;
      }
   }

   return false;
}


Offline
 Profile  
 Post subject: Re: tiled movement
PostPosted: Thu Jul 15, 2010 8:28 am 
Site Admin
Joined: Tue Oct 28, 2008 9:55 am
Posts: 185
Location: Indiana

A simple fix would be to know where the entity is trying to go, and then to detect the proximity to that destination. If they are close, then stop all movement, and fixate them at those exact coords. Other than that, you simple have to time the movement right.


Offline
 Profile  
 Post subject: Re: tiled movement
PostPosted: Thu Jul 15, 2010 12:05 pm 
Joined: Thu Apr 01, 2010 4:25 am
Posts: 8

i am going to check it out:

Code:
void Player::Think( void )
{
   switch( m_playerState )
   {
   case PLAYER_IDLE:
      break;

   case PLAYER_WALK:
      /*if( std::abs( position_x - destination_x ) <= 0.001f && std::abs( position_y - destination_y ) <= 0.001f )
      {
         velocity_x = 0;
         velocity_y = 0;
         m_playerState = PLAYER_IDLE;
      }*/

      switch( m_movementDirection )
      {
      case EAST:
         if( position_x >= destination_x )
         {
            position_x = destination_x;
            position_y = destination_y;
            velocity_x = 0.0f;
            velocity_y = 0.0f;
            m_playerState = PLAYER_IDLE;
         }

         break;
      }

      break;
   }

}


i think that it works and the part with std::abs was somehow bugged or not well written


Offline
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


 Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
 
cron