(look at title)It is really getting getting on my nerves

# How do you do distance away like magmaPOP did in his minecraft game?

**Stampys_fans**#3

Thanks thoigh I meant ummm.... How do I explain .... I know, e.g. So the music gets slower when you are closer to steve

**oio**#6

I have not looked at the code and probably will not. It is trivial, however, to get distance in 2D space, if that's what you want.

Every object has a position in x and y. Any two objects not at the same point can be connected by a line segment whose length is the distance between the two points. That line segment is the hypotenuse of a right triangle. For that reason, the distance formula will look very familiar to you, if you know the Pythagorean theorem. And if you dont... no problem. Just use this:

Let x1 and y1 be the coordinates of object 1

Let x2 and y2 be the coordinates of object 2

The distance between object 1 and object 2 is the square root of the sum of the squares of the differences between their two x coordinates and their two y coordinates. In text, that looks like this:

Distance = SquareRoot( (x2-x1)^2 + (y2-y1)^2 )

**oio**#8

Hey, guys, just for fun we could make a little piece of code that makes the noise faster or slower based upon how closely we position two objects on the screen with our fingers. Would that be helpful? I could make and publish something in the next few minutes. Or not... If that's not interesting. Or, maybe you would like to do it? Just let me know, if I can help somehow.

**oio**#10

Sure. Not much of a game, really. But something that others can learn from. I can make something, when I get finished helping a family member with his python questions. Of course, I'm sure that you or just about anybody else can make your own version that is just as good. Either way, I will publish something in just a few minutes, after I'm done with this python thing.

**Valgo**#11

Turning a negative value into its respective positive value is called Absolute Value. Although hopscotch doesn't have a direct operation for it, you can still easily get an absolute value. Just do this:

(x^2)

**Stradyvarious**#14

There's a problem. When I click the link and tap the play arrow it should open with the Hopscotch app.

Instead it's opening with the safari browser and there's no sound and a blank white screen.

@Ian @Liza

**Stradyvarious**#16

Was this made with the Beta or the Latest Hopscotch app update.

A link @Phase_Studios once placed was made with the beta and worked through the IE browser when clicked. Other projects with the latest Hopscotch app didn't run through the browser and a link to itunes came up,advertising Hopscotch.

**t1_hopscotch**#18

Hello, this topic is from a while back but it's still something we keep using I was originally going to post a new topic but I thought why not add to this one but in some extra detail. This is thanks to evanmstice, for wanting to understand and find out more and of course it's for everyone to look at

This is for working through bit by bit, so take it easy and slow, and with time it will become clearer too the more you see it

So what we are trying to do is work out the distance between two objects. This is using Pythagoras' theorem.

**Pythagoras' theorem:**

When you have a triangle with a 90 degree angle, squaring the two shorter sides and adding them together gives the square of the longest side.

The longest side is always the one that is opposite to the 90 degree angle.

Here is a great video on Pythagoras' theorem (there are some extra exercises and examples available on the site too) but you can leave it if you feel okay with the concept above too or are short on time: Intro to the Pythagorean Thoerem from Khan Academy

**Using Pythagoras' theorem to find the distance between two points:**

Say we have two points on a grid and we would like to know how far away they are from each other:

If we make a right-angled triangle, it means we can use Pythagoras's theorem The simplest way to do it is draw two sides that are in the X and Y direction since the X and Y axes are at right angles to each other:

Now the distance we want to find is a side of a right-angled triangle. We just need the lengths of the other sides and then we can use Pythagoras' theorem to find the length we want.

The green side of the triangle is in the X direction, and looking along the X axis, we can see it is the difference between the X coordinates of the two points.

```
Length of green side:
= (X coordinate of one point) - (X coordinate of other point)
// You can choose whichever order you want the points in the next step
= (X coordinate of yellow point) - (X coordinate of black point)
= 16 - 8
= 8
```

## (Why the order doesn't matter in the third line)

