# Sticky colision detection...?

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

## 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 on other sites

What is map? is it an array of bools (telling what?)?

##### Share on other sites
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 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 on other sites
Nevermind, that was rediculously easy :)

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 9
• 9
• 9
• 14
• 12
• ### Forum Statistics

• Total Topics
633299
• Total Posts
3011263
• ### Who's Online (See full list)

There are no registered users currently online

×