Discuss Scratch
- Discussion Forums
- » Advanced Topics
- » Making an arctan2 function
- franciscomagic
-
Scratcher
38 posts
Making an arctan2 function
Hello, I am making a 3d engine and I absolutely need an “atan2” function. Those anyone know how to make a workaround? (I would like that it returns the answer in degres.)
Discuss below.
Discuss below.
arctan2 x: () y: () :: operators reporter
Last edited by franciscomagic (Sept. 9, 2024 18:08:05)
- TheCreatorOfUnTV
-
Scratcher
1000+ posts
Making an arctan2 function
([atan v] of ((x) / (y)))should work.
EDIT: Never mind, I forgot about negative arctangents.
Last edited by TheCreatorOfUnTV (Sept. 11, 2024 20:53:30)
- blubby4
-
Scratcher
100+ posts
Making an arctan2 function
Except you need logic to handle different quadrants, and it's a bit of a headache. I think it's a reasonable workaround, though, given the advanced nature of trigonometry([atan v] of ((x) / (y)))should work.
- Maximouse
-
Scratcher
1000+ posts
Making an arctan2 function
if <(x) = [0]> then
if <(y) > [0]> then
set [atan2 v] to [90]
else
set [atan2 v] to [-90]
end
else
if <(x) > [0]> then
set [atan2 v] to ([atan v] of ((y) / (x)))
else
set [atan2 v] to (([atan v] of ((y) / (x))) + (180))
end
end
To convert the result to a Scratch direction where 0 is up and 90 is right:
point in direction ((90) - (atan2))
- franciscomagic
-
Scratcher
38 posts
Making an arctan2 function
It works! In about 10 minutes I finished my 3D engine and it is now available here
.
.if <(x) = [0]> then
if <(y) > [0]> then
set [atan2 v] to [90]
else
set [atan2 v] to [-90]
end
else
if <(x) > [0]> then
set [atan2 v] to ([atan v] of ((y) / (x)))
else
set [atan2 v] to (([atan v] of ((y) / (x))) + (180))
end
end
To convert the result to a Scratch direction where 0 is up and 90 is right:point in direction ((90) - (atan2))
- awesome-llama
-
Scratcher
1000+ posts
Making an arctan2 function
set [atan2 v] to (([atan v] of ((y) / (x))) + ((180) * <(x) < [0]>))
This also works. There is no need for special checks when one of the axes are 0 using it, though if you want Scratch's bearings, subtract it from 90 like how Maximouse shows.
- Jonathan50
-
Scratcher
1000+ posts
Making an arctan2 function
This also works. There is no need for special checks when one of the axes are 0 using it, though if you want Scratch's bearings, subtract it from 90 like how Maximouse shows.Aha… intuitively (and if you're just doing math instead of IEEE 754), you need the x=0 check to avoid zero division (it's in Wikipedia's atan2 definition). But atan is smart enough to know what to do with Infinity and -Infinity.
Last edited by Jonathan50 (Sept. 11, 2024 13:32:09)
- franciscomagic
-
Scratcher
38 posts
Making an arctan2 function
True. I am using that one for my black hole simulation game
set [atan2 v] to (([atan v] of ((y) / (x))) + ((180) * <(x) < [0]>))
This also works. There is no need for special checks when one of the axes are 0 using it, though if you want Scratch's bearings, subtract it from 90 like how Maximouse shows.
- Discussion Forums
- » Advanced Topics
-
» Making an arctan2 function





