Public Group

# Collision Detection

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

## Recommended Posts

Trying to do collision detection, where there are 12 points, each one a part of a circle. Running into an infinite loop, or at least something that freezes the computer, and need some help.
void aPlayer::Move(double MaxMapX,double MaxMapY) {
//MapX/MapY are variables that describe the location of the player.  In the middle of the circle.
MapX=MapX+XMove;
MapY=MapY+YMove;
//If there is no movement, don't check for collisions
if ((XMove>0 || YMove>0) || (XMove<0 || YMove<0)) {

for (int arrCount=0;arrCount<12;++arrCount) {
//XOffSet/YOffSet is a precalculated number that describes how far away from the center the point is	PlayerPoints[arrCount].MapX=MapX+PlayerPoints[arrCount].XOffSet;
PlayerPoints[arrCount].MapY=MapY+PlayerPoints[arrCount].YOffSet;
if (PlayerPoints[arrCount].MapX<=0) {
MapX=(PlayerPoints[arrCount].XOffSet*-1)+.04;
arrCount=-1;
//Restart the loop, found a collision
} else if (PlayerPoints[arrCount].MapY<=0) {
MapY=(PlayerPoints[arrCount].YOffSet*-1)+.04;
arrCount=-1;
//Restart the loop, found a collision
} else if (PlayerPoints[arrCount].MapX>=MaxMapX) {
MapX=(PlayerPoints[arrCount].XOffSet*-1)+MaxMapX;
arrCount=-1;
} else if (PlayerPoints[arrCount].MapY>=MaxMapY) {
MapY=(PlayerPoints[arrCount].YOffSet*-1)+MaxMapY;
arrCount=-1;
}
}

}

}


I want to set arrCount to -1 so that it basically restarts the loop with a new center value for each coordinate. However, this causes it to freeze, though I don't know why. Shouldn't by changing the MapX/MapY it shouldn't happen again? [EDIT] Could the possible problem be that because the PlayerPoints.MapX is a float, it rounds down the float to become 0 when it checks the if statement?

##### Share on other sites
I suppose there was a mistake in the post when this line was commented out...
PlayerPoints[arrCount].MapX=MapX+PlayerPoints[arrCount].XOffSet;

here's your problem :) (I think)
if (PlayerPoints[arrCount].MapX<=0)

if this line (or any of those if statements) evaluates as true, and you reset arrCount to -1, the statement will be true the next iterarion aswell since you don't change the variable PlayerPoints[arrCount].MapX

##### Share on other sites
Yea, I fixed that, wasn't supposed to look like that.

But er, PlayerPoints[arrCount].MapX does get changed, right in that line of code you quoted.

• 18
• 29
• 11
• 21
• 16