# MathHelper.Clamp() problems

This topic is 3532 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I'm creating a game at the moment where I need to program a speed dial. The dial is essentially a semi circle where the values on it (purely design) run from 0 to 200. This is because I am programming a float variable 'altitude' with those values. Basically, when the player stops moving, the altitude drops slowly from 200 to 0. When it hits zero, it's game over baby. The speed dial is there to show the player how close he is to hitting 0. I'm using MathHelper.Clamp() to program the pointer on the dial but so far all I have is the pointer doing a silly amount of full circles around the dial. The code I have so far is as follows: speedline.rotation = MathHelper.Clamp(altitude, 0, 180); spriteBatch.Draw(speedline.sprite, speedline.position, null, Color.White, altitude, speedline.center, 1.0f, SpriteEffects.None, 0); My reasoning for this code is that it should use the altitude value (200) to increase or decrease from 180 to 0 (the values in degrees of the semi circle). Looking at it in this post, is it rotating so ridiculously because im drawing it with altitude rather than speedline.rotation? Though when I change it to speedline.rotation I get more or less the same result. Please let me know if I need to be more clear! Thanks!! --Senor Beer

##### Share on other sites
The rotation value for SpriteBatch.Draw takes radians, so giving it such huge numbers will undoubtedly make it spin very quickly.

##### Share on other sites

I've since realised (and feel free to correct me if I'm wrong here) 1 radian = 180 degrees. So yeah using 180 was a little silly.

I've changed the values and almost got it working.

Thanks again mate!
--Senor Beer

##### Share on other sites
You also want to remember to map your [0,200] interval to [0,180]. The final code should be something like

altitude = MathHelper.Clamp(altitude, 0, 200);altitude = altitude / 200 * 180;speedline.rotation = ConvertToRadians(altitude);spriteBatch.Draw(speedline.sprite, speedline.position, null, Color.White, speedline.rotation, speedline.center, 1.0f, SpriteEffects.None, 0);

##### Share on other sites
Quote:
 I've since realised (and feel free to correct me if I'm wrong here) 1 radian = 180 degrees.
No, that's not right. A radian is not an arbitrary unit of measurement; rather, it is equivalent to the angle swept out by a circular arc, the length of which is equal to the radius of the circle.

##### Share on other sites
Nice one, cheers guys!! That wiki article made it all a LOT cleared.

Got it all working now, if I have to code radians again it'll be too soon :D

Thanks again!
--Senor Beer

1. 1
Rutin
26
2. 2
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 14
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631756
• Total Posts
3002125
×