2016-05-13 35 views
1

我正在使用SDL2製作太陽系(只是太陽,地球和月球)的簡單遊戲/模擬。我使用浮點來表示地球相對於太陽的度數(即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; 

我把地球放在距離太陽約三分之一高度的電腦屏幕上,以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語句的計算結果爲true。

enter image description here

我在做什麼錯?

回答

2

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

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

+0

似乎這是問題...我的土精靈是不動的,但至少度顯然改變。謝謝。 – Qwurticus

+0

這是否真的回答了這個問題?發佈的問題聽起來不像輸出格式問題。另外,爲'printf'使用不正確的格式說明符是未定義的行爲。最好使用'std :: cout',你不會陷入這種麻煩。 – PaulMcKenzie