Jump to content
  • Advertisement
Sign in to follow this  
Senor Beer

MathHelper.Clamp() problems

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
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 this post


Link to post
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.

More info here.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!