collision dectection in 2d

This is a discussion on collision dectection in 2d within the Game Programming forums, part of the General Programming Boards category; can anyone help with an air hockey game i am creating in c++, i need to figure out what do ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    84

    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;
    	temp = player.GetRadius();
    	temp2 = AIpuck.GetRadius();
    	temp3 = temp + temp2;
    
    	if( (player.GetXLocation - AIpuck.GetXLocation)%(player.GetYLocation - AIpuck.GetYLocation) <= temp3
    	{
    
    
    
    }
    this is what i have so far

  2. #2
    Registered User MathFan's Avatar
    Join Date
    Apr 2002
    Posts
    190
    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
    The OS requirements were Windows Vista Ultimate or better, so we used Linux.

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    84
    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 playerradius;
    	int puckradius;
    	int playerx;
    	int playery;
    	int puckx;
    	int pucky;
    	int distancex;
    	int distancey;
    	int totaldistancesqd;
    	int totdistance;
    	playerradius = player.GetRadius();
    	puckradius = AIpuck.GetRadius();
    	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;};
    	int GetRadius(){return radius;};
    	bool collision();
    private:
    	int m_locx, m_locy;
    	int m_velx, m_vely;
    	int height;
    	int width;
    	int radius;
    Code:
    class CPaddle  
    {
    public:
    	CPaddle();
    	virtual ~CPaddle();
    	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 GetRadius(){return radius;};
    	int ScoreOutput();
    private:
    	int m_locx, m_locy;
    	int m_velx, m_vely;
    	int score;
    	int height;
    	int width;
    	int radius;
    player is an instance of the paddle class btw

  4. #4
    Registered User MathFan's Avatar
    Join Date
    Apr 2002
    Posts
    190
    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:

    check if (Puck.getLocationX()+Puck.getRadius())<=Paddle.get LocationY()

    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()
    	{
    		if((Puck.getLocationY()+Puck.getRadius())>=Paddle.getLocationY())
    			{
    				Puck.setVelocityY(Puck.getVelocityY()*-1);
    				Puck.setLocationY(Puck.getLocationY()-(Puck.getRadius()-(Paddle.getLocationY()-Puck.getLocationY())));
    			}
    	}
    (Remember, I am still using the coordinate system I described above.)

    This may help, but remember that if the puck moves too fast (if speed per tick is more than (PaddleWidth+2*PuckRadius)) this collision engine won't detect the collision itself.

    I just hope the code I wrote works... If not, tell me
    The OS requirements were Windows Vista Ultimate or better, so we used Linux.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D pixel perfect collision detection
    By Warlax in forum Game Programming
    Replies: 0
    Last Post: 06-22-2006, 07:39 AM
  2. Pixel perfect 2D collision detection with Allegro
    By Warlax in forum C++ Programming
    Replies: 1
    Last Post: 06-21-2006, 02:14 PM
  3. Help with reacting to collision in my 2D sidescroller
    By Marcos in forum Game Programming
    Replies: 6
    Last Post: 03-04-2006, 10:47 PM
  4. 2d collision and image structure
    By valis in forum Game Programming
    Replies: 6
    Last Post: 11-20-2005, 09:28 PM
  5. 2d Collision Detection in openGL
    By Shamino in forum Game Programming
    Replies: 4
    Last Post: 05-12-2005, 11:02 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21