MathHelper.Clamp() problems

Started by
4 comments, last by Senor Beer 15 years, 5 months ago
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
Advertisement
The rotation value for SpriteBatch.Draw takes radians, so giving it such huge numbers will undoubtedly make it spin very quickly.
Mike Popoloski | Journal | SlimDX
Fantatstic, thanks for the reply!

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
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);
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.

More info here.
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

This topic is closed to new replies.

Advertisement