Movement within a boundary

Started by
2 comments, last by Colin Jeanne 17 years, 11 months ago
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. :)
Mitchen Games
Advertisement
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.0elsif key == 's'  v = -1.0p += vp = max(min(p, 100), -100)


Hope this helps,
Xavier
umm...what does p represent? I've never used velocity (or much physics at all) in any of my programs. Now's a good time to start though. Could you put some comments next to your code?
Mitchen Games
p is the position of the object. In the example an object cannot go outside of the range [-100, 100].

This topic is closed to new replies.

Advertisement