java - Implementation of Projectile Motion -


i have created projectile motion simulation in java user interface. program allows user enter in initial values calculate projectile of object. don't have set draw projectile onto screen.

i have separate spring worker thread handling simulation code in background.

i have added in collision detection when object hits ground bounce , continue doing until loop exits.

the equations have in place not correct trying achieve.

with following initial conditions, here plot of outputted data yields:

initial conditions: angle: 30 degrees; initial speed 8.66 m/s; height: 50 m; elasticity of object: .5 coefficient of restitution in y direction; acceleration: -9.8 m/s^2; no acceleration in x direction 

enter image description here

it appears once simulation begins, y gets bigger , bigger, loop never exit itself.

here code:

    //this class handle time consuming activities class simulation extends swingworker<void, void> {     //execute time consuming task     protected void doinbackground() throws exception      {         filewriter fstream = new filewriter("output.txt");          bufferedwriter out = new bufferedwriter(fstream);          double angle = double.valueof(angletext.gettext());         double radians = angle * (math.pi/180);          double vel = double.valueof(speedtext.gettext());          double mass = double.valueof(masstext.gettext());          double y = double.valueof(heighttext.gettext());         double x = 0;         double epx = double.valueof(epxtext.gettext());         double epy = double.valueof(epytext.gettext());         double ax = double.valueof(accxtext.gettext());         double ay = double.valueof(accytext.gettext());           int numbounces = 0;         double deltatime = .00000001;          double total_velocity = 0.0;         double time = 0.0;          string fs;           angle = angle * math.pi / 180;           while(numbounces < 10)         {             //increment time             time = time + deltatime;              //calculate new values velocity[x] , velocity[y]             double vx = (vel*math.cos(angle)) + ax*time;;             double vy = (vel*math.sin(angle)) + ay*time;               //calculate new values x , y             x = x + vx*time;             y = y + vy*time + .5*ay*(time*time);               system.out.format("%.3f\n", y);               fs = string.format("%f\t %f\t %f\t %f\t %f\t %f\t %f\t\n", ax, ay, x, y, vx, vy, time);               out.write(fs);               //if ball hits ground: y < 0             if(y < 0)             {                 numbounces++;                  system.out.println("number of bounces: " + numbounces);                   //why statement needed if velocity in y direction being reversed?                 vy = -vy - ay*time; //  vy = -vy - ay*time;                   //calculate angle                 angle = math.atan(vy/vx);                  angle = angle * math.pi / 180;                   //calculate total velocity                 total_velocity = math.sqrt((vy*vy) + (vx*vx));                  //velocity elasticity factored in                 total_velocity = math.sqrt((epy) * total_velocity);                  //new velocities when ball makes next trip                 vy = total_velocity*math.sin(angle);                  vx = total_velocity*math.cos(angle);                  out.write(fs);               }              //draw projectile                  //thread.sleep(.00001); //sleep deltatime - 10 nanoseconds or draw after n number of points         }          out.close();           return null;     }      //swingworker lets execute code on event dispatching thread. allows update gui     public void done()     {         try         {                            /*             rangetext.settext(" " + x);             heightttext.settext(" " + y);              timetext.settext(" " + time);              */         }          catch (exception e)         {             e.printstacktrace();         }     } } 

what possible problem? guess might have angle. in previous version of code, did not factor in angle, worked fine. also, not sure if bounds on gui have set y won't go on forever.

i have nullpointerexception.

the first problem see here:

//calculate angle angle = math.atan(vy/vx); angle = angle * math.pi / 180;  

math.atan returns value in radians already:

returns arc tangent of value; returned angle in range -pi/2 through pi/2.

so * math.pi / 180 not going favors.

the second problem here:

//calculate new values velocity[x] , velocity[y] double vx = (vel*math.cos(angle)) + ax*time;; double vy = (vel*math.sin(angle)) + ay*time;  

every pass through loop, these values reinitialized. because angle, ax, ay, , time cannot change during loop, means end same vx , (positive) vy. vy should getting smaller each loop pass, more like:

//calculate initial values velocity[x] , velocity[y] double vx = (vel*math.cos(angle)) + ax*time; double vy = (vel*math.sin(angle)) + ay*time;   while(numbounces < 10) {     //increment time     time = time + deltatime;      //calculate new values x , y     x = x + vx*time;     y = y + vy*time + .5*ay*(time*time);       //calculate new values velocity[x] , velocity[y]     vx += ax * time;     vy += ay * time; 

Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

android - layout with fragment and framelayout replaced by another fragment and framelayout -