开发者

Wrapping a number?

I have this float which is a rotation angle.

Camera.roty += (float) diffx * 0.2;

where diff is the change in mouse position.

In OpenGL it will wrap it if it exceeds 360 or is below 0, but how could I do this if I want to verify if the angle is between 0 an开发者_运维问答d 180?

Thanks


If I understand your question correctly you're basically looking for something like this?:

float Wrap( const float Number, const float Max, const float Min ) {
 if( Number > 0.0f ) {
  return fmod( Number, Max ) + Min;
 }
 else {
  return Max - fmod( abs( Number ), Max ) + Min;
 }
}


To deal with floating-point values, you could do:

angle = angle - floor(angle / 360) * 360;

This should deal with negative values properly too (-1 would be converted to 359).


According to comment by @bta:

why not use:

angle % 180

and save that number as an angle?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