我正在使用SDL2对太阳系(只是太阳,地球和月球)进行简单的游戏/模拟。我使用浮点表示地球相对于太阳的 Angular (即0度为东,显然90度为下,等等)。当我尝试将值锁定在0到360之间时,它不起作用。

我目前将模拟的上限设为90 fps,并希望每60秒旋转1圈。

// The desired FPS and the number of milliseconds (ticks) between frame draws.
const int FPS = 90;
const int TICKS_PER_FRAME = 1000 / FPS;
// The number of seconds it will take for Earth to make one revolution around the sun.
const int EARTH_REVOLUTION_IN_SECONDS = 1 * 60;
// Number of degrees per millisecond for the Earth.
const float EARTH_DEGREES_PER_MILLISECOND = (float)EARTH_REVOLUTION_IN_SECONDS / 360000.0f;

我将地球放置在距离计算机屏幕约1/3高度的位置,远离太阳,以0.0度角启动地球,然后启动计时器。
   // The distance from the Sun's center to the Earth's center. This will be
   // a quarter of the screen height (presumed to be the smaller dimension).
   int distSunToEarth = resolution_y * 0.3;

   // Starting degrees of Earth relative to the Sun. 0.0 degrees is East.
   float earthDegrees = 0.0f;

   // Amount of time between now and the last frame draw.
   Uint32 deltaTime = 0;
   // The start time. No frames have been drawn yet.
   Uint32 startedTime = SDL_GetTicks();
   // Set current time to the started time.
   Uint32 currentTime = startedTime;

在主循环开始时,我检查所经过的时间是否大于帧之间应该经过的时间。
   // Get the current time in milliseconds.
   currentTime = SDL_GetTicks();
   // Calculate how much time has passed since the last frame draw.
   deltaTime = currentTime - startedTime;

   // If the amount of time that has passed is greater than our desired
   // delay between frames, draw the next frame.
   if (deltaTime > TICKS_PER_FRAME) {
      /* Draw Frame */
   }

这是绘制和移动地球的逻辑。 earthDegrees应该保持在0到360之间,但不是。该代码仍然报告它在那些值内。
   /* Earth */

   // Determine the center of the Earth. Start from the Sun's center and calculate the
   // x and y values relative to it using Soh-Cah-Toa (Yay, trigonometry!). The degrees must
   // be converted to radians using <degrees> * PI / 180.
   int earthCenterX = backgroundCenterX + (distSunToEarth * cos(earthDegrees * M_PI / 180));
   int earthCenterY = backgroundCenterY + (distSunToEarth * sin(earthDegrees * M_PI / 180));

   // Determine the x and y values needed to center the Earth sprite at the above coordinates.
   int earthSpriteX = earthCenterX - (earthSpriteSheet.GetClipWidth() / 2);
   int earthSpriteY = earthCenterY - (earthSpriteSheet.GetClipHeight() / 2);

   // Render the next frame.
   earthSpriteSheet.RenderNextFrame(earthSpriteX, earthSpriteY);

   // Move the Earth aroudn the Sun. Multiply the number of milliseconds that
   // have passed by the number of degrees the Earth moves per millisecond.
   earthDegrees += ((float)deltaTime * EARTH_DEGREES_PER_MILLISECOND);

   //printf("degrees: %f.\n", (float)deltaTime * EARTH_DEGREES_PER_MILLISECOND);
   //printf("earthDegrees: %f.\n\n", earthDegrees);

   // If the degrees become negative, loop back to 360.
   //
   // e.g. if earthDegrees become -2.5 degrees, the new degrees would be:
   // 360 deg - abs(-2.5 deg) => 357.5 deg.
   if (earthDegrees < 0.0)
   {
       printf("Less than 0.0");
       earthDegrees = 360.0f - abs(earthDegrees);
   }
   // Else if the Earth becomes greater than 2PI, round back to 0.
   //
   // e.g. if degrees become 362.5, the new degrees would be:
   // 362.5 deg - 360 deg => 2.5 deg.
   else if (earthDegrees > 360.0f)
   {
       printf("Greater than 360.0");
       earthDegrees = earthDegrees - 360.0;
   }
   else if (earthDegrees >= 0.0f && earthDegrees <= 360.0f)
   {
       printf("Between 0 and 360\n");
   }

   printf("earthDegrees: %d.\n", earthDegrees);



   /*
   ...
   Render code
   ...
   */

   // Reset the started time and current time to now.
   startedTime = SDL_GetTicks();
   currentTime = startedTime;
   // Reset the change in time to 0.
   deltaTime = 0;

这是代码的输出。尽管EarthDegrees不在0到360之间,但是else-if语句检查它是否在0到360之间,但求值结果为true。

c&#43;&#43; - 浮点/双重比较在太阳系模拟中不起作用-LMLPHP

我究竟做错了什么?

最佳答案

printf("earthDegrees: %d.\n", earthDegrees);不打印float,它打印整数。

试试printf("earthDegrees: %f.\n", earthDegrees);

09-06 11:37