# Collision Detection (Game Design)

**URL:** <https://discourse.processing.org/t/collision-detection-game-design/24142>\
**Category:** Coding Questions\
**Created:** [September 26, 2020, 8:20pm UTC](https://discourse.processing.org/t/collision-detection-game-design/24142 "2020-09-26T20:20:19Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [September 29, 2020, 9:35pm UTC](https://discourse.processing.org/t/collision-detection-game-design/24142/3 "2020-09-29T21:35:18Z")

</div>

> [@Aoyanco](#):
>
> (Basically, I’m going to need over 500 rectangles). Is there a relatively clean way of doing this so it won’t lag?

How to optimize depends on the details. Are they all moving every frame? Are any limited to specific regions, such that they could never interact with each other?

In the worst case, if you have 500 rectangles bouncing at super-speed around a space, then you would need to do about 500\*499 = 249,000 [rectRect](http://www.jeffreythompson.org/collision-detection/rect-rect.php) collision checks per frame.

You may be interested in @Kevin 's page – particularly the last two sections on many objects and “grid-based” detection.

> **[Processing Collision Detection](https://happycoding.io/tutorials/processing/collision-detection)**
>
> Learn how to trigger an action when objects overlap in Processing!

A common advanced approach is cell-space partitioning and specifically quad trees. Here are two past discussions that get into the details:

- [Implementing Grid-Based Collision System](https://discourse.processing.org/t/implementing-grid-based-collision-system/3248)
- [Quadtree vs grid](https://discourse.processing.org/t/quadtree-vs-grid/13844)

In particular, @quark’s QuadTreeDemo is great to play around with to understand what some of the tradeoffs are in trying to decide how to partition space based on the number and size of your objects.

---

_[View the full topic](https://discourse.processing.org/t/collision-detection-game-design/24142)._
