Very odd problem with collision detection

Started by
4 comments, last by FantasyVII 11 years, 8 months ago
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)
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.
Advertisement
Hello

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

Isn'it what you're trying to do ?

Hello

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

Isn'it what you're trying to do ?


sir you are a genius :D.

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

thank you so much

[quote name='Tournicoti' timestamp='1343741882' post='4964854']
Hello

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

Isn'it what you're trying to do ?


sir you are a genius biggrin.png.

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

thank you so much
[/quote]

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."

New game in progress: Project SeedWorld

My development blog: Electronic Meteor

Glad I can help you rolleyes.gif

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
[/quote]
Even without 'break', it would work. It's just because we now know there is collision so we can skip the enumeration

[quote name='FantasyVII' timestamp='1343744145' post='4964863']
[quote name='Tournicoti' timestamp='1343741882' post='4964854']
Hello

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

Isn'it what you're trying to do ?


sir you are a genius biggrin.png.

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

thank you so much
[/quote]

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."
[/quote]

Glad I can help you
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 smile.png

This topic is closed to new replies.

Advertisement