Discuss Scratch

NamePending_
Scratcher
100+ posts

Detecting distance in a specific direction.

Hi, how can I make it so that a sprite can detect the distance to another sprite, but ONLY in one direction?
This block
(distance to [mouse-pointer v])
will detect the shortest possible distance to a sprite, in any direction. To go in a specific direction, I've used this script under “run without screen refresh”, but it lags the project IMMENSELY.
set [distance v] to (0)
repeat until <touching [sprite v]>
move (1) steps
change [distance v] by (1)
If anyone can help me with this, it would be appreciated a lot. Thanks

Welcome to my signature!
Was this post helpful? If so, please consider checking out my profile!
say pasta in it please
100 POSTS YESSIRRRRRRRR
EDawg2011
Scratcher
1000+ posts

Detecting distance in a specific direction.

Make the costume that calculates the distance REALLY wide, like, so wide that it's 1000 or more pixels in length.

Please note that I may sometimes make a mistake and give wrong information.

Can you please put this at/near the top of your signature and tell people that tag spam isn't allowed and it manipulates the algorithm, to start a chain and spread the word? https://scratch.mit.edu/discuss/topic/747346/?page=1#post-7864829 - Thanks, @EDawg2011.

But then I had a very good idea. I used F5. See, using F5 gave me a whole new perspective and I was able to see a chest I couldn't have seen before.

(Highlight text + down arrow + shift to see the rest of my signature.)


Help find out who ate @cheddargirl's signature! l me when i accidentally spread misinformation l Platformer Skibidi





<0-0::sensing>//This is Charles; he protects my signature from evil kumquats.
when I'm spawned::events hat//This is the code Charles' brain runs on.
forever
if <[100] > (distance to [an evil kumquat v])> then
delete the evil kumquat::control
end
end











Be moist.















-JS_Test-
Scratcher
53 posts

Detecting distance in a specific direction.

NamePending_ wrote:

Hi, how can I make it so that a sprite can detect the distance to another sprite, but ONLY in one direction?
This block
(distance to [mouse-pointer v])
will detect the shortest possible distance to a sprite, in any direction. To go in a specific direction, I've used this script under “run without screen refresh”, but it lags the project IMMENSELY.
set [distance v] to (0)
repeat until <touching [sprite v]>
move (1) steps
change [distance v] by (1)
If anyone can help me with this, it would be appreciated a lot. Thanks

Of course this causes the project to lag immensely, you forgot to point this sprite towards the other sprite, so they will never touch each other, meaning that the loop under “run without screen refresh” is infinite. In other words, you just turned your computer into a particle accelerator.

Make this sprite point in the direction of that sprite before the loop begins. Or during the loop. Whatever you prefer.

Last edited by -JS_Test- (Feb. 4, 2024 19:57:35)


Main account: @JS_Coder
Malicondi
Scratcher
1000+ posts

Detecting distance in a specific direction.

NamePending_ wrote:

Hi, how can I make it so that a sprite can detect the distance to another sprite, but ONLY in one direction?
Hi, this is really simple, just make a formula for distance rather than moving towards it: here it is
distance = sqrt of( (x1 - x2)^2 + (y1 - y2)^2))
In scratch blocks this is
Set [distance v] to ([sqrt v] of (((x - x2) * (x -x2)) + ((y - y2) * (y - y2)))

//x and y would be the sprites positions, x2 and y2 would be the target obj's positions

Last edited by Malicondi (Feb. 4, 2024 20:15:11)


post #1000 post #100 i help in the forums post #1 post #500 0 second ninja
I recommend reading jvvg's essay about the scratch team before complaining, as it may change your opinion and provide insight on the topic.

coming soon :)


NamePending_
Scratcher
100+ posts

Detecting distance in a specific direction.

-JS_Test- wrote:

NamePending_ wrote:

Hi, how can I make it so that a sprite can detect the distance to another sprite, but ONLY in one direction?
This block
(distance to [mouse-pointer v])
will detect the shortest possible distance to a sprite, in any direction. To go in a specific direction, I've used this script under “run without screen refresh”, but it lags the project IMMENSELY.
set [distance v] to (0)
repeat until <touching [sprite v]>
move (1) steps
change [distance v] by (1)
If anyone can help me with this, it would be appreciated a lot. Thanks

Of course this causes the project to lag immensely, you forgot to point this sprite towards the other sprite, so they will never touch each other, meaning that the loop under “run without screen refresh” is infinite. In other words, you just turned your computer into a particle accelerator.

Make this sprite point in the direction of that sprite before the loop begins. Or during the loop. Whatever you prefer.
oh yeah im stupid, my bad

Welcome to my signature!
Was this post helpful? If so, please consider checking out my profile!
say pasta in it please
100 POSTS YESSIRRRRRRRR
candymana11
Scratcher
61 posts

Detecting distance in a specific direction.

You could do something like this:
set [distance on x axis v] to ([abs v] of (([x position v] of [Sprite1 v]) - ([x position v] of [Sprite2 v])))

The
([abs v] of ())
block makes it always a positive number. If you want to have it be positive or negative, just take that out and only have the subtraction

when green flag clicked
forever
imagine::motion
program::sound
share::operators

Scratch on everybody!!
AHypnoman
Scratcher
1000+ posts

Detecting distance in a specific direction.

To get the x or y difference/delta between two sprites you can use this:

([abs v] of ((x position) - ([x position v] of [target sprite v])))

Swap out X for Y as needed.

~ AHypnoman (Use Shift+Down Arrow to see the rest of my signature)







I am an evil kumquat, and your siggy looks tasty…
Hello there, I'm AHypnoman, If you write my name remember to capitalize the “H”. “When you don't create things, you become defined by your tastes rather than ability. your tastes only narrow & exclude people. so create.” I write though as tho.
According to the Chrome Console, my viewbox attribute is “Garbage”. Very nice, Chrome.
#TrendyHashtag
Gib internneeet

Play Retorquebit!


Powered by DjangoBB