2d collistion detection

Started by
5 comments, last by jsloan 19 years, 7 months ago
i was wondering what the easyest way is to write collition detection in c++ WIN API for just a simple game of pong.
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
Advertisement
Check the X value of the ball to the inner X value of the paddles, then check to see if the Y value is within the location of the paddle. Its not the prettiest method but its about the simplest i can think of
---------------------------------------------think outside the quadLogic GamesI realized I stay on the pc too long when my wireless mouse died after replacing the batteries...twice
see im having some problems witht that maybe imy just not updating the ball position correctly but that method is giving me some problems how would i store the location of the ball (in terms of the window) becuase i would need to store this in a variable to do the check
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
make a class or struct that represents a ball.

class Ball{    public:     Ball(int x,int y){xPos = x; yPos = y;}    ~Ball(){}       int Get_X(){return xPos;}     int Get_Y(){return yPos;}      int Set_X(int x){xPos = x;}     int Set_Y(int y){yPos = y;}    private:     int xPos;     int yPos;};


FTA, my 2D futuristic action MMORPG
This is how i did mine.

But like the previous poster said use a struct\class, and yeah im pretty sure this aint the best way to do it.
bool DDraw::CheckCollision(int iSprite1,int iSprite2){	bool foundX;	//for each X pixel of sprite 2	for (int iX=DDsprite[iSprite2].posX;iX<DDsprite[iSprite2].width;iX++)	{		//loop through all X pixels of sprite 1  and compare		for (int i=DDsprite[iSprite1].posX;i<DDsprite[iSprite1].width;i++)		{			//if match found			if (i == iX)			{				foundX = true;			}		}	}	if (foundX)	{		//for each Y pixel of sprite 2		for (int iY=DDsprite[iSprite2].posY;iY<DDsprite[iSprite2].height;iY++)		{			//loop through all Y pixels of sprite 1 for match			for (int k=DDsprite[iSprite1].posY;k<DDsprite[iSprite1].height;k++)			{				//if match found				if (k == iY)				{					//sprites have collided					return true;				}			}		}	}	return false;}

[EDIT] i find it a bit strange for a board of this caliber that focuses mostly on code not to have a CODE block option right on the post screen.

[Edited by - jsloan on September 12, 2004 2:18:26 AM]
Quote:Original post by jsloan
[EDIT] i find it a bit strange for a board of this caliber that focuses mostly on code not to have a CODE block option right on the post screen.


Good point. Anyway use [source] // code to be formated goes here [/source] or [code] // short snippets go here [/code] tags to accomplish that task.
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________
Thank you kind sir :)

This topic is closed to new replies.

Advertisement