   Breakout Collision Detection
  I'm trying to do some form of collision detection for my game of breakout.I can't seem to get it to work =/I know it's missing something.If someone could give me a hand that would be great.#define ninetyRad  (1.5707955f)#define oneEightyRad  (3.14159f)#define twoSeventyRad  (4.7123857f)#define threeSixtyRad  (6.28318f)namespace Collision{struct Point{float x;float y;Point( float my_x = 0, float my_y = 0 ) : x(my_x), y(my_y){};};struct Path{Point p1;Point p2;Path( Point my_p1 = 0, Point my_p2 = 0 ) : p1(my_p1), p2(my_p2){};};enum Type{None,Top,Right,Bottom,Left,Horizontal,Vertical,CountTypes};struct Information{Collision::Type Object1;Collision::Type Object2;Collision::Point PointOfCollision;Information(){Object1 = Type::None;Object2 = Type::None;PointOfCollision.x = -1;PointOfCollision.y = -1;}};float BounceObject( float directionAngle, Type collisionType ){// If there is no collision, returnif( collisionType == Type::None ) return directionAngle;switch( collisionType ){case Type::Vertical:{if( directionAngle &lt; twoSeventyRad && directionAngle &lt; ninetyRad ){directionAngle = twoSeventyRad + ( twoSeventyRad - directionAngle );}else if( directionAngle &gt; ninetyRad && directionAngle &gt; twoSeventyRad ){directionAngle = ninetyRad + ( ninetyRad - directionAngle );}break;}case Type::Horizontal:{if( directionAngle &lt; oneEightyRad && directionAngle &gt; 0 ){directionAngle = oneEightyRad + ( oneEightyRad - directionAngle );}else if ( directionAngle &gt; oneEightyRad && directionAngle &lt; threeSixtyRad ){directionAngle = oneEightyRad - ( directionAngle - oneEightyRad );}break;}default:return directionAngle;}return directionAngle;};Information DetectDynamicCollision( Sprite& Object1, float Object1PrevX, float Object1PrevY, Sprite& Object2 ){Information cInfo;// Fix for if center is not in the centerfloat centerX1 = Object1.GetCenter().x;float centerY1 = Object1.GetCenter().y;// Object 1 info.float object1Left = Object1.GetPosition().x - centerX1;float object1Right = object1Left + Object1.GetSubRect().GetWidth();float object1Top = Object1.GetPosition().y - centerY1;float object1Bottom = object1Top + Object1.GetSubRect().GetHeight();float object1CenterX = ( object1Right - object1Left ) / 2;float object1CenterY = ( object1Bottom - object1Top ) / 2;// Fix for if center is not in the centerfloat centerX2 = Object2.GetCenter().x;float centerY2 = Object2.GetCenter().y;// Object 1 info.float object2Left = Object2.GetPosition().x - centerX2;float object2Right = object2Left + Object2.GetSubRect().GetWidth();float object2Top = Object2.GetPosition().y - centerY2;float object2Bottom = object2Top + Object2.GetSubRect().GetHeight();float object2CenterX = ( object2Right - object2Left ) / 2;float object2CenterY = ( object2Bottom - object2Top ) / 2;// Flag to say which path we're using.int usingPath = 1;Path object1Path1;Path object2Path1;Path object2Path2;// Path for the moving object (object 1)object1Path1.p1.x = Object1PrevX;object1Path1.p1.y = Object1PrevY;object1Path1.p2.x = object1CenterX;object1Path1.p2.y = object1CenterY;// it is heading right, so display left sideif( Object1PrevX &gt; 0 ){object2Path1.p1.x = object2Left;object2Path1.p1.y = object2Top;object2Path1.p2.x = object2Left;object2Path1.p2.y = object2Bottom;}// it is heading left, so display right sideelse{object2Path1.p1.x = object2Right;object2Path1.p1.y = object2Top;object2Path1.p2.x = object2Right;object2Path1.p2.y = object2Bottom;}// it is heading down, so display top sideif( Object1PrevY &gt; 0 ){object2Path2.p1.x = object2Left;object2Path2.p1.y = object2Top;object2Path2.p2.x = object2Right;object2Path2.p2.y = object2Top;}// it is heading up, so display bottom sideelse{object2Path2.p1.x = object2Left;object2Path2.p1.y = object2Bottom;object2Path2.p2.x = object2Right;object2Path2.p2.y = object2Bottom;}cInfo.PointOfCollision.x =((((object1Path1.p1.x * object1Path1.p2.y) - (object1Path1.p1.y * object1Path1.p2.x)) * (object2Path1.p1.x - object2Path1.p2.x)) - ((object1Path1.p1.x - object1Path1.p2.x) * ((object2Path1.p1.x * object2Path1.p2.y) - (object2Path1.p1.y * object2Path1.p2.x)))/(((object1Path1.p1.x - object1Path1.p2.x) * (object2Path1.p1.y - object2Path1.p2.y)) - ((object1Path1.p1.y - object1Path1.p2.y) * (object2Path1.p1.x - object2Path1.p2.x))));cInfo.PointOfCollision.y = ((((object1Path1.p1.x * object1Path1.p2.y) - (object1Path1.p1.y * object1Path1.p2.x)) * (object2Path1.p1.y - object2Path1.p2.y)) - ((object1Path1.p1.y - object1Path1.p2.y) * ((object2Path1.p1.x * object2Path1.p2.y) - (object2Path1.p1.y * object2Path1.p2.x)))/(((object1Path1.p1.x - object1Path1.p2.x) * (object2Path1.p1.y - object2Path1.p2.y)) - ((object1Path1.p1.y - object1Path1.p2.y) * (object2Path1.p1.x - object2Path1.p2.x))));if( cInfo.PointOfCollision.x &gt; 1000 || cInfo.PointOfCollision.y &gt; 1000 ){usingPath = 2;cInfo.PointOfCollision.x =((((object1Path1.p1.x * object1Path1.p2.y) - (object1Path1.p1.y * object1Path1.p2.x)) * (object2Path2.p1.x - object2Path2.p2.x)) - ((object1Path1.p1.x - object1Path1.p2.x) * ((object2Path2.p1.x * object2Path2.p2.y) - (object2Path2.p1.y * object2Path2.p2.x)))/(((object1Path1.p1.x - object1Path1.p2.x) * (object2Path2.p1.y - object2Path2.p2.y)) - ((object1Path1.p1.y - object1Path1.p2.y) * (object2Path2.p1.x - object2Path2.p2.x))));cInfo.PointOfCollision.y = ((((object1Path1.p1.x * object1Path1.p2.y) - (object1Path1.p1.y * object1Path1.p2.x)) * (object2Path2.p1.y - object2Path2.p2.y)) - ((object1Path1.p1.y - object1Path1.p2.y) * ((object2Path2.p1.x * object2Path2.p2.y) - (object2Path2.p1.y * object2Path2.p2.x)))/(((object1Path1.p1.x - object1Path1.p2.x) * (object2Path2.p1.y - object2Path2.p2.y)) - ((object1Path1.p1.y - object1Path1.p2.y) * (object2Path2.p1.x - object2Path2.p2.x))));}if( cInfo.PointOfCollision.x &gt; 1000 || cInfo.PointOfCollision.y &gt; 1000 ){cInfo.Object1 = Type::None;cInfo.Object2 = Type::None;}else{if( object1Left + object1CenterX == cInfo.PointOfCollision.x && object1Top + object1CenterY == cInfo.PointOfCollision.y ){if( usingPath == 1 ){cInfo.Object1 = Type::Vertical;cInfo.Object2 = Type::Vertical;}else{cInfo.Object1 = Type::Horizontal;cInfo.Object2 = Type::Horizontal;}}}/*This is the ugly equation!cInfo.PointOfCollision.x =((((x1 * y2) - (y1 * x2)) * (x3 - x4)) - ((x1 - x2) * ((x3 * y4) - (y3 * x4)))/(((x1 - x2) * (y3 - y4)) - ((y1 - y2) * (x3 - x4))));cInfo.PointOfCollision.y = ((((x1 * y2) - (y1 * x2)) * (y3 - y4)) - ((y1 - y2) * ((x3 * y4) - (y3 * x4)))/(((x1 - x2) * (y3 - y4)) - ((y1 - y2) * (x3 - x4))));*/// Return the type if there is a collisionreturn cInfo;}}; [Edited by - ivekilledem on June 6, 2010 1:13:09 AM]
Quote:Original post by ivekilledemI can't seem to get it to workWhat about it doesn't work?And for long code listings, please use source tags instead of code tags.   ;Quote:What about it doesn't work?the line intersection never = the position of Object1regardless of whether or not the paths are actually intersectingWhat I'm trying to get it to do is, during that frame, if the path of Object1, (in this case the ball), crosses either of the two paths drawn up by Object2,It should fire off and bounce the ball back at the appropriate angle.   