Sign in to follow this  

Collision Problem

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

I have 2 players when they collide i have my program remove the players from the vector holding all the objects to render and that works. Before i remove them i have their differences in x and y stored in variables. Then what i do is figure out which ones x is bigger and which ones y is bigger whatever is bigger i store that x + the x difference and that y + the y difference to get a central point between the two. Then i try to blit a yellow bitmap to show explosion and it always blits in the wrong place can someone help me figure out the problem? Here's my source
int diffx;
				int diffy;
				int boomx;
				int boomy;
				if(players[i].getX() >= players[j].getX()){
					diffx = players[i].getX() - players[j].getX();
					boomx = players[j].getX() + diffx;
				}
				else{
					diffx = players[j].getX() - players[i].getX();
					boomx = players[i].getX() + diffx;
				}
				if(players[i].getY() >= players[j].getY()){
					diffy = players[i].getY() - players[j].getY();
					boomy = players[j].getY() + diffy;
				}
				else{
					diffy = players[j].getY() - players[i].getY();
					boomy = players[i].getY() + diffy;
				}
				cmap.removeplayer(i);
				cmap.removeplayer(j-1);
				HBITMAP boom = (HBITMAP)LoadImage(0,"boom.bmp",IMAGE_BITMAP,50,50,LR_LOADFROMFILE);
				HDC idc = CreateCompatibleDC(hdc);
				SelectObject(idc,boom);
				BitBlt(hdc,boomx,boomy,50,50,idc,0,0,SRCCOPY);



note that this is within my display method. I'm positive that didcollide works but i am unsure why this doesnt work. also note that eventually i will be implementing an animation class to do all teh work :-) Thanks for any help you can offer me, -Cory Fisher

Share this post


Link to post
Share on other sites
I believe your method of calculating the centre point is wrong and should be like this:


// Calculate the vector between player i and player j
int diffx = players[i].getX() - players[j].getX();
int diffy = players[i].getY() - players[j].getY();

// Find the midpoint along the vector from player j to player i
// I.e. move half the distance from player j to player i
// Note if you didn't have the divide by 2 you'd end of with the position of
// player i
boomx = players[j].getX() + (diffx /2);
boomy = players[j].getY() + (diffy /2);



It may help to sketch out what this code is doing. Try having player j at (0,0) and player i at (10,10)

Share this post


Link to post
Share on other sites

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