We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi. I'm trying to constrain an angle. , measured in radians. But radians takes negative values so constrain() method will not work.
So, i tried converting radians to angles, constrain degrees and convert to radians again. But the result is the same.
a = radians(constrain(degrees(a), 90, 270));
Any clue?
Answers
Note: I'm using this to contrain arms an legs in a character. And code fails as soon as angle takes 180º value. It just jumps to 0 suddenly.
You mean this?
Thanks. Your code works just fine. I don't know why mine is failing!
See, this is the simplified version:
Try moving the yellow point to 180º
Sorry, I tried posting my code with "<"code">" tags but it's a mess. How do you do it?
...
Nevermind about code block posting. Indenting did the trick.
So, the problem with my code is this line, throwing negative numbers:
Not sure how should i do it without atan2
I'm not understanding what atan2 does. According to docs, it "Calculates the angle (in radians)...". But radians are not supposed to take negative numbers, am I right?:
While atan2 returns somthing else (don't know what)
Anyway, how can I get numbers like in the graphic?
atan2 returns an angle -PI to +PI or -180 to 180 degrees i.e
East = 0
South = PI/2
West = PI and -PI
North = 3*PI/2
to get convert this to 0-2PI simple add 2PI if atan2 returns a negative value like this
That's clever. Thanks.
If anyone (like me) ignores the negative values of radians, check this: http://www.dummies.com/how-to/content/negative-and-positive-angles-cutting-a-circle.html
There was a mistake in my previous code at line 9 I have corrected it. Sorry about that :)
I know, don't worry.
Just another question. Now it's easy to constrain value from 0 to 360 (thinking in degrees). But what if I need to constrain values from, let's say, 270 to 40? I tried setting 40º as 400 (360+40) but that didn't work. Same would happen with radian, going from 0 to 6.x right?
I'm a dummy. thanks for the link :)
Constraining an angle to a sub range such as 270-40 is much more challenging and it would be good to specify the solution requirements from which we can design a reusable function to do the job.
Function requirements
1) The function requires three angles to be passed as parameters. The angle to constrain as well as the start and end angles for the range.
2) All 3 angles to be stored as radians (more efficient)
3) All 3 angles to be in the range 0 - 2π (i.e. 0 - 360 degrees)
4) Visualized on a circle the constraning-arc is the area swept by a line rotating clockwise from the range start angle to the range end angle.
5) if the range start angle is greater than the end angle it means the constraining-arc includes the angle 0 (zero)
6) If the angle is outside the constraining-arc it returns the nearest of the 2 range limits, otherwise it retuns the angle unchanged.
The following sketch demonstrates the function I have just described.