# Distance between two objects

**URL:** https://discourse.processing.org/t/distance-between-two-objects/30751
**Category:** Coding Questions
**Tags:** homework
**Created:** [June 17, 2021, 1:54am UTC](https://discourse.processing.org/t/distance-between-two-objects/30751 "2021-06-17T01:54:37Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![MacJ](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@MacJ](https://discourse.processing.org/u/MacJ)
#### Post date: [June 17, 2021, 1:54am UTC](https://discourse.processing.org/t/distance-between-two-objects/30751/1 "2021-06-17T01:54:37Z")

</div>

Hello i want to calculate the distance between the centre of Pac-Man and the centre of the treat and If this distance is less than the radius of Pac-Man, we will consider Pac-Man to have “eaten” the treat. When Pac-Man has “eaten” the treat, make the treat reappear in a new random location

```auto
void generateTreat() { 
  treatX = random(width-1);
  treatY = random(height-1);
}

void drawTreat() {
  circle(treatX, treatY, TREAT_SIZE);
}

void eatTreat() {
  float distance = sqrt(sq(pacmanY-pacmanX)+sq(treatY-treatX));
  if (distance < PACMAN_DIAMETER) {
    circle(treatX, treatY, TREAT_SIZE);
  }
}

```

Thanks for the help

---

<div class="post-metadata">

### Author: ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)
#### Post date: [June 17, 2021, 2:59am UTC](https://discourse.processing.org/t/distance-between-two-objects/30751/2 "2021-06-17T02:59:21Z")

</div>

Draw yourself a diagram! Heck, I’ll draw you one!

 ![PacHelp001](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/5/5c8e2a834e2a40dab980cc2869f680750b3a20d6.png)

To work out the (purple) distance, you need to take the square root of the sum of the squares of the change in the X position (blue) and Y positions (green).

How can you work out the lengths of the blue and green lines?

Compare that with what you’re doing! Do you see the problem?

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [June 17, 2021, 10:27am UTC](https://discourse.processing.org/t/distance-between-two-objects/30751/3 "2021-06-17T10:27:23Z")

</div>

Hello,

You may have to start with some basics first…

There are lots of resources (tutorials, references, examples, books, etc.) here:  
_[processing.org](http://processing.org/)_

Check out the examples that come with Processing.  
These are in the menu under _File \> Examples…\>_ :

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/3/35f05e0e0227932c25f0c7f86269d86609c2c294.png)

Also check out YouTube for the [Coding Train](https://www.youtube.com/channel/UCvjgXvBlbQiydffZU7m1_aw).

Functions:  
[https://www.youtube.com/watch?v=zBo2D3Myo6Q](https://www.youtube.com/watch?v=zBo2D3Myo6Q)  
[https://happycoding.io/tutorials/processing/creating-functions](https://happycoding.io/tutorials/processing/creating-functions)

Try it without functions first and once that is working then add functions.

> **Template with setup() and draw() \< Click to open**
>
> ```auto
> // Variable declarations
> // https://processing.org/examples/variablescope.html
> 
> int number;
> 
> // setup() runs only once
> //https://processing.org/reference/setup_.html
> void setup() 
> {
> size(200, 200);
> // Code here...
> }
> 
> // draw() continuously loops
> // https://processing.org/reference/draw_.html
> void draw() 
> {
> background(0);
> // Code here...
> }
> 
> ```

I do not know what the scope of your _homework_ is… there may be a function already available to do this.

_You have to do this work and find it in the available references._

I recommend you write your own function to learn how to do this.

`:)`

---

<div class="post-metadata">

### Author: ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)
#### Post date: [June 17, 2021, 5:25pm UTC](https://discourse.processing.org/t/distance-between-two-objects/30751/4 "2021-06-17T17:25:40Z")

</div>

Hello @MacJ

This Abe Pazos tutorial may be of help. There is a built-in distance function in processing – **dist()** – that I think will make your task easier.  
Do not get distracted by the use of noise() in the tutorial, just focus on the implementation of dist().

Since you don’t show setup() or draw() in your code, I’m not sure what your overall thought process is… but provided you’ve worked that all out…

https://www.youtube.com/embed/N5Ka4C3RVmw?feature=oembed&wmode=opaque&list=PL632BB8C3F7E776BA&start=47

Good luck!  
🤓
