Jump to content
  • Advertisement
Sign in to follow this  
Thrust

Sticky colision detection...?

This topic is 4886 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

Hey, for the first time I actuially figured out colision detection on my own... WOOHOO! For some reason though if im touching a wall that I cant walk through I can move in any direction but the opposite of the blocking wall. For example if im all the way down I can go I cant move left or right, just up. I dont get it.
      if ( keys[SDLK_UP] )
       {
              if (map[(ypos/32)][(xpos/32)] == 1 && map[(ypos/32)][((xpos+spritew)/32)] == 1)
              {
              ypos-=2;
              }
       }
       if ( keys[SDLK_DOWN] ) 
       {
              if (map[((ypos+spriteh)/32)][(xpos/32)] == 1 && map[((ypos+40)/32)][((xpos+spritew)/32)] == 1  )
              {
              ypos+=2;
              }
       }
       if ( keys[SDLK_LEFT] ) 
       {
              if (map[(ypos/32)][((xpos)/32)] == 1 && map[((ypos+spriteh)/32)][((xpos)/32)] == 1 && map[((ypos+(spriteh/2))/32)][((xpos)/32)] == 1)
              {
              xpos-=2;
              }
       }
       if ( keys[SDLK_RIGHT] )       
       {
              if (map[(ypos/32)][((xpos+spritew)/32)] == 1 && map[((ypos+spriteh)/32)][((xpos+spritew)/32)] == 1 && map[((ypos+(spriteh/2))/32)][((xpos+spritew)/32)] == 1)
              {
              xpos+=2;
              }
       }


Share this post


Link to post
Share on other sites
Advertisement
Just get the position and velocity of the moving object and calculate if its next move its going to hit the wall, if it does, stop it, or make its velocity the opposite way, so u bounce off

Share this post


Link to post
Share on other sites
The map is an int of numbers, one is walkable, 0 isnt.




int map [20][25]= {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
0,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};




Its global, but yet when I hit the sides I stick, I think its because I move 2 spaces and get stuck inside the tiles, I dont know how to fix it though.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!