# Using dist(); for collision detection

**URL:** https://discourse.processing.org/t/using-dist-for-collision-detection/40050
**Category:** Coding Questions
**Tags:** homework
**Created:** [December 7, 2022, 11:33am UTC](https://discourse.processing.org/t/using-dist-for-collision-detection/40050 "2022-12-07T11:33:32Z")
**Posts on this page:** 1
**Showing post:** 2

<div class="post-metadata">

### Author: ![sableraph](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sableraph/32/251_2.png) [@sableraph](https://discourse.processing.org/u/sableraph)
#### Post date: [December 7, 2022, 11:42am UTC](https://discourse.processing.org/t/using-dist-for-collision-detection/40050/2 "2022-12-07T11:42:54Z")

</div>

The problem with your code is that you are trying to pass in an array as a parameter to the “dist()” function, but the “dist()” function expects individual x and y coordinates as parameters. To fix this issue, you can simply use the values from the x and y arrays in the “dist()” function instead of passing in the entire array.

For example, to calculate the distance between the first and second ellipse in your array, you can use the following code:

```auto
if(dist(x[0], y[0], x[1], y[1]) < r)

```

You can then use a loop to check the distance between each ellipse in the array and apply the necessary changes to the x and y speeds if the distance is less than the radius.

_Note: part of this message was generated with ChatGPT_

---

_[View the full topic](https://discourse.processing.org/t/using-dist-for-collision-detection/40050)._
