Discuss Scratch

UndeadSorcerer
Scratcher
100+ posts

Distance to (x) (y)

For my tower defense game I think this will help. It might be useful for other things too!

(distance to x:() y:()::sensing)

Tell me if you support!

Also if there is any way to get around this please tell me.
stickfiregames
Scratcher
1000+ posts

Distance to (x) (y)

Support, the workaround is very long:
([sqrt v] of ((((x :: grey) - (x position)) * ((x :: grey) - (x position))) + (((y :: grey) - (y position)) * ((y :: grey) - (y position))))
CatsUnited
Scratcher
1000+ posts

Distance to (x) (y)

Support
zacadoole1
Scratcher
100+ posts

Distance to (x) (y)

stickfiregames wrote:

Support, the workaround is very long:
([sqrt v] of ((((x :: grey) - (x position)) * ((x :: grey) - (x position))) + (((y :: grey) - (y position)) * ((y :: grey) - (y position))))
Support, but for an alternate workaround couldn't you just have a hidden sprite at the location you want to find the distance from, then use the "distance to " block?

Last edited by zacadoole1 (April 23, 2015 18:46:24)

stickfiregames
Scratcher
1000+ posts

Distance to (x) (y)

zacadoole1 wrote:

stickfiregames wrote:

Support, the workaround is very long:
([sqrt v] of ((((x :: grey) - (x position)) * ((x :: grey) - (x position))) + (((y :: grey) - (y position)) * ((y :: grey) - (y position))))
Support, but for an alternate workaround couldn't you just have a hidden sprite at the location you want to find the distance from, then use the "distance to " block?
You could, but then of course you need another sprite and have to use broadcasts.

zacadoole1
Scratcher
100+ posts

Distance to (x) (y)

stickfiregames wrote:

zacadoole1 wrote:

stickfiregames wrote:

Support, the workaround is very long:
([sqrt v] of ((((x :: grey) - (x position)) * ((x :: grey) - (x position))) + (((y :: grey) - (y position)) * ((y :: grey) - (y position))))
Support, but for an alternate workaround couldn't you just have a hidden sprite at the location you want to find the distance from, then use the "distance to " block?
You could, but then of course you need another sprite and have to use broadcasts.

Yeah, but it's easier for me to remember than all those math blocks!
Prinseskat
Scratcher
1000+ posts

Distance to (x) (y)

zacadoole1 wrote:

stickfiregames wrote:

zacadoole1 wrote:

stickfiregames wrote:

Support, the workaround is very long:
([sqrt v] of ((((x :: grey) - (x position)) * ((x :: grey) - (x position))) + (((y :: grey) - (y position)) * ((y :: grey) - (y position))))
Support, but for an alternate workaround couldn't you just have a hidden sprite at the location you want to find the distance from, then use the "distance to " block?
You could, but then of course you need another sprite and have to use broadcasts.

Yeah, but it's easier for me to remember than all those math blocks!
Easy to remember if you know how it works.

lol the block's going out of the quote section

Last edited by Prinseskat (April 23, 2015 19:10:23)

zacadoole1
Scratcher
100+ posts

Distance to (x) (y)

Prinseskat wrote:

zacadoole1 wrote:

stickfiregames wrote:

zacadoole1 wrote:

stickfiregames wrote:

Support, the workaround is very long:
([sqrt v] of ((((x :: grey) - (x position)) * ((x :: grey) - (x position))) + (((y :: grey) - (y position)) * ((y :: grey) - (y position))))
Support, but for an alternate workaround couldn't you just have a hidden sprite at the location you want to find the distance from, then use the "distance to " block?
You could, but then of course you need another sprite and have to use broadcasts.

Yeah, but it's easier for me to remember than all those math blocks!
Easy to remember if you know how it works.
Oh, I could probably memorize it if I really wanted to, but you know what they say, you can't teach an old dog new tricks!
stickfiregames
Scratcher
1000+ posts

Distance to (x) (y)

zacadoole1 wrote:

Prinseskat wrote:

zacadoole1 wrote:

stickfiregames wrote:

zacadoole1 wrote:

stickfiregames wrote:

Support, the workaround is very long:
([sqrt v] of ((((x :: grey) - (x position)) * ((x :: grey) - (x position))) + (((y :: grey) - (y position)) * ((y :: grey) - (y position))))
Support, but for an alternate workaround couldn't you just have a hidden sprite at the location you want to find the distance from, then use the "distance to " block?
You could, but then of course you need another sprite and have to use broadcasts.

Yeah, but it's easier for me to remember than all those math blocks!
Easy to remember if you know how it works.
Oh, I could probably memorize it if I really wanted to, but you know what they say, you can't teach an old dog new tricks!
It would be easier if we had a power block too:
([sqrt v] of ((((x :: grey) - (x position)) ^ (2) :: operators) + (((y :: grey) - (y position)) ^ (2) :: operators))
Still long though.
Seapats
Scratcher
70 posts

Distance to (x) (y)

stickfiregames wrote:

Support, the workaround is very long:
([sqrt v] of ((((x :: grey) - (x position)) * ((x :: grey) - (x position))) + (((y :: grey) - (y position)) * ((y :: grey) - (y position))))

Yes I used this for a project this would really help!
MegaApuTurkUltra
Scratcher
1000+ posts

Distance to (x) (y)

Support. This would help new scratchers who don't want to use the “complicated” workaround.
Dobbatron
Scratcher
3 posts

Distance to (x) (y)

This needs the formula:
SQRT of Xposition of one sprite - X position of other sprite * Xposition of one sprite - X position of other sprite + yposition of one sprite - y position of other sprite * yposition of one sprite - y position of other sprite
Hope this helped!

Last edited by Dobbatron (Oct. 23, 2017 18:06:48)

Cub56
Scratcher
1000+ posts

Distance to (x) (y)

I won't support because this workaround is what's used in many other programming languages and it'd probably be a good thing for new Scratchers to learn the maths behind it.
Sigton
Scratcher
1000+ posts

Distance to (x) (y)

define distance to (x) (y)
set [dx v] to ((x) - (x position))
set [dy v] to ((y) - (y position))
set [distance v] to ([sqrt v] of (((dx) * (dx)) + ((dy) * (dy))))

A nice and neat custom block for you

Sigton
Charles12310
Scratcher
1000+ posts

Distance to (x) (y)

Cub56 wrote:

I won't support because this workaround is what's used in many other programming languages and it'd probably be a good thing for new Scratchers to learn the maths behind it.
So, how do they even know if that script is gonna work? They don't understand how it works, why should we be expecting users to solve complicated problems and they aren't even in high school?
walkcycle
Scratcher
500+ posts

Distance to (x) (y)

Cub56 wrote:

I won't support because this workaround is what's used in many other programming languages and it'd probably be a good thing for new Scratchers to learn the maths behind it.

I agree, it is a great opportunity to learn the math of distance.

Sigton wrote:

define distance to (x) (y)
set [dx v] to ((x) - (x position))
set [dy v] to ((y) - (y position))
set [distance v] to ([sqrt v] of (((dx) * (dx)) + ((dy) * (dy))))

A nice and neat custom block for you

Sigton
That's excellent.

Another work round

go to x: () y: () // sprite1

(distance to [sprite1 v]) // sprite2

Charles12310 wrote:

So, how do they even know if that script is gonna work?

By testing.
Charles12310
Scratcher
1000+ posts

Distance to (x) (y)

walkcycle wrote:

Cub56 wrote:

I won't support because this workaround is what's used in many other programming languages and it'd probably be a good thing for new Scratchers to learn the maths behind it.

I agree, it is a great opportunity to learn the math of distance.

Sigton wrote:

define distance to (x) (y)
set [dx v] to ((x) - (x position))
set [dy v] to ((y) - (y position))
set [distance v] to ([sqrt v] of (((dx) * (dx)) + ((dy) * (dy))))

A nice and neat custom block for you

Sigton
That's excellent.

Another work round

go to x: () y: () // sprite1

(distance to [sprite1 v]) // sprite2

Charles12310 wrote:

So, how do they even know if that script is gonna work?

By testing.
Yes but that's like saying the workaround is easy. -_-

The workaround is difficult, not everyone knows how it works, and I don't think everyone's gonna remember. I know about square roots but I can't even remember either. It's better to drag one block than to drag so many blocks and type so many stuff and then you realized that you forgot everything.
Cub56
Scratcher
1000+ posts

Distance to (x) (y)

Charles12310 wrote:

Cub56 wrote:

I won't support because this workaround is what's used in many other programming languages and it'd probably be a good thing for new Scratchers to learn the maths behind it.
So, how do they even know if that script is gonna work? They don't understand how it works, why should we be expecting users to solve complicated problems and they aren't even in high school?
It is only Pythagoras and I learnt it practically at the start of the equivalent of middle school.
Charles12310
Scratcher
1000+ posts

Distance to (x) (y)

Yes, but still you don't understand, the workaround is too long, it takes too much dragging of blocks, it's better to use one block alone. Most people probably aren't in middle school yet!

Powered by DjangoBB