Jump to content
• Advertisement

Public Group

# 2d collistion detection

This topic is 5177 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 was wondering what the easyest way is to write collition detection in c++ WIN API for just a simple game of pong.

#### Share this post

##### Share on other sites
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

#### Share this post

##### Share on other sites
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

#### Share this post

##### Share on other sites
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;};

#### Share this post

##### Share on other sites
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]

#### Share this post

##### Share on other sites
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.

#### Share this post

##### Share on other sites
Thank you kind sir :)

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5
• Advertisement

• 9
• 9
• 9
• 14
• 12
• ### Forum Statistics

• Total Topics
633302
• Total Posts
3011275
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!