Sign in to follow this  

Collision Detection

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

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 this post


Link to post
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 this post


Link to post
Share on other sites

This topic is 4845 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this