【问题标题】:Drawing a Projectile Motion Path in Android在 Android 中绘制弹丸运动路径
【发布时间】:2011-01-27 14:37:57
【问题描述】:

我以前问过这个问题,但我认为我措辞不好,因此没有得到任何回应。我正在尝试为 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 变量似乎存在问题。

【问题讨论】:

  • 我对这个问题的先前版本的评论仍然有效。如果你想解决这个问题,你必须付出一些努力。

标签: android physics draw gravity geometric-arc


【解决方案1】:

我猜你的问题在那里:

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

你只循环一次......

【讨论】:

  • 我很抱歉。当我试图找出问题所在时,我把它放在那里。它实际上循环了 50 次。
【解决方案2】:

我想通了。事实证明,我的问题只是代码的一半。首先,我没有考虑初始垂直偏移,它创建了两行中的第一行。第二个问题是我输入的数字。我没有意识到,但我输入的速度大约是每小时 70 英里,而弹丸只行进了几英尺。这使路径看起来笔直。一直以来,我都输入了相同的数字来测试一致性。只花了 10 个小时就弄清楚了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多