- Main Page
- Classes
- Files

- File List
- File Members

# flCollision.c

Go to the documentation of this file.

    00001 #include "flGlobal.h"
    00002 #if FL_COLLISION != 0
    00003
    00004 #if FL_INCLUDE_ALL_C == 0
    00005 #include "flCollision.h"
    00006 #endif
    00007
    00008 vect2f collisionLast;
    00009
    00010 vect2f collision2dLineClosestPoint(Line2d inLine, vect2f inPoint) {
    00011     vect2f tempDir;
    00012     tempDir.x = (inLine.linePoints[1].x - inLine.linePoints[0].x);
    00013     tempDir.y = (inLine.linePoints[1].y - inLine.linePoints[0].y);
    00014
    00015     vect2f tempDot;
    00016     tempDot.x = (inPoint.x - inLine.linePoints[0].x);
    00017     tempDot.y = (inPoint.y - inLine.linePoints[0].y);
    00018
    00019     float tempNumer = ((tempDot.x * tempDir.x) + (tempDot.y * tempDir.y));
    00020     if (tempNumer <= 0.0f)
    00021         return inLine.linePoints[0];
    00022     float tempDenom = ((tempDir.x * tempDir.x) + (tempDir.y * tempDir.y));
    00023     if (tempNumer >= tempDenom)
    00024         return inLine.linePoints[1];
    00025     tempDot.x = (inLine.linePoints[0].x + ((tempNumer / tempDenom) * tempDir.x));
    00026     tempDot.y = (inLine.linePoints[0].y + ((tempNumer / tempDenom) * tempDir.y));
    00027     return tempDot;
    00028 }
    00029
    00030 bool collision2dLineLine(Line2d inLine0, Line2d inLine1) {
    00031      vect2f tempCollision;
    00032
    00033      float tempM[2];
    00034      float tempC[2];
    00035
    00036      if(inLine0.linePoints[0].x == inLine0.linePoints[1].x) {
    00037           tempCollision.x = inLine0.linePoints[0].x;
    00038           if(inLine1.linePoints[0].x == inLine1.linePoints[1].x) {
    00039                if(inLine1.linePoints[0].x == inLine0.linePoints[0].x) {
    00040                     if(min(inLine1.linePoints[0].y, inLine1.linePoints[0].y) <= max(inLine0.linePoints[1].y, inLine0.linePoints[1].y)) {
    00041                          if(max(inLine1.linePoints[0].y, inLine1.linePoints[0].y) >= min(inLine0.linePoints[1].y, inLine0.linePoints[1].y)) {
    00042                               tempM[0] = min(inLine0.linePoints[0].y, inLine0.linePoints[1].y);
    00043                               tempM[1] = min(inLine1.linePoints[0].y, inLine1.linePoints[1].y);
    00044                               tempM[0] = max(tempM[0], tempM[1]);
    00045                               tempC[0] = max(inLine0.linePoints[0].y, inLine0.linePoints[1].y);
    00046                               tempC[1] = max(inLine1.linePoints[0].y, inLine1.linePoints[1].y);
    00047                               tempC[0] = min(tempC[0], tempC[1]);
    00048                               tempCollision.y = ((tempM[0] + tempC[0]) * 0.5f);
    00049                               collisionLast = tempCollision;
    00050                               return true;
    00051                          }
    00052                     }
    00053                }
    00054                return false;
    00055           }
    00056           tempM[1] = ((inLine1.linePoints[0].y - inLine1.linePoints[1].y) / (inLine1.linePoints[0].x - inLine1.linePoints[1].x));
    00057           tempC[1] = (inLine1.linePoints[0].y - (tempM[1] * inLine1.linePoints[0].x));
    00058           tempCollision.y = ((tempM[1] * tempCollision.x) + tempC[1]);
    00059           if(tempCollision.y < min(inLine0.linePoints[0].y, inLine0.linePoints[1].y))
    00060                return false;
    00061           if(tempCollision.y < min(inLine1.linePoints[0].y, inLine1.linePoints[1].y))
    00062                return false;
    00063           if(tempCollision.y > max(inLine0.linePoints[0].y, inLine0.linePoints[1].y))
    00064                return false;
    00065           if(tempCollision.y > max(inLine1.linePoints[0].y, inLine1.linePoints[1].y))
    00066                return false;
    00067           if(tempCollision.x < min(inLine0.linePoints[0].x, inLine0.linePoints[1].x))
    00068                return false;
    00069           if(tempCollision.x < min(inLine1.linePoints[0].x, inLine1.linePoints[1].x))
    00070                return false;
    00071           if(tempCollision.x > max(inLine0.linePoints[0].x, inLine0.linePoints[1].x))
    00072                return false;
    00073           if(tempCollision.x > max(inLine1.linePoints[0].x, inLine1.linePoints[1].x))
    00074                return false;
    00075           collisionLast = tempCollision;
    00076           return true;
    00077      } else if(inLine1.linePoints[0].x == inLine1.linePoints[1].x) {
    00078           tempCollision.x = inLine1.linePoints[0].x;
    00079           tempM[0] = ((inLine0.linePoints[0].y - inLine0.linePoints[1].y) / (inLine0.linePoints[0].x - inLine0.linePoints[1].x));
    00080           tempC[0] = (inLine0.linePoints[0].y - (tempM[0] * inLine0.linePoints[0].x));
    00081           tempCollision.y = ((tempM[0] * tempCollision.x) + tempC[0]);
    00082           if(tempCollision.y < min(inLine0.linePoints[0].y, inLine0.linePoints[1].y))
    00083                return false;
    00084           if(tempCollision.y < min(inLine1.linePoints[0].y, inLine1.linePoints[1].y))
    00085                return false;
    00086           if(tempCollision.y > max(inLine0.linePoints[0].y, inLine0.linePoints[1].y))
    00087                return false;
    00088           if(tempCollision.y > max(inLine1.linePoints[0].y, inLine1.linePoints[1].y))
    00089                return false;
    00090           if(tempCollision.x < min(inLine0.linePoints[0].x, inLine0.linePoints[1].x))
    00091                return false;
    00092           if(tempCollision.x < min(inLine1.linePoints[0].x, inLine1.linePoints[1].x))
    00093                return false;
    00094           if(tempCollision.x > max(inLine0.linePoints[0].x, inLine0.linePoints[1].x))
    00095                return false;
    00096           if(tempCollision.x > max(inLine1.linePoints[0].x, inLine1.linePoints[1].x))
    00097                return false;
    00098           collisionLast = tempCollision;
    00099           return true;
    00100      }
    00101
    00102      tempM[0] = ((inLine0.linePoints[0].y - inLine0.linePoints[1].y) / (inLine0.linePoints[0].x - inLine0.linePoints[1].x));
    00103      tempM[1] = ((inLine1.linePoints[0].y - inLine1.linePoints[1].y) / (inLine1.linePoints[0].x - inLine1.linePoints[1].x));
    00104
    00105      tempC[0] = (inLine0.linePoints[0].y - (tempM[0] * inLine0.linePoints[0].x));
    00106      tempC[1] = (inLine1.linePoints[0].y - (tempM[1] * inLine1.linePoints[0].x));
    00107
    00108      if(tempM[0] == tempM[1]) {
    00109           if(tempC[0] == tempC[1]) {
    00110                // TODO - Stop being lazy and calculate hit point properly.
    00111                tempCollision.x = ((inLine0.linePoints[0].x + inLine0.linePoints[1].x + inLine1.linePoints[0].x + inLine1.linePoints[1].x) * 0.25f);
    00112                tempCollision.y = ((inLine0.linePoints[0].y + inLine0.linePoints[1].y + inLine1.linePoints[0].y + inLine1.linePoints[1].y) * 0.25f);
    00113                collisionLast = tempCollision;
    00114                return true;
    00115           }
    00116           return false;
    00117      }
    00118
    00119      tempCollision.x = ((tempC[1] - tempC[0]) / (tempM[0] - tempM[1]));
    00120      tempCollision.y = ((tempM[0] * tempCollision.x) + tempC[0]);
    00121
    00122      if(tempCollision.y < min(inLine0.linePoints[0].y, inLine0.linePoints[1].y))
    00123           return false;
    00124      if(tempCollision.y < min(inLine1.linePoints[0].y, inLine1.linePoints[1].y))
    00125           return false;
    00126      if(tempCollision.y > max(inLine0.linePoints[0].y, inLine0.linePoints[1].y))
    00127           return false;
    00128      if(tempCollision.y > max(inLine1.linePoints[0].y, inLine1.linePoints[1].y))
    00129           return false;
    00130      if(tempCollision.x < min(inLine0.linePoints[0].x, inLine0.linePoints[1].x))
    00131           return false;
    00132      if(tempCollision.x < min(inLine1.linePoints[0].x, inLine1.linePoints[1].x))
    00133           return false;
    00134      if(tempCollision.x > max(inLine0.linePoints[0].x, inLine0.linePoints[1].x))
    00135           return false;
    00136      if(tempCollision.x > max(inLine1.linePoints[0].x, inLine1.linePoints[1].x))
    00137           return false;
    00138      collisionLast = tempCollision;
    00139      return true;
    00140 }
    00141
    00142 bool collision2dCircleCircle(Circle2d inCirc0, Circle2d inCirc1) {
    00143      vect2f tempCollision;
    00144
    00145      tempCollision.x = (max(inCirc0.circCenter.x, inCirc1.circCenter.x) - min(inCirc0.circCenter.x, inCirc1.circCenter.x));
    00146      tempCollision.y = (max(inCirc0.circCenter.y, inCirc1.circCenter.y) - min(inCirc0.circCenter.y, inCirc1.circCenter.y));
    00147      float tempDist = sqrt((tempCollision.x * tempCollision.x) + (tempCollision.y * tempCollision.y));
    00148      if(tempDist < min(inCirc0.circRadius, inCirc1.circRadius)) {
    00149           tempCollision.x *= 0.5f;
    00150           tempCollision.y *= 0.5f;
    00151           tempCollision.x += min(inCirc0.circCenter.x, inCirc1.circCenter.x);
    00152           tempCollision.y += min(inCirc0.circCenter.y, inCirc1.circCenter.y);
    00153           collisionLast = tempCollision;
    00154           return true;
    00155      }
    00156
    00157      return false;
    00158 }
    00159
    00160 bool collision2dLineCircle(Line2d inLine, Circle2d inCirc) {
    00161      vect2f tempCollision;
    00162      vect2f tempPoint = collision2dLineClosestPoint(inLine, inCirc.circCenter);
    00163
    00164      tempCollision.x = (max(tempPoint.x, inCirc.circCenter.x) - min(tempPoint.x, inCirc.circCenter.x));
    00165      tempCollision.y = (max(tempPoint.y, inCirc.circCenter.y) - min(tempPoint.y, inCirc.circCenter.y));
    00166      float tempDist = sqrt((tempCollision.x * tempCollision.x) + (tempCollision.y * tempCollision.y));
    00167      if(tempDist < inCirc.circRadius) {
    00168           collisionLast = tempPoint;
    00169           return true;
    00170      }
    00171
    00172      return false;
    00173 }
    00174
    00175 bool collision2dRectRect(Rect2d inRect0, Rect2d inRect1) {
    00176      if(inRect1.rectPoints.x > (inRect0.rectPoints.x + inRect0.rectSize.x))
    00177           return false;
    00178      if((inRect1.rectPoints.x + inRect1.rectSize.x) < inRect0.rectPoints.x)
    00179           return false;
    00180      if(inRect1.rectPoints.y > (inRect0.rectPoints.y + inRect0.rectSize.y))
    00181           return false;
    00182      if((inRect1.rectPoints.y + inRect1.rectSize.y) < inRect0.rectPoints.y)
    00183           return false;
    00184
    00185      collisionLast.x = inRect1.rectPoints.x + (((inRect0.rectPoints.x + inRect0.rectSize.x) - inRect1.rectPoints.x) / 2.0f);
    00186      collisionLast.y = inRect1.rectPoints.y + (((inRect0.rectPoints.y + inRect0.rectSize.y) - inRect1.rectPoints.y) / 2.0f);
    00187
    00188      return true;
    00189 }
    00190
    00191 #endif