Discuss Scratch

Scyth3d
Scratcher
500+ posts

How do I calculate direction of travel based on x velocity and y velocity?

I haven’t taken trig so I genuinely have no clue
Woodfur
Scratcher
100+ posts

How do I calculate direction of travel based on x velocity and y velocity?

The standard formula for direction is atan(Y/X). However, Scratch labels directions differently than standard trig, so you need one more operation to point the right direction in Scratch:

((90) - ([atan v] of ((yvel)/(xvel))::operators))
The_Cool_Test_Alt
Scratcher
100+ posts

How do I calculate direction of travel based on x velocity and y velocity?

Woodfur wrote:

The standard formula for direction is atan(Y/X). However, Scratch labels directions differently than standard trig, so you need one more operation to point the right direction in Scratch:

((90) - ([atan v] of ((yvel)/(xvel))::operators))

Actually, since there are two angles for every slope, this will only report positive directions.

This will give the full range of directions:

((90) - (([atan v] of ((yvel)/(xvel))::operators) * ((yvel) / ([abs v] of (yvel)::operators))))
Scyth3d
Scratcher
500+ posts

How do I calculate direction of travel based on x velocity and y velocity?

The_Cool_Test_Alt wrote:

Woodfur wrote:

The standard formula for direction is atan(Y/X). However, Scratch labels directions differently than standard trig, so you need one more operation to point the right direction in Scratch:

((90) - ([atan v] of ((yvel)/(xvel))::operators))

Actually, since there are two angles for every slope, this will only report positive directions.

This will give the full range of directions:

((90) - (([atan v] of ((yvel)/(xvel))::operators) * ((yvel) / ([abs v] of (yvel)::operators))))
Why does it not work? https://scratch.mit.edu/projects/1214568304/ (Look in The astronaut sprite)
Scyth3d
Scratcher
500+ posts

How do I calculate direction of travel based on x velocity and y velocity?

bump
Scyth3d
Scratcher
500+ posts

How do I calculate direction of travel based on x velocity and y velocity?

never mind I figured it outno i havent

Last edited by Scyth3d (Sept. 13, 2025 00:34:08)

Jareddddddd
Scratcher
1000+ posts

How do I calculate direction of travel based on x velocity and y velocity?

its because of floating point errors. add this script to round down and it should work.

if <([abs v] of (Xv)::operators) < [0.1]> then
set [Xv v] to [0]
end
if <([abs v] of (Yv)::operators) < [0.1]> then
set [Yv v] to [0]
end
Scyth3d
Scratcher
500+ posts

How do I calculate direction of travel based on x velocity and y velocity?

Jareddddddd wrote:

its because of floating point errors. add this script to round down and it should work.

if <([abs v] of (Xv)::operators) < [0.1]> then
set [Xv v] to [0]
end
if <([abs v] of (Yv)::operators) < [0.1]> then
set [Yv v] to [0]
end
I tried that but it doesnt work if your doing down and left
Scyth3d
Scratcher
500+ posts

How do I calculate direction of travel based on x velocity and y velocity?

Bump
Scyth3d
Scratcher
500+ posts

How do I calculate direction of travel based on x velocity and y velocity?

Bump
awesome-llama
Scratcher
1000+ posts

How do I calculate direction of travel based on x velocity and y velocity?

Handling of edge cases with atan2 are explained here:
https://scratch.mit.edu/projects/1090711087/

Verified working implementations:

(([atan v] of ((y) / ((x) + (0)))) + ((180) * <(x) < [0]>))
// swap the x and y vars if you want Scratch's bearings

define point towards (x) (y)
point in direction (([atan v] of (((x) - (x position)) / (((y) - (y position)) + (0)))) + ((180) * <(y) < (y position)>))
Scyth3d
Scratcher
500+ posts

How do I calculate direction of travel based on x velocity and y velocity?

awesome-llama wrote:

Snip
Tysm I really appreciate it. Also for the scratch direction i just did 90-(xxxxx)

Powered by DjangoBB