2011-01-27 14 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个小时才能弄清楚。