Discuss Scratch

N0N4M3L0L
Scratcher
3 posts

Determine the direction between two coordinates

I have two coordinate pairs and i want to find the direction between them.
Cool_Dude2022
Scratcher
500+ posts

Determine the direction between two coordinates

N0N4M3L0L wrote:

I have two coordinate pairs and i want to find the direction between them.
I don't know what you mean by that, but I tried to take my best guess at it. Here:

https://scratch.mit.edu/projects/918152443/

Can you share the project and reply with it also?
MrDanglyLegs
Scratcher
100+ posts

Determine the direction between two coordinates

Try this script:

define Find direction from (x1) , (y1) to (x2) , (y2) :: custom
set [direction v] to ([atan v] of (([abs v] of ((x1) - (x2))) / ([abs v] of ((y1) - (y2))

Hope this helps.
N0N4M3L0L
Scratcher
3 posts

Determine the direction between two coordinates

thanks, this is very helpful. I am working on a project where a sprite will follow another sprite with a variable turn rate. I got a system that determines the coordinates behind the character but I needed help getting the follower to go to those coords. In this project the sprites always move forward.

Last edited by N0N4M3L0L (Nov. 17, 2023 20:31:39)

N0N4M3L0L
Scratcher
3 posts

Determine the direction between two coordinates

nvm it did not work, this is my project: https://scratch.mit.edu/projects/917146460/
medians
Scratcher
1000+ posts

Determine the direction between two coordinates

N0N4M3L0L wrote:

nvm it did not work, this is my project: https://scratch.mit.edu/projects/917146460/
The project is unshared.
-cloudcoding-
Scratcher
1000+ posts

Determine the direction between two coordinates

Take the square root of (x*x)+(y*y)
Jonablauwster
Scratcher
1 post

Determine the direction between two coordinates

what worked for me was making 2 lists for the clone x and y, and making the clones set the list to their positions, and making the seperate sprite go to those coordinates, then make your sprite point to the sprite.

like this:

when I start as a clone
add [X position] to [list clone x]
add [Y position] to [list clone y]

when green flag clicked
forever
go to x: (first of clone x) y: (first of clone y)
end

when green flag clicked
forever
point towards [sprite that goes to the clone x and y]
end
LinkSander
Scratcher
1 post

Determine the direction between two coordinates

I made a project using a 3th sprite to calculate wich way the main sprite has to move, sadly this only works if the main sprite is moving at a high speed and
only has to track for a small time
project: https://scratch.mit.edu/projects/1213803179/
awesome-llama
Scratcher
1000+ posts

Determine the direction between two coordinates

Somehow this topic didn't get much of an answer.

The function you are looking for is often called atan2 in other programming languages. It takes a 2D coordinate and returns the angle to it. With two coordinates, you'll want to subtract the first from the second to get the difference.

I have a project with various code examples for atan2 here:
https://scratch.mit.edu/projects/1090711087/

set [vx v] to ((x2) - (x1)) // find difference between the coordinates
set [vx v] to ((y2) - (y1))
set [angle v] to (([atan v] of ((vx) / ((vy) + (0)))) + ((180) * <(vy) < [0]>)) // atan2


Note that if you want the angle counter-clockwise as is common in maths (not scratch's clockwise bearings), swap x and y:

(([atan v] of ((vy) / ((vx) + (0)))) + ((180) * <(vx) < [0]>))

Last edited by awesome-llama (Sept. 6, 2025 15:30:04)

Powered by DjangoBB