# Optimizing collision detection and resolution between shapes

**URL:** <https://discourse.processing.org/t/optimizing-collision-detection-and-resolution-between-shapes/41104>\
**Category:** Coding Questions\
**Created:** [March 1, 2023, 2:39pm UTC](https://discourse.processing.org/t/optimizing-collision-detection-and-resolution-between-shapes/41104 "2023-03-01T14:39:21Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![bdooley](https://avatars.discourse-cdn.com/v4/letter/b/7cd45c/32.png) [@bdooley](https://discourse.processing.org/u/bdooley)\
**Post date:** [March 1, 2023, 2:39pm UTC](https://discourse.processing.org/t/optimizing-collision-detection-and-resolution-between-shapes/41104/1 "2023-03-01T14:39:21Z")

</div>

Hi all,

I am working on a sketch with falling rectangles and circles that collide with each other, the left and right walls, and ground.

[https://editor.p5js.org/bdooley7/sketches/WXa5AQgKD](https://editor.p5js.org/bdooley7/sketches/WXa5AQgKD)

The collision detection seems mostly resolved, however when shapes do overlap, whether it be when they are added to the sketch in an already overlapping position, or are pushed together later when there are many colliding shapes, sometimes it takes a few seconds before the collision is resolved. Does this have to do with lag or with collision detection and resolution being carried out of the circle and rectangle classes instead of in a class of its own?

I would greatly appreciate any help!!

All best,  
Ben
