1

以前にも質問したのですが、私の言い方が悪かったので、回答が得られなかったと思います。発射体の動きでオブジェクトのパスを描画する Android 用のアプリを作成しようとしています。これを機能させるための方程式がありますが、何らかの理由で、プログラムを実行すると、適切な円弧ではなく、2 つの接続された線しか得られません。私は何時間もこれを見つめてきました。何が起こっているのか、それを修正するために何をする必要があるのか​​ 誰か教えてもらえますか? これが私のコードです:(地面も描画しますが、その部分は機能しているようです。地面を作成するために使用される変数の一部がアークでも使用されるため、含まれています。)

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 変数に問題があるようです。

4

2 に答える 2

2

あなたの問題があると思います:

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

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

于 2011-01-27T14:42:24.970 に答える
0

私はそれを考え出した。結局のところ、私の問題はコードの半分に過ぎませんでした。まず、最初の垂直方向のオフセットを考慮していませんでした。これにより、2 つのラインのうち最初のラインが作成されました。2 つ目の問題は、入力する数値でした。気が付きませんでしたが、発射体が数フィートしか移動していないときに、時速約 70 マイルの速度を入力していました。これにより、道はまっすぐに見えました。今回は、一貫性をテストするために同じ数値を入力しました。それを理解するのに10時間しかかかりませんでした。

于 2011-01-30T08:46:49.207 に答える