09-18-2025, 08:53 PM
That really made me want to dig into collisions.
1st challenge: handle collisions for objects that are longer than they are wide.
2nd challenge: detect collisions before they happen using ray tracing.
But first, a short debrief of how my proximity-and-mask-overlap collision detection code works.
- All objects (shapes) are built to fit inside a circle with a predefined maximum radius.
- Space is divided into square areas whose side length equals the maximum radius.
- At any given moment an object will overlap at least 1 area and at most 9 areas.
- If an area is overlapped by several objects, the distance between the centers of those objects is computed pairwise.
- If the calculated distance is less than or equal to 2 times the maximum radius, the masks of the two objects are copied into an image buffer the size of the area, placed according to their actual positions relative to that area.
- Since the masks are semi‑transparent, overlapping pixels become opaque.
- If the resulting image buffer contains at least one opaque pixel, a collision is confirmed for both objects.
- Pairs of objects already detected as colliding do not need to be detected again.
I made two improvements (code updated in post #21):
1. Because an object’s mask must be recalculated every cycle due to the object rotating, the mask is now recomputed for an object only when a collision is suspected.
2. For better performance, detection precision can now be reduced by changing mask size with the PIXELS_PRECISION parameter. A mask’s area is divided by PIXELS_PRECISION squared.
1st challenge: handle collisions for objects that are longer than they are wide.
2nd challenge: detect collisions before they happen using ray tracing.
But first, a short debrief of how my proximity-and-mask-overlap collision detection code works.
- All objects (shapes) are built to fit inside a circle with a predefined maximum radius.
- Space is divided into square areas whose side length equals the maximum radius.
- At any given moment an object will overlap at least 1 area and at most 9 areas.
- If an area is overlapped by several objects, the distance between the centers of those objects is computed pairwise.
- If the calculated distance is less than or equal to 2 times the maximum radius, the masks of the two objects are copied into an image buffer the size of the area, placed according to their actual positions relative to that area.
- Since the masks are semi‑transparent, overlapping pixels become opaque.
- If the resulting image buffer contains at least one opaque pixel, a collision is confirmed for both objects.
- Pairs of objects already detected as colliding do not need to be detected again.
I made two improvements (code updated in post #21):
1. Because an object’s mask must be recalculated every cycle due to the object rotating, the mask is now recomputed for an object only when a collision is suspected.
2. For better performance, detection precision can now be reduced by changing mask size with the PIXELS_PRECISION parameter. A mask’s area is divided by PIXELS_PRECISION squared.

