• Advertisement
Sign in to follow this  

Collision Problem

This topic is 4656 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.getX() >= players[j].getX()){
					diffx = players.getX() - players[j].getX();
					boomx = players[j].getX() + diffx;
					diffx = players[j].getX() - players.getX();
					boomx = players.getX() + diffx;
				if(players.getY() >= players[j].getY()){
					diffy = players.getY() - players[j].getY();
					boomy = players[j].getY() + diffy;
					diffy = players[j].getY() - players.getY();
					boomy = players.getY() + diffy;
				HBITMAP boom = (HBITMAP)LoadImage(0,"boom.bmp",IMAGE_BITMAP,50,50,LR_LOADFROMFILE);
				HDC idc = CreateCompatibleDC(hdc);

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.getX() - players[j].getX();
int diffy = players.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
Sign in to follow this  

  • Advertisement