Float/Double 比较在太阳系模拟中不起作用

Float/Double comparison not working in Solar System simulation

我正在使用 SDL2 制作一个简单的 game/simulation 太阳系(只有太阳、地球和月球)。我正在使用一个浮点数来表示地球相对于太阳的度数(即 0 度为东,90 度明显向下,等等)。当我尝试将值锁定在 0 到 360 之间时,它不起作用。

我目前将模拟限制在 90 fps,并希望每 60 秒转一圈。

// 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 之间,但检查它是否在 0 和 360 之间的 else-if 语句的计算结果为真。

我做错了什么?

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

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