# How do I detect if two ellipses are colliding?

**URL:** <https://discourse.processing.org/t/how-do-i-detect-if-two-ellipses-are-colliding/18603>\
**Category:** Coding Questions\
**Created:** [March 11, 2020, 4:23pm UTC](https://discourse.processing.org/t/how-do-i-detect-if-two-ellipses-are-colliding/18603 "2020-03-11T16:23:26Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yorunoshi](https://avatars.discourse-cdn.com/v4/letter/y/3be4f8/32.png) [@Yorunoshi](https://discourse.processing.org/u/Yorunoshi)\
**Post date:** [March 11, 2020, 4:23pm UTC](https://discourse.processing.org/t/how-do-i-detect-if-two-ellipses-are-colliding/18603/1 "2020-03-11T16:23:26Z")

</div>

Hi I tried using the distance command and the coordinates of 2 ellipses to detect if they are colliding.  
But the programm uses the start coordinates and not the coordinates after they moved.  
Is there another possibility to detect if things are colliding?  
What could be the problem if my coordinates are not updated?

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 11, 2020, 8:08pm UTC](https://discourse.processing.org/t/how-do-i-detect-if-two-ellipses-are-colliding/18603/2 "2020-03-11T20:08:17Z")

</div>

Excuse me, could you post your code please?

Because when your ellipses move you must have the coordinates in some variables. Maybe other than the initial variable.

For dist() use the correct variables you also use to display the ellipses

---

<div class="post-metadata">

**Author:** ![Yorunoshi](https://avatars.discourse-cdn.com/v4/letter/y/3be4f8/32.png) [@Yorunoshi](https://discourse.processing.org/u/Yorunoshi)\
**Post date:** [March 11, 2020, 8:57pm UTC](https://discourse.processing.org/t/how-do-i-detect-if-two-ellipses-are-colliding/18603/3 "2020-03-11T20:57:50Z")

</div>

Thats the 2 classes I use for the ellipses and in the second one is also the part for the collision: [https://codeshare.io/2K1MAY](https://codeshare.io/2K1MAY)

I also tried using new variales bur they just would´nt get updated.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [March 12, 2020, 3:18am UTC](https://discourse.processing.org/t/how-do-i-detect-if-two-ellipses-are-colliding/18603/4 "2020-03-12T03:18:59Z")

</div>

> **[Collision Detection](http://jeffreythompson.org/collision-detection/circle-circle.php)**
>
> An online book about collision detection using Processing.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 12, 2020, 8:16am UTC](https://discourse.processing.org/t/how-do-i-detect-if-two-ellipses-are-colliding/18603/5 "2020-03-12T08:16:26Z")

</div>

Control your code whether the variables you use to display you also use to check the distance

---

<div class="post-metadata">

**Author:** ![Yorunoshi](https://avatars.discourse-cdn.com/v4/letter/y/3be4f8/32.png) [@Yorunoshi](https://discourse.processing.org/u/Yorunoshi)\
**Post date:** [March 12, 2020, 11:00am UTC](https://discourse.processing.org/t/how-do-i-detect-if-two-ellipses-are-colliding/18603/6 "2020-03-12T11:00:29Z")

</div>

I´ve checked my code several times and now the processing tells me that the class i try to reference does not exist. The variables i use for dist() are the exact ones i have to use and are not misspelled.

[https://www.codepile.net/pile/gn7bJGQA](https://www.codepile.net/pile/gn7bJGQA)

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 16, 2020, 11:39am UTC](https://discourse.processing.org/t/how-do-i-detect-if-two-ellipses-are-colliding/18603/7 "2020-03-16T11:39:11Z")

</div>

I can’t run your code.

The Brain class is missing?

---

<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:** [March 23, 2020, 3:44am UTC](https://discourse.processing.org/t/how-do-i-detect-if-two-ellipses-are-colliding/18603/8 "2020-03-23T03:44:38Z")

</div>

Are your ellipses actually circles, or are they elliptical?

Ellipse-ellipse collision detection can be wickedly hard. Axis-aligned ellipses are a bit better, but still awful. It is worth doing almost anything else to avoid the math

> **The math: ellipse-ellipse collision**
>
> [Ellipse–circle and ellipse–ellipse collision detection – iki.fi/o](http://yehar.com/blog/?p=2926)
> 
> > x = c_b^2/(2_(b^2 - 1)) - 1/2_sqrt(c^2_b^4/(b^2 - 1)^2 + (c^2_b^4 - b^4 + d^2_b^2 + 2_b^2 - 1)/(3_(b^4 - 2_b^2 + 1)) - (c^2_b^4 - b^4 + d^2_b^2 + 2_b^2 - 1)/(b^2 - 1)^2 + (- 108_c^2_(b^4_c - b^2_c)^2_b^4 + 72_(b^4 - 2_b^2 + 1)c^2(c^2_b^4 - b^4 + d^2_b^2 + 2_b^2 - 1)_b^4 + 2_(c^2_b^4 - b^4 + d^2_b^2 + 2_b^2 - 1)^3 + 108_(b^4 - 2_b^2 + 1)_(b^4_c - b^2_c)^2 + 36\*(b^4_c - b^2_c)^2\*(c^2_b^4 - b^4 + d^2_b^2 + 2_b^2 - 1) + sqrt((- 108_c^2\*(b^4_c - b^2_c)^2_b^4 + 72_(b^4 - 2_b^2 + 1)c^2(c^2_b^4 - b^4 + d^2_b^2 + 2_b^2 - 1)_b^4 + 2_(c^2_b^4 - b^4 + d^2_b^2 + 2_b^2 - 1)^3 + 108_(b^4 - 2_b^2 + 1)_(b^4_c - b^2_c)^2 + 36\*(b^4_c - b^2_c)^2\*(c^2_b^4 - b^4 + d^2_b^2 + 2_b^2 - 1))^2 - 4_(- 12\*(b^4 - 2_b^2 + 1)c^2b^4 + 12_(b^4_c - b^2_c)^2 + (c^2_b^4 - b^4 + d^2_b^2 + 2_b^2 - 1)^2)^3))^(1/3)/(3_2^(1/3)_(b^2 - 1)^2) + 2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2 + 36\*(b^4_c - b^2_c)^2\*(c^2_b^4 - b^4 + d^2_b^2 + 2_b^2 - 1) + sqrt((- 108_c^2\*(b^4_c - b^2_c)^2_b^4 + 72_(b^4 - 2_b^2 + 1)c^2(c^2_b^4 - b^4 + d^2_b^2 + 2_b^2 - 1)_b^4 + 2_(c^2_b^4 - b^4 + d^2_b^2 + 2_b^2 - 1)^3 + 108_(b^4 - 2_b^2 + 1)_(b^4_c - b^2_c)^2 + 36\*(b^4_c - b^2_c)^2\*(c^2_b^4 - b^4 + d^2_b^2 + 2_b^2 - 1))^2 - 4_(- 12\*(b^4 - 2_b^2 + 1)c^2b^4 + 12_(b^4_c - b^2_c)^2 + (c^2_b^4 - b^4 + d^2_b^2 + 2\*b^2 - 1)^2)^3))^(1/3)))))

– options include scaling the coordinate axes to make them circle-circle, for example, or creating a good-enough convex hull and doing polygon-polygon, or creating a buffer and using picking e.g. with the Picking library.
