android - 在 Android 中绘制弹丸运动路径

标签 android physics draw gravity geometric-arc

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

最佳答案

我猜你的问题出在那里:

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

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

关于android - 在 Android 中绘制弹丸运动路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4817687/

相关文章:

c# - 你如何取向量的倒数?

3d - XNA 3d 物理引擎

Java Canvas 类不作画

javascript - 程序生成形状

android - 如何在 MPAndroidChart PieChart 中将自定义浮点值设置为 y 轴

android - 未处理的 JS 异常 : Could not find "store" in either the context or props

android - 如何仅在 Ubuntu 上安装 Android Studio 命令行工具

android - BottomSheetBehavior 非法状态参数 : 5

javascript - 为什么球会在任意时间后粘在地上?

uml - 如何在序列图中显示 "OR"语句?