Stopping movement when collision occurs
While creating a 2D game in DirectX, I''ve been having some slight problems with collisions. I have 2 boxes, when they collide, i want one of the boxes (the player box) to not be able to move any farther (like they hit a wall). I''m not having any problems with detecting that there is a collison, and i can also make it so the player box stops moving, but they can''t move any further after.
Example:
//UP key test:
if(upkeypressed())
{
//if no collision, move the player
if(collision == 0)
{
movetheplayer();
}
{
The problem is, once I have detected a collision, it continues to detect that collison and the player is stuck on the wall. Any ideas of how I could not let the player move through the wall, and at the same time not make them stuck on the wall?
Thanks,
Kirko
I had a similar problem recently. If you''re having a problem with sprites getting stuck to eachother with collision, you need to get them "unstuck". You can do this by setting the position of one of the sprites so that it doesn''t overlap with the wall anymore. (youll need to calculate how much overlap there is between the two images or at least their bounding boxes)
Another thing I did to solve this problem was to detect which side of the image was colliding. For example, if my sprite was moving right and its right side collided with a wall, I would simply not allow it to move right anymore until the collision has stopped. The other directions of movement would not be affected by this solution. You could also reverse the vector you''re using for movement and your image would bounce off the wall.
Another thing I did to solve this problem was to detect which side of the image was colliding. For example, if my sprite was moving right and its right side collided with a wall, I would simply not allow it to move right anymore until the collision has stopped. The other directions of movement would not be affected by this solution. You could also reverse the vector you''re using for movement and your image would bounce off the wall.
You should do something like this:
Hope that helps
if( upkeypressed() ){ SavedPos = Player.pos; MoveThePlayer(); if( IsColliding() ) Player.pos = SavedPos;}
Hope that helps
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement