123
-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|399|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
Socoder -> On Topic -> Angle - Staying in the 360 range - how?

Posted : Wednesday, 20 June 2018, 13:29
Pakz

Angle - Staying in the 360 range - how?


I still have trouble with this problem. I have a angle value and want to decrease it or increase it by a certain amount but stay within the 0.360 or -360..360 value.

I know there is a mod command but I really have no idea how to get this one working. I think it was something like angle-toleftval mod 360 to stay in the 0..360 range?

I made a function once that uses a for ..next command to increase or decrease an angle value but this kind of is not how it should be done.

Does anyone have some code to show in how you do it?
Posted : Wednesday, 20 June 2018, 13:37
rockford
something like this?

IF angle#>360.0 THEN angle#=angle#-360.0
IF angle#<-360.0 THEN angle#=angle#+360.0
Posted : Wednesday, 20 June 2018, 13:44
Pakz
That works.. thanks.

If there is a modula version I would like to know also
Posted : Wednesday, 20 June 2018, 13:47
rychan
angle = angle % 360

Works pretty well for me in C but I'm not dealing with floating points, just integers.

-=-=-
Web / Game Dev, occasionally finishes off coding games also!

Refresh Games - Game Dev Blog
Posted : Wednesday, 20 June 2018, 13:54
Pakz
Ok, I will try to experiment with this. Certain math things really are hard to understand for me still.

Google really does not show the good useful results when I am looking to (re)learn things. Looking for snippets and examples really shows nothing usefull.
Posted : Wednesday, 20 June 2018, 13:56
Jayenkai
Alternatively..



That removes any negatives. I've never really understood why negatives exist in the mod (and/or %) function.. And it frequently annoys the bejesus out of me whenever it happens.

Also, you want to make use of brackets when using Mod.
a=a+b mod c
will result in b being mod'ed on it's own.. (eg, a=a + (b mod c) )
a=(a+b) mod c
is what you actually want.

-=-=-
''Load, Next List!''
Posted : Thursday, 21 June 2018, 00:46
rockford
You can also use ABS to ensure the value is only postive.
Posted : Thursday, 21 June 2018, 02:31
Jayenkai
Nope, a classic error I’ve made numerous times.
Abs inverts any negative number.

Eg, -16 becomes +16, not +344.
Angle=360+Angle is the better way to do that.

-=-=-
''Load, Next List!''
Posted : Thursday, 21 June 2018, 02:40
TomToad
Problem with Mod is that it doesn't work when the result is negative.

You would want 355, but you will get -5 instead.

I will usually use rockford's method if I know the change in angle will be less than 360 degrees. I'll use a couple of While/Wend loops if there is a chance the change will be greater than 360 degrees


Edit: Just reread jayenkai's answer, I think his solution is probably best.
Posted : Thursday, 21 June 2018, 08:17
rockford
Nope, a classic error I’ve made numerous times.
Abs inverts any negative number.

Read what I wrote Jay. I said it ensures the value is positive. Silly jay.
Posted : Thursday, 21 June 2018, 10:18
Jayenkai
Positive, yes, but wrong!!!
Abs(-90) degrees is completely the opposite of -90 degrees!
... unless it's Cos... because... um.. yeah, that!

-=-=-
''Load, Next List!''
Posted : Thursday, 21 June 2018, 10:48
rockford
Sorry Jay, but you will only ever be dealing with positive numbers with ABS, never negatives - even if you take 360 away from a val less than 360 the end result will be positive and the position in the circle accurate.

So ABS is correct in this manner .

And of course you'll be using COS or SIN: it's a circle!
Posted : Thursday, 21 June 2018, 11:55
Jayenkai
Blitz2D/3D/Plus code



-=-=-
''Load, Next List!''
Posted : Friday, 22 June 2018, 09:44
rockford
That's NOT how I would use it and I never gave any code that would even suggest it should be used like that. Obiously I haven;t explained myself very well. But hey ho. That's what night shifts do to you!
Posted : Friday, 22 June 2018, 10:09
Jayenkai
Come on, then! Give us an example!!
Posted : Saturday, 23 June 2018, 17:49
Pakz
I was just experimenting with angles and noticed that if I go above 360 with the cos and sin commands the effect I wanted stays the same. It seems to wheel the values automatically. No need to do this with extra code.