The order doesn't matter because it just changes whether the answer will be positive or negative. And that won't matter because when you square it in Pythagoras' theorem, it will end up being positive.

Similarly for the grey side of the triangle, its length is the change in the Y coordinates of the two points.

```
Length of grey side:
= (Y coordinate of one point) - (Y coordinate of other point)
= (Y coordinate of yellow point) - (Y coordinate of black point)
= 15 - 9
= 6
```

Now we have the lengths of two sides, let's find out the length of the third side (here it is the longest one).

Using Pythagoras' theorem:

c^{2} = a^{2} + b^{2}`c is the longest side, which we want.`

c^{2} = 8^{2} + 6^{2}`The lengths of the other two sides are 8 and 6 so let's put them in.`

c^{2} = 64 + 36`8 squared is 64, and 6 squared is 36. (Even if you had negative lengths here, they will now be positive.)`

c^{2} = 100`Square root both sides, and since we are looking at a positive distance:`

c = 10

So the orange side of that triangle has length 10. This means the distance between the black and the yellow point is 10

Here is a video explaining these ideas too: Distance formula from Khan Academy

**How we can apply it in Hopscotch:**

This particular case is for figuring the distance between a circle object and a finger touching the screen, but you can apply it to anything

The circle's coordinates are its X and Y position, and the coordinates of a finger on the screen are `last touch X`

and `last touch Y`

.

Let's make a right-angled triangle again:

Working out its side lengths are the same as before (I used the same colours to show the similarity)

`Length of green side (difference/change in X coordinates of points):`

```
= (X coordinate of one point) - (X coordinate of other point)
// Again you can choose whichever order you want the points in the next step
= (X coordinate of finger) - (X coordinate of circle)
= last touch x - circle x position
```

`Length of grey side (change in Y coordinates of points):`

```
= (Y coordinate of one point) - (Y coordinate of other point)
= (Y coordinate of finger) - (Y coordinate of circle)
= last touch y - circle y position
```

Here is how it looks in the diagram and in Hopscotch:

In Hopscotch:

And now to work out the orange side of the triangle, which is the distance between the finger and the circle.

Using Pythagoras's theorem again:

c^{2} = a^{2} + b^{2}

The two side lengths this time are `last touch x - circle x`

and `last touch y - circle y`

. And c is the distance between the circle and finger, so let's call it that:

`Distance`

^{2} = `(last touch x - circle x)`

^{2} + `(last touch y - circle y)`

^{2}

To get `Distance`

on its own, we can square root both sides. This gives us:

`Distance`

= √ ( `(last touch x - circle x)`

^{2} + `(last touch y - circle y)`

^{2} )

And that gives you the distance between the circle and a finger touching the screen

In Hopscotch it looks like this,when you split it up:

In our case, we wanted to find if the distance between the finger and the circle was less than 25. This is because if your finger is close to the circle, it means that you've just tapped that circle and not a different one.

So that is how we ended up with this :

Because the objects are clones however, we can't use a value to store the information about the Change in X, Change in Y and Distance because the value will end up being the same for all the clones — this is because Hopscotch doesn't have separate values for clones.

That's why it was originally in one line and it looks pretty long, but it is the same thing so don't worry when you see something like this:

Like everything, it is just made up of smaller parts

Whew, that was pretty long, but don't worry — remember, take it bit by bit. If you have any questions or suggestions please feel very free to share

Tangent Tutorial

**Snoopy**#19

Wow! I never thought I'd actually use that theorem for anything!

I learned that last year, and the thing I thought was, "I'm not going to use this for anything."

But that makes sense how we can apply it!

Thanks for posting that!

**t1_hopscotch**#20

Glad to hear it, @Snoopy! Thank you for sharing that Hopscotch is lovely for showing us new ways to use what we learn!

It's same the same here, when we did sine and cosine in my classes I hear people saying "when are we ever going to use this?" and yet you see on Hopscotch people making all sorts of fun stuff with them all the time If only they could get to try this themselves...