Discuss Scratch

Vaieve
Scratcher
12 posts

How do I average angles properly in scratch terms?

Im attempting to create a velocity system and I need a way to average angles in order to combine vectors.

Averaging the usual way doesn't work since the average will always be counterclockwise to the bigger angle, even when rotating clockwise would be more accurate half the time.

The Angles of 300 and 100 should average to around ~40 degrees, but instead it averages to 200, nearly the complete opposite of the actual answer

Last edited by Vaieve (Feb. 21, 2025 11:26:47)

deck26
Scratcher
1000+ posts

How do I average angles properly in scratch terms?

300 and 100 would average to 20 degrees (80 degrees clockwise from 300 and 80 degrees anti-clockwise from 100. The 200 you get is directly opposite.

Just a thought. If you work out the adjustment to get one direction to be 0 - eg you need to add 60 to 300 to get 0 - and add that to both in your case you'd then get a total of 160 wyhich you can halve and then subtract the adjustment again to get 20.

That feels like it would work as long as adjusting the second angle doesn't take it beyond 180 at which point you're better using Scratch's negative angle for the range 180 to 360. I think that works but haven't really considered all the cases. For example if your angels were instead 300 and 160 you want the result 230 (or -130). If you do the same adjustment you get 0 and 220, change 220 to -140, calculate -70 as the average and then subtract 60 to get -130.

If you instead subtracted 100 in your initial case you'd get the angles 0 and 200, convert that to -160, average to -80, add 100 back on to get 20. That suggests it doesn't matter which direction you reset to 0.

I think the general idea works but it needs more testing and there may be a simpler way.

Last edited by deck26 (Feb. 21, 2025 12:52:02)

PaSc_Clan
Scratcher
64 posts

How do I average angles properly in scratch terms?

Your wording is a bit confusing, but if I'm following correctly, you're wanting to have a number from 100-300 and calculate the direction in degrees for scratch's compiler to understand?

((((direction) + [180]) mod [360]) - [180])
I'm pretty sure the code above is what can achieve that.
deck26
Scratcher
1000+ posts

How do I average angles properly in scratch terms?

PaSc_Clan wrote:

Your wording is a bit confusing, but if I'm following correctly, you're wanting to have a number from 100-300 and calculate the direction in degrees for scratch's compiler to understand?

((((direction) + [180]) mod [360]) - [180])
I'm pretty sure the code above is what can achieve that.
I don't think you understand at all! There are two directions involved and the OP wants the ‘average’ direction between those two. Your code gives the Scratch angle equivalent to any angle in the range 0-360 except it will give -180 instead of 180 when that is the actual value. That may be useful in some cases but doesn't address this question.
PaSc_Clan
Scratcher
64 posts

How do I average angles properly in scratch terms?

I think I'm starting to understand a bit more. Is this more so what you're looking for?

// Deleted.

I reviewed this code and it's incorrect. I'm currently fixing it, one moment.

Last edited by PaSc_Clan (Feb. 21, 2025 17:54:38)

PaSc_Clan
Scratcher
64 posts

How do I average angles properly in scratch terms?

PaSc_Clan wrote:

I think I'm starting to understand a bit more. Is this more so what you're looking for?

define Calculate Angle Average (angle1) (angle2)
set [radians1 v] to (((angle1) * (3.14159)) / (180))
set [radians2 v] to (((angle2) * (3.14159)) / (180))
set [x1 v] to ([cos v] of (radians1))
set [y1 v] to ([sin v] of (radians1))
set [x2 v] to ([cos v] of (radians2))
set [y2 v] to ([sin v] of (radians2))
set [avgX v] to (((x1) + (x2)) / (2))
set [avgY v] to (((y1) + (y2)) / (2))
set [avgAngleRadians v] to (([atan v] of (((avgX) - (x position)) / ((avgY) - (y position)))) + ((180) * <(y position) > (avgY)>))
set [avgAngle v] to (((avgAngleRadians) * (180)) / (3.14159))

I reviewed this code and it's incorrect. I'm currently fixing it, one moment.

I have fixed the code.

define Calculate Angle Average (Angle1) (Angle2)
set [AngleNorm1 v] to ((Angle1) mod [360])
set [AngleNorm2 v] to ((Angle2) mod [360])
if <([abs v] of ((AngleNorm1) - (AngleNorm2))) > [180]> then
if <(AngleNorm1) > (AngleNorm2)> then
change [AngleNorm2 v] by [360]
else
change [AngleNorm1 v] by [360]
end
end
set [AvgAngle v] to ((((AngleNorm1) + (AngleNorm2)) / [2]) mod [360])

Last edited by PaSc_Clan (Feb. 21, 2025 17:53:31)

Powered by DjangoBB