2011-01-27 76 views
1

我之前問過這個問題,但我想我寫得很糟糕,因此沒有得到任何答覆。我正在試圖製作一個android應用程序,用於繪製拋物體運動中物體的路徑。我有方程來完成這個工作,但由於某種原因,當我運行我的程序時,我所得到的只有2條連接線,而不是正確的弧。我一直在盯着這幾個小時,任何人都可以告訴我發生了什麼,我需要做些什麼來解決它?這裏是我的代碼:在Android中繪製彈道運動路徑

float constx = 400; 
    float consty = 375; 
    float deltx = (float) ProjectileMotionDrawingActivity.dx; 
    float delty = (float) ProjectileMotionDrawingActivity.dy; 
    float maxDrawingHeight; 
    float totwidth; 
    float totheight; 
    float starty; 
    float ydist; 
    float cx = canvas.getWidth()/2; 
    float cy = 210; 
    boolean limiter; 

    float vin = (float) ProjectileMotionDrawingActivity.vin; 
    float vxd = (float) ProjectileMotionDrawingActivity.vxd; 
    float acc = (float) ProjectileMotionDrawingActivity.ac; 

    float scaleda; 
    float scaledv; 
    float scaledvi; 



    //Set background color and get paint ready 
    canvas.drawColor(Color.WHITE); 
    Paint linePaint = new Paint(Paint.ANTI_ALIAS_FLAG); 
    linePaint.setColor(Color.BLACK); 

    //Define maxDrawingHeight 
    if(delty >= 0){ 
     maxDrawingHeight = (float) ProjectileMotionDrawingActivity.mhe; 
    }else{ 
     maxDrawingHeight = (float) (ProjectileMotionDrawingActivity.mhe + Math.abs(delty)); 
    } 

    // Determine whether x or y is limiting factor (true=x, false =y) (For future use if needed) 
    if(Math.abs(maxDrawingHeight/deltx) >=consty/constx){ 
     limiter = false; 
    }else{ 
     limiter = true; 
    } 

    //set width and height of projectile motion 
    if(limiter){ 
     totwidth = constx; 
     totheight = constx*maxDrawingHeight/deltx; 
     scaleda = acc*constx/deltx; 
     scaledvi = vin*constx/deltx; 
     scaledv = vxd*constx/deltx; 

    }else{ 
     totheight = consty; 
     totwidth = consty*deltx/maxDrawingHeight; 
     scaleda = acc*consty/maxDrawingHeight; 
     scaledvi = vin*consty/maxDrawingHeight; 
     scaledv = vxd*consty/maxDrawingHeight; 
    } 

    //height of cliff 
    ydist = delty*totheight/maxDrawingHeight; 

    //start height 
    starty = cy+(totheight/2); 

    canvas.drawLine(0, starty, totwidth+35, starty, linePaint); 
    canvas.drawLine(totwidth+35, starty, totwidth+35, starty-ydist, linePaint); 
    canvas.drawLine(totwidth+35, starty-ydist, 2*cx, starty-ydist, linePaint); 

    //Parabola 

    float porabx = 35; 
    float poraby = starty; 
    float porabx2 = 35 + totwidth/50; 
    float poraby2 = (float) (starty - scaledvi*porabx2/scaledv-.5*scaleda*Math.pow(porabx2/scaledv,2)); 

    for(int i=0;i<50;i++){ 
     canvas.drawLine(porabx, poraby, porabx2, poraby2 , linePaint); 

     porabx = porabx2; 
     poraby = poraby2; 
     porabx2 += totwidth/50; 
     poraby2 = (float) (starty - scaledvi*porabx2/scaledv-.5*scaleda*Math.pow(porabx2/scaledv,2)); 

    } 
} 

更新(這也吸引了地上,但是這部分似乎工作它包括在內,因爲一些用於創建地面的變量也都在電弧中使用。):後看着這一段時間,嘗試不同的數字,我相信所畫的第一條線是正確的第一條弧(1/50)。出於某種原因,似乎在循環中poraby2變量存在問題。

+0

我對這個問題的前一版本的評論依然存在。如果你想解決這個問題,你必須付出一些努力。 – Beta 2011-01-30 01:59:40

回答

2

我猜你的問題是有:

for(int i=0;i<1;i++){ 

你循環只有一次......

+0

我很抱歉。當我試圖找出錯誤時,我把它放在那裏。它實際上循環了50次。 – 2011-01-27 14:44:48

0

我想通了。事實證明,我的問題在代碼中只有一半。首先,我沒有考慮創建兩條線中第一條的初始垂直偏移量。第二個問題是我輸入的數字。我沒有意識到,但是我的速度大約是每小時七十英里,而射彈只有幾英尺。這使得道路看起來很直。而且這一次我爲測試一致性輸入相同的數字。只花了10個小時才能弄清楚。