2012-11-03 41 views
1

這是一個程序,它可以在任何地方點擊鼠標,從而落下球並彈跳。有誰會知道如何改變球落到重力的速度嗎? 林試圖找出適當的解決方案,這...但我有一點麻煩。所有的幫助和/或投入將不勝感激。改變落球速度......正在處理

float x; 
    float y; 
    float yspeed = 0; 
    float xspeed = 0; 
    float balldiameter = 10; 
    float ballradius = balldiameter/2; 

    void setup() { 
    size (400,400); 
    background (255); 
    fill (0); 
    ellipseMode(CENTER); 
    smooth(); 
    noStroke(); 
    x = width/2; 
    y = height/2; 
    } 

    void draw() { 
    mouseChecks(); 
    boundaryChecks(); 
    ballFunctions(); 
    keyFunctions(); 
    } 

    void mouseChecks() { 
     if (mousePressed == true) { 
     x = mouseX; 
     y = mouseY; 
     yspeed = mouseY - pmouseY; 
     xspeed = mouseX - pmouseX; 
     } 
    } 

    void boundaryChecks() { 
    if (y >= height - ballradius) { 
    y = height - ballradius; 
    yspeed = -yspeed/1.15; 
    } 
    if (y <= ballradius) { 
    y = ballradius; 
    yspeed = -yspeed/1.35; 
    } 
    if (x >= width -ballradius) { 
    x = width -ballradius; 
    xspeed = -xspeed/1.10; 
    } 
    if (x <= ballradius) { 
    x = ballradius; 
    xspeed = -xspeed/1.10; 
    } 
    } 

void ballFunctions() { 
    if (balldiameter < 2) { 
    balldiameter = 2; 
    } 
    if (balldiameter > 400) { 
    balldiameter = 400; 
    } 
    ballradius = balldiameter/2; 
    background(255); //should this be in here? 
    ellipse (x,y,balldiameter,balldiameter); 
    yspeed = yspeed += 0.2; 
    xspeed = xspeed/1.005; 
    y = y + yspeed; 
    x = x + xspeed; 
    } 
void keyFunctions() { 
    if (keyPressed) { 
    if(keyCode == UP) { 
    balldiameter +=1; 
    } 
    if (keyCode == DOWN) { 
    balldiameter -=1; 
    } 
    } 
} 
+1

你有什麼嘗試?如果你可以從上一幀獲得時間間隔,那麼這很容易,如果你可以這樣做的話,在draw()過程中,將yspeed乘以9.8 *時間(以秒爲單位)。 – 2012-11-03 02:59:37

+0

你在使用什麼庫?你可以調用像'size'和'background'這樣的東西,但是這些函數沒有在你的代碼中定義。 – durron597

回答

5

地球重力加速度爲9.81m/s^2。所以,如果在點擊鼠標的時候你的球的速度是0,那麼最終的速度就是與時間相關的加速度。這將是(9.81 * t)/ 2。其中t是以秒爲單位。由此產生的單位將是米/秒。您將不得不將儀表轉換爲某個屏幕空間單元進行繪圖。