2011-01-27 5 views
1

私はこの質問を以前に聞いたことがありますが、私はそれに悪いと言い、応答を得られなかったと思います。私は、投射物の動きでオブジェクトのパスを描くアンドロイド用のアプリを作ろうとしています。私はこの作業を行う方程式を持っていますが、何らかの理由でプログラムを実行すると、適切な円弧の代わりに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

答えて

2

私はあなたの問題があると思います。

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

あなたは一度だけループしている...

+0

私はお詫び申し上げます。私は何が間違っているのかを解明しようとしていたが、そこに置いた。実際には50回ループします。 –

0

私はそれを考え出しました。判明したように、私の問題はコードの半分に過ぎませんでした。最初に、最初の2つの垂直オフセットを考慮していませんでした。 2番目の問題は、私が入れていた数字でした。私はそれを認識しませんでしたが、発射物が数フィートだけ移動していた間に、速度は約70マイル/時間にしていました。これにより、パスが真っ直ぐに見えるようになりましそして今度は、一貫性テストのために同じ数字を入れました。それを理解するのに10時間しかかかりませんでした。

関連する問題