# Thread: collision dectection in 2d

1. ## collision dectection in 2d

can anyone help with an air hockey game i am creating in c++, i need to figure out what do do after the code has detected a collision

but i can't seem to find any good sources on it at all

Code:
```void checkPlayerCollision()
{
int temp;
int temp2;
int temp3;
temp3 = temp + temp2;

if( (player.GetXLocation - AIpuck.GetXLocation)%(player.GetYLocation - AIpuck.GetYLocation) <= temp3
{

}```
this is what i have so far

2. Well, there are many different approaches and I don't really know what kind of movement system you have used. If you are calculating collision with a wall, things are rather simple, but if you are calculating collision between two pucks (as your code indicates), things can be more complicated.

Take a look at this site (especially the links at the bottom of the page):
http://www.edenwaith.com/products/pi.../collision.php

And here are some other sites:
http://www.gamedev.net/reference/art...rticle1026.asp
http://www.harveycartel.org/metanet/...tutorialA.html
http://www.gamedev.net/reference/list.asp?categoryid=45 (look a bit down the page)

....

but i can't seem to find any good sources on it at all
I don't understand why you didn't find info on this subject. I suggest you google the topic, something like "collision detection games" or whatever.

Hope this will help

3. ok i have had a look and attempted to impliment it, but no joy so far i'm not sure if its the maths that isn't working or not

Code:
```void checkPlayerCollision()
{
int playerx;
int playery;
int puckx;
int pucky;
int distancex;
int distancey;
int totaldistancesqd;
int totdistance;
playerx = player.GetXLocation();
puckx = AIpuck.GetXLocation();
playery = player.GetYLocation();
pucky = AIpuck.GetYLocation();

distancex = playerx-puckx;
distancey = playery-pucky;
cout << distancex << endl;
cout << distancey << endl;

totaldistancesqd = ((distancex*distancex)+(distancey*distancey));

totdistance = sqrt (totaldistancesqd);

cout << totdistance << endl;

if (totdistance <= 0)
{
AIpuck.setVelocityX(5);
AIpuck.setVelocityY(-5);
}

}```
Code:
```class CPuck
{
public:
CPuck();
virtual ~CPuck();
SDL_Surface *m_image;
void update();
void passImage(SDL_Surface *img) {m_image = img;};
void drawImage(SDL_Surface*);
void moveLocationX(int a){m_locx += a;};
void moveLocationY(int a){m_locy += a;};
void setVelocityX(int a){m_locx += a;};
void setVelocityY(int a){m_locy += a;};
int GetXLocation(){return m_locx;};
int GetYLocation(){return m_locy;};
int GetHeight(){return height;};
int GetWidth(){return width;};
bool collision();
private:
int m_locx, m_locy;
int m_velx, m_vely;
int height;
int width;
Code:
```class CPaddle
{
public:
SDL_Surface *m_image;
void update();
void passImage(SDL_Surface *img) {m_image = img;};
void drawImage(SDL_Surface*);
//this has the code in the {} brackets
void moveLocationX(int a){m_locx += a;};
void moveLocationY(int a){m_locy += a;};
void setVelocityX(int a){m_locx += a;};
void setVelocityY(int a){m_locy += a;};
void outofbounds();
int GetXLocation(){return m_locx;};
int GetYLocation(){return m_locy;};
int GetHeight(){return height;};
int GetWidth(){return width;};
int ScoreOutput();
private:
int m_locx, m_locy;
int m_velx, m_vely;
int score;
int height;
int width;
player is an instance of the paddle class btw

4. distancex = playerx-puckx;
distancey = playery-pucky;
cout << distancex << endl;
cout << distancey << endl;

totaldistancesqd = ((distancex*distancex)+(distancey*distancey));

totdistance = sqrt (totaldistancesqd);

cout << totdistance << endl;

if (totdistance <= 0)
{
AIpuck.setVelocityX(5);
AIpuck.setVelocityY(-5);
}
What you do here is following (as I can see): you first calculate a vector between centers of the puck and the player paddle. Then you calculate the length of that vector.

What I can see right away, is that you take in account only the starting position of the paddle and NOT its width at all.

I don't know what kind of coordinate system you are using, but in this example I use the following one:

0----1-----2------->
|
|
1
|
|
2
|
|
V

I can't tell for sure (since I right now have no time to test if it works), but as far as I can see you can just do the following:

And further this:

AIpuck.setVelocityX(5);
AIpuck.setVelocityY(-5);
This wont help you much. You do not take in account the movement vector of the puck when it hits the paddle. (NB! Implement first getVelocityX() and getVelocityY() in your CPuck class)

We also assume that the paddle is always horizontal and the puck comes down from the top of the screen.

If you are using another coordinate system, change the code to fit your requirements.

Here is the code:

Code:
```if((Puck.getLocationY()+Puck.getRadius())>=Paddle.getLocationY())
{
Puck.setVelocityY(Puck.getVelocityY()*-1);
}```
This again wont always be sufficient either. You will all the time see the puck almost going through the paddle, so what we need is a correction to this. Here is the whole function for collision testing:

Code:
```void checkPlayerCollision()
{