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

Wed, 20 Jun 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?
Wed, 20 Jun 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
Wed, 20 Jun 2018, 13:44
Pakz
That works.. thanks.

If there is a modula version I would like to know also
Wed, 20 Jun 2018, 13:47
cyangames
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!
Wed, 20 Jun 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.
Wed, 20 Jun 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!''
Thu, 21 Jun 2018, 00:46
rockford
You can also use ABS to ensure the value is only postive.
Thu, 21 Jun 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!''
Thu, 21 Jun 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.
Thu, 21 Jun 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.
Thu, 21 Jun 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!''
Thu, 21 Jun 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!
Thu, 21 Jun 2018, 11:55
Jayenkai
Blitz2D/3D/Plus code



-=-=-
''Load, Next List!''
Fri, 22 Jun 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!
Fri, 22 Jun 2018, 10:09
Jayenkai
Come on, then! Give us an example!!
Sat, 23 Jun 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.