Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


Stopping movement when collision occurs

This topic is 5455 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post

Link to post
Share on other sites
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.

Share this post

Link to post
Share on other sites
You should do something like this:

if( upkeypressed() )
SavedPos = Player.pos;
if( IsColliding() )
Player.pos = SavedPos;

Hope that helps

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!