diff options
Diffstat (limited to 'balls.cl')
| -rw-r--r-- | balls.cl | 15 |
1 files changed, 8 insertions, 7 deletions
@@ -1,6 +1,7 @@ #define G_FACTOR 3600.0 #define G (9.81 / G_FACTOR) #define DENSITY 1500.0 +#define EPSILON 1e-8 /* Account for FP rounding error. */ float mass(float radius); int isCollision(float2 p1, float r1, float2 p2, float r2); @@ -33,10 +34,10 @@ collideWalls(__global float2 *positions, __global float2 *velocities, __global f r = radii[id]; /* Set bounds. */ - min.x = -1.0 + r; - min.y = -1.0 + r; - max.x = 1.0 - r; - max.y = 1.0 - r; + min.x = -1.0 + r + EPSILON; + min.y = -1.0 + r + EPSILON; + max.x = 1.0 - r - EPSILON; + max.y = 1.0 - r - EPSILON; /* Check for collision with bounds. */ if (p.x <= min.x || p.x >= max.x) { @@ -122,7 +123,7 @@ isCollision(float2 p1, float r1, float2 p2, float r2) { float rhs; dist = p1 - p2; - rhs = r1 + r2; + rhs = r1 + r2 + EPSILON; return (dist.x*dist.x + dist.y*dist.y) <= rhs*rhs; } @@ -133,8 +134,8 @@ setPosition(float2 *p1, float r1, float2 *p2, float r2) { mid = (*p1 + *p2) / 2.0f; n = unitNorm(*p2 - *p1); - *p1 = mid - n*r1; - *p2 = mid + n*r2; + *p1 = mid - (n*r1 + EPSILON); + *p2 = mid + (n*r2 + EPSILON); } /* Return the velocity of ball 1 after colliding with ball 2. */ |