Sign in to follow this  
bballmitch

Movement within a boundary

Recommended Posts

For the purpose of this discussion, the boundary is that -100 < x < 100 and -100 < z < 100. So I have my player moving like this:
if( buttonPressed == 6 )  //'a'
     {
          if( player1->getX() < 100 && player1->getX() > -100 && player1->getZ() < 100 && player1->getZ() > -100 )
           {
               player1->addX( player1->getSpeed() * sin(view_angle) );
               player1->addZ( (-1)*player1->getSpeed() * cos(view_angle) );
           }
           else
           {
               player1->addX( 5 * (-1)*player1->getSpeed() * sin(view_angle) );
               player1->addZ( 5 * player1->getSpeed() * cos(view_angle) );
           }
     }
     else if( buttonPressed == 7 )  //'d'
     {
          if( player1->getX() < 100 && player1->getX() > -100 && player1->getZ() < 100 && player1->getZ() > -100 )
           {
               player1->addX( (-1)*player1->getSpeed() * sin(view_angle) );
               player1->addZ( player1->getSpeed() * cos(view_angle) );
           }
           else
           {
               player1->addX( 5 * player1->getSpeed() * sin(view_angle) );
               player1->addZ( 5 * (-1)*player1->getSpeed() * cos(view_angle) );
           }
     }
     else if( buttonPressed == 8 )   //'s'
     {
          if( player1->getX() < 100 && player1->getX() > -100 && player1->getZ() < 100 && player1->getZ() > -100 )
           {
               player1->addX( player1->getSpeed() * cos(view_angle) );
               player1->addZ( player1->getSpeed() * sin(view_angle) );
           }
           else
           {
               player1->addX( 5 * (-1)*player1->getSpeed() * cos(view_angle) );
               player1->addZ( 5 * (-1)*player1->getSpeed() * sin(view_angle) );
           }
     }
     else if( buttonPressed == 9 )    //'w'
     {
          if( player1->getX() < 100 && player1->getX() > -100 && player1->getZ() < 100 && player1->getZ() > -100 )
           {
               player1->addX( (-1)*player1->getSpeed() * cos(view_angle) );
               player1->addZ( (-1)*player1->getSpeed() * sin(view_angle) );
           }
           else
           {
               player1->addX( 5 * player1->getSpeed() * cos(view_angle) );
               player1->addZ( 5 * player1->getSpeed() * sin(view_angle) );
           }
     }

The problem lies when I go to the end of my boundary. At first, the boundary holds its own, but if I try to get through the boundary (lets say by holding down 'w' so I keep going foreward) eventually everything goes backwards. So when I press 'w', it does what 's' is supposed to do and vice versa. I can't figure out why. It seems like my code should work... BTW, if you see a better method of accomplishing what I'm trying to do, please share. :)

Share this post


Link to post
Share on other sites
So you're trying to constraint the player to within the boundary?
I would use a different approach. First determine the velocity of the player, without regard to the boundary. Then move the player, and check if the new location is outside the boundary. If so, clamp the offending coordinate.

In 1D:


if key == 'w'
v = 1.0
elsif key == 's'
v = -1.0

p += v
p = max(min(p, 100), -100)


Hope this helps,
Xavier

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