diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2024-10-28 11:22:52 -0400 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2024-10-28 11:22:52 -0400 |
| commit | 36623f17ca2194fbb614a3cbce6b887734418f06 (patch) | |
| tree | a3e98ed93361be3a3c8eff02c957d08fe5cd4775 /balls.cl | |
| parent | 93a3f005480effe48513e7bc81161db1adc43f32 (diff) | |
| download | balls-36623f17ca2194fbb614a3cbce6b887734418f06.zip | |
collideWalls
Diffstat (limited to 'balls.cl')
| -rw-r--r-- | balls.cl | 49 |
1 files changed, 49 insertions, 0 deletions
@@ -1,5 +1,24 @@ #define RADIUS 0.15f +float +min(float a, float b) { + if (a < b) + return a; + return b; +} + +float +max(float a, float b) { + if (a > b) + return a; + return b; +} + +float +clamp(float x, float lo, float hi) { + return min(hi, max(x, lo)); +} + __kernel void move(__global float2 *positions, __global float2 *velocities) { size_t id; @@ -9,6 +28,36 @@ move(__global float2 *positions, __global float2 *velocities) { } __kernel void +collideWalls(__global float2 *positions, __global float2 *velocities) { + float2 min, max, p, v; + size_t id; + + /* Set bounds. */ + min.x = -1.0 + RADIUS; + min.y = -1.0 + RADIUS; + max.x = 1.0 - RADIUS; + max.y = 1.0 - RADIUS; + + id = get_global_id(0); + p = positions[id]; + v = velocities[id]; + + /* Check for collision with bounds. */ + if (p.x <= min.x || p.x >= max.x) { + p.x = clamp(p.x, min.x, max.x); + v.x = -v.x; + } + if (p.y <= min.y || p.y >= max.y) { + p.y = clamp(p.y, min.y, max.y); + v.y = -v.y; + } + + /* Write back. */ + positions[id] = p; + velocities[id] = v; +} + +__kernel void genVertices(__global float2 *positions, __global float2 *vertices) { size_t ball, nsegs; float2 center; |