Elements used in the Balls and Things games for the RETRO.

Dependents:   RETRO_BallsAndPaddle RETRO_BallAndHoles

Revision:
7:4fa3edaa1201
Parent:
4:f421e34313d3
Child:
8:19dd2a538cbe
--- a/Shapes.cpp	Sat Feb 28 19:40:48 2015 +0000
+++ b/Shapes.cpp	Mon Mar 02 09:04:13 2015 +0000
@@ -83,7 +83,7 @@
 
 int Line::getDistance(Point pt)
 {   // get the distance of a line to a point
-    //TODO
+    // TODO: more precise calculation, currently it's a kind-of lame approximation
     Line ln1(get1(), pt);
     Line ln2(get2(), pt);
     int nDist1=ln1.getSize();
@@ -122,7 +122,6 @@
     y2 = pt2.getY();
 }
 
-
 bool Rectangle::collides(Point pt)
 {
     if(pt.getX() >= x1 && pt.getX() <= x2) {
@@ -133,28 +132,18 @@
     return false;
 }
 
-/*
-Rectangle Rectangle::intersection(Rectangle r)
-{   // return the intersection of two rectangles or null
-    Rectangle rResult;
-    rResult=Rectangle()
-    if(this->x2 >= 
-}
-*/
-
 bool Rectangle::collides(Rectangle r)
 {   // check if two rectangles collide
-    // method 1: check if corners of eithter rectangle collide
-    // method 2: compare sides
-    if(this->collides(r.get1()) || this->collides(r.get2())) return(true);
-    if(this->collides(r.get3()) || this->collides(r.get4())) return(true);
-    if(r.collides(this->get1()) || r.collides(this->get2())) return(true);
-    if(r.collides(this->get3()) || r.collides(this->get4())) return(true);
-    // TODO: check other corners
-    return(false);
+    // method: compare rectangle sides
+    // see http://stackoverflow.com/questions/7610129/simple-collision-detection-android
+    if ((getX2() < r.getX1()) || // A is to the left of B
+        (r.getX2() < getX1()) || // B is to the left of A
+        (getY2() < r.getY1()) || // A is above B
+        (r.getY2() < getY1()))   // B is above A
+        return(false);
+    return(true);
 }
 
-
 Point Rectangle::get1()
 {
     return(Point(x1, y1));