Drawing a Projectile Motion Path in Android
I asked this question before, but I think I worded it badly and thus did not get any response. I am trying to make an app for android that draws the path of an object in projectile motion. I have the equations to make this work, but for some reason, when I run my program, all I get is 2 connected lines instead of the proper arc. I've been staring at this for hours, can anyone tell me what is happening and what I need to do to fix it? Here is my code:(It also draws the ground, but that part seems to work. It is included because some of the variables used to create the ground are also used in the arc.)
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));
}
}
Update: After looking at this for a while and trying different numbers,开发者_StackOverflow社区 I believe that the first line being drawn is the correct first (1/50) of the arc. For some reason it seems like the there is a problem with the poraby2 variable in the loop.
I guess your problem is there:
for(int i=0;i<1;i++){
You are looping only once ...
I figured it out. As it turns out, my problem was only half in the code. First, I had not accounted for the initial vertical offset, which created the first of the two lines. The second problem was the numbers I was putting in. I didn't realize it but I was putting in velocities that were about seventy miles per hour while the projectile was only travelling a couple of feet. This made the path seem straight. And all this time I put in the same numbers for testing consistency. It only took 10 hours to figure that out.
精彩评论