• Create Account

## Very odd problem with collision detection

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

5 replies to this topic

### #1FantasyVII  Members

1005
Like
0Likes
Like

Posted 31 July 2012 - 05:44 AM

Hello everyone,

I'm making a 2D Tower defense game.

I have two maps.

The first map draw all the tiles in my game and each tile is 32px.
The second map stores the positions of tiles that I don't want the tower to be build on (in another word tiles that are collidable).

first I'm converting the mouse position into tile position which mean the mouse can only move every 32px not ever 1px.

for example if you move the mouse on the X-axis it will look like this X=32, X=64, X=96 etc... same goes for the Y-axis.

then I have an if statment that check if the player mouse position is equal to the collidable tiles which are stores in a list. so if its true I set a bool value call Collision to true.

like so

MousePosition.X = (int)MousePos.X / 32;
MousePosition.Y = (int)MousePos.Y / 32;

// for example if MousePosition which is (128, 64) == Map.CollisionPosition[0] which is (128, 64) collision = true
if (MousePosition * 32 == Map.CollisionPosition[0])
collision = true;
else
collision = false;


no I have 66 tiles to check for. So natuarlly I did a for loop to check if the mouse position is equal to the collidable tile position like to.

for(int i = 0; i< Map.CollisionPosition.Count; i++)
if (MousePosition * 32 == Map.CollisionPosition[i])
collision = true;
else
collision = false;


but that didn't work at all. it only check the last two tiles position in the list and sometimes it work and sometimes it doesn't.

Now I found a solution but its not good at all. If I wanted this to work I had to do an if statement to check for each tile. So I would do 65 if statement to check if collision occur or not !!!

if (MousePosition * 32 == Map.CollisionPosition[0])
collision = true;
else if (MousePosition * 32 == Map.CollisionPosition[1])
collision = true;
else if (MousePosition * 32 == Map.CollisionPosition[2])
collision = true;
............................................
else if (MousePosition * 32 == Map.CollisionPosition[65])
collision = true;
else
collision = false;


Map.CollisionPosition is public static in Map class

so my question is why does this work when I do 65 if statements and doesn't when I try to use a for loop. FYI this is all done in the Update method of the game.

Edited by FantasyVII, 31 July 2012 - 05:47 AM.

### #2Tournicoti  Prime Members

704
Like
2Likes
Like

Posted 31 July 2012 - 07:38 AM

Hello
collision=false;
for(int i = 0; i< Map.CollisionPosition.Count; i++)
if (MousePosition * 32 == Map.CollisionPosition[i])
{
collision = true;
break;
}

Isn'it what you're trying to do ?

Edited by Tournicoti, 31 July 2012 - 07:40 AM.

### #3FantasyVII  Members

1005
Like
0Likes
Like

Posted 31 July 2012 - 08:15 AM

Hello

collision=false;
for(int i = 0; i< Map.CollisionPosition.Count; i++)
if (MousePosition * 32 == Map.CollisionPosition[i])
{
collision = true;
break;
}

Isn'it what you're trying to do ?

sir you are a genius .

just one last question. can you tell me why my for loop didn't work and yours did?

thank you so much

### #4CC Ricers  Members

1491
Like
2Likes
Like

Posted 31 July 2012 - 08:22 AM

Hello

collision=false;
for(int i = 0; i< Map.CollisionPosition.Count; i++)
if (MousePosition * 32 == Map.CollisionPosition[i])
{
collision = true;
break;
}

Isn'it what you're trying to do ?

sir you are a genius .

just one last question. can you tell me why my for loop didn't work and yours did?

thank you so much

The second loop works because of the "break" statement. In your loop, you kept resetting collision to false when it found the next tile to check against. It will not return true unless the collision happened in the very last tile. The break statement tells the program to exit the loop, basically saying "we found a collision, looping further isn't necessary."

Edited by CC Ricers, 31 July 2012 - 08:23 AM.

New game in progress: Project SeedWorld

My development blog: Electronic Meteor

### #5Tournicoti  Prime Members

704
Like
2Likes
Like

Posted 31 July 2012 - 08:29 AM

My idea is "I assume at first there is no collision, and then if I find any positive case in the loop, there is collision (and I can exit now the loop)"
The problem is that you set collision to false in your loop when you encounter a negative case, but it must be set to false once before the loop only.

The second loop works because of the "break" statement

Even without 'break', it would work. It's just because we now know there is collision so we can skip the enumeration

Edited by Tournicoti, 31 July 2012 - 08:31 AM.

### #6FantasyVII  Members

1005
Like
0Likes
Like

Posted 31 July 2012 - 08:30 AM

Hello

collision=false;
for(int i = 0; i< Map.CollisionPosition.Count; i++)
if (MousePosition * 32 == Map.CollisionPosition[i])
{
collision = true;
break;
}

Isn'it what you're trying to do ?

sir you are a genius .

just one last question. can you tell me why my for loop didn't work and yours did?

thank you so much

The second loop works because of the "break" statement. In your loop, you kept resetting collision to false when it found the next tile to check against. It will not return true unless the collision happened in the very last tile. The break statement tells the program to exit the loop, basically saying "we found a collision, looping further isn't necessary."

My idea is "I assume at first there is no collision, and then if I find any positive case in the loop, there is collision (and I can exit now the loop)"
The problem is that you set collision to false in your loop when you encounter a negative case, but it must be set to false once before the loop only.

thank you so much guys.

now I understand

Edited by FantasyVII, 31 July 2012 - 08:31 AM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.