collision detection problem solved!

This is a discussion on collision detection problem solved! within the Game Programming forums, part of the General Programming Boards category; I decided to go back to the roots of where all this came from, with all the trigonometry and calculus ...

  1. #1
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964

    collision detection problem solved!

    I decided to go back to the roots of where all this came from, with all the trigonometry and calculus and whatnot.

    I decided to add a few structs to help my cause, ray and plane.

    I learned that a ray is nothing but a point origin and basically another point which is found by subtracting point a from b.

    I learned that I actually need the dot product of that specific ray's directional vector.

    Among other numerous vector math stuff.

    I applied my knowledge and came up with this:

    Perfect!

    Code:
    	bool planeCollision2d(plane & boundingPlane, vector2 & objectPosition)
    	{
    		ray line_between_objects(objectPosition,boundingPlane.position);
    		double DotProduct=line_between_objects.direction.dot_product(boundingPlane.normal);
    		double l2;
    
    		//determine if ray paralle to plane
    		if ((DotProduct<0)&&(DotProduct>-0)) 
    			return false;
    
    		l2=(boundingPlane.normal.dot_product(boundingPlane.position-objectPosition))/DotProduct;
    
    		if (l2<-0) 
    			return false;
    
    		return true;
    	}

    Ahhh the beauty and simplicity of pong logic.
    Code:
    		if(ball->getVelocity().x > 0)
    		{
    			if (collision2d(ball->getMinimum(), ball->getMaximum(), player2->paddle->getMinimum(), player2->paddle->getMaximum()))
    			{
    				reverse_x_velocity(ball->getVelocity());
    			}
    			else if(planeCollision2d(right, ball->getLocation()))
    			{
    				reverse_x_velocity(ball->getVelocity());
    				ball->getLocation().x = 0;
    				ball->getLocation().y = 0;
    				player1->goal();
    			}
    		}
    		if(ball->getVelocity().x < 0)
    		{
    
    			if (collision2d(ball->getMinimum(), ball->getMaximum(), player1->paddle->getMinimum(), player1->paddle->getMaximum()))
    			{
    				reverse_x_velocity(ball->getVelocity());
    			}
    			else if(planeCollision2d(left, ball->getLocation()))
    			{
    				reverse_x_velocity(ball->getVelocity());
    				ball->getLocation().x = 0;
    				ball->getLocation().y = 0;
    				player2->goal();
    			}
    		}
    		if(ball->getVelocity().y > 0)
    		{
    			if (planeCollision2d(top, ball->getLocation()))
    			{
    				reverse_y_velocity(ball->getVelocity());
    			}
    		}
    		if(ball->getVelocity().y < 0)
    		{
    			if (planeCollision2d(bottom, ball->getLocation()))
    			{
    				reverse_y_velocity(ball->getVelocity());
    			}
    		}
    	}
    	void check_paddle_collision()
    	{	
    		if (planeCollision2d(top, player1->paddle->getLocation()))
    		{
    			player1->paddle->getLocation().y = 70;
    		}
    		else if(planeCollision2d(bottom, player1->paddle->getLocation()))
    		{
    			player1->paddle->getLocation().y = -70;
    		}
    		if (planeCollision2d(top, player2->paddle->getLocation()))
    		{
    			player2->paddle->getLocation().y = 70;
    		}
    		else if(planeCollision2d(bottom, player2->paddle->getLocation()))
    		{
    			player2->paddle->getLocation().y = -70;
    		}	
    	}
    Last edited by Shamino; 04-07-2009 at 09:40 PM.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Still using numeric constants like 70, I see.

    Also, try to avoid calling variables things that look like numbers, such as l2.

    Code:
    if (l2<-0)
    is that supposed to be l2 <= 0?

    Edit:
    Code:
    reverse_x_velocity(ball->getVelocity());
    Do I take it that getVelocity returns a reference to Ball::velocity? If so, perhaps you should do something like this instead:
    Code:
    ball->setVelocity(-ball->getVelocity().x, ball->getVelocity().y);
    That maintains strict encapsulation.
    Or you could let the velocity itself have a public member function "reversex()" and "reversey()".

    --
    Mats

    --
    Mats
    Last edited by matsp; 04-08-2009 at 02:29 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple 2D rubiks cube algorithm.
    By jeanmarc in forum Game Programming
    Replies: 19
    Last Post: 11-11-2008, 06:40 PM
  2. questions about arrays basic collision detection
    By The_Hermit in forum Game Programming
    Replies: 2
    Last Post: 07-23-2008, 11:29 AM
  3. sprite collision detection
    By LogicError in forum Game Programming
    Replies: 1
    Last Post: 05-06-2005, 10:43 AM
  4. Scheduling - Collision Detection
    By BigDaddyDrew in forum C++ Programming
    Replies: 10
    Last Post: 02-19-2003, 01:48 PM
  5. matrixes for collision detection
    By DavidP in forum Game Programming
    Replies: 10
    Last Post: 11-09-2002, 09:31 PM

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