diff options
| author | Ray <raysan5@gmail.com> | 2016-02-20 01:09:47 +0100 |
|---|---|---|
| committer | Ray <raysan5@gmail.com> | 2016-02-20 01:09:47 +0100 |
| commit | 4b6e6d4dd45fc3a47b82af7eeb60871140a7eccf (patch) | |
| tree | d9ee679c3b0bc5d4a20b31930994deb0e4c37948 /src/shapes.c | |
| parent | f582ab06add085594f2579ee6e7d625212abd204 (diff) | |
| parent | 954ced21a42eb489ad382b4c00406a28778fee41 (diff) | |
| download | raylib-4b6e6d4dd45fc3a47b82af7eeb60871140a7eccf.tar.gz raylib-4b6e6d4dd45fc3a47b82af7eeb60871140a7eccf.zip | |
Merge pull request #83 from raysan5/develop
Develop branch integration
Diffstat (limited to 'src/shapes.c')
| -rw-r--r-- | src/shapes.c | 19 |
1 files changed, 14 insertions, 5 deletions
diff --git a/src/shapes.c b/src/shapes.c index 071fa63c..65e3621b 100644 --- a/src/shapes.c +++ b/src/shapes.c @@ -391,19 +391,28 @@ bool CheckCollisionCircles(Vector2 center1, float radius1, Vector2 center2, floa } // Check collision between circle and rectangle +// NOTE: Reviewed version to take into account corner limit case bool CheckCollisionCircleRec(Vector2 center, float radius, Rectangle rec) { - bool collision = false; + int recCenterX = rec.x + rec.width/2; + int recCenterY = rec.y + rec.height/2; + + float dx = fabs(center.x - recCenterX); + float dy = fabs(center.y - recCenterY); - float dx = fabs((rec.x + rec.width/2) - center.x); - float dy = fabs((rec.y + rec.height/2) - center.y); + if (dx > (rec.width/2 + radius)) { return false; } + if (dy > (rec.height/2 + radius)) { return false; } - if ((dx <= (rec.width/2 + radius)) && (dy <= (rec.height/2 + radius))) collision = true; + if (dx <= (rec.width/2)) { return true; } + if (dy <= (rec.height/2)) { return true; } - return collision; + float cornerDistanceSq = pow(dx - rec.width/2, 2) + pow(dy - rec.height/2, 2); + + return (cornerDistanceSq <= (radius*radius)); } // Get collision rectangle for two rectangles collision +// TODO: Depending on rec1 and rec2 order, it fails -> Review! Rectangle GetCollisionRec(Rectangle rec1, Rectangle rec2) { Rectangle retRec = { 0, 0, 0, 0 }; |
