Half Projectile Vi

float dx=10;  // Inital x position
float dy=150; // Inital y position
float dyi=150;
float vx=0;  // Inital x velocity
float vy=0; //Inital v velocity, remember negative y is up
float t=.1;  // Time steps
float g=9.8; // Acceleration due to gravity, where each pixel = 1 meter
float vyf=0; // Final Velocity
float r=.85; // Coeficient of Restitution (Bounciness)
float aimX=50;
float aimY=320;
int angle = 0;
int dySlider=25;
int vi=0;
boolean launch=false;
float time=0.000;
float projAngle;
float projV;

void setup()
{
  size(900, 630);
  smooth();
  background(100);
  textSize(20);
}

void draw ()
{
  background(100);
  strokeWeight(1);
  fill(255);
  ellipse (dx, dy, 20, 20);
  ellipse (50, height/2, 50, 50);

  //Draw the Velocity Slider
  fill(180);
  rect(25, 25, 90, 25);
  strokeWeight(6);
  line(vi+25, 25, vi+25, 50);
  text(vi+" m/s", 125, 50);
  
  //Print the Time of flight on the screen
  text("Time ="+time+" s", 700,50);
  
  //Draw the Velocity Vector Indicator
  pushMatrix();
  translate(50, height/2);
  rotate(radians(angle));
  line(0, 0, vi, 0);
  line(vi, 0, vi-10, -10);
  line(vi, 0, vi-10, 10);
  popMatrix();
  
/*  For each frame vy is initial velocity in the y direction, vyf is the final velocity
    dy is displacement, or distance traveld each frame in the y direction
    g is the acceleration of falling objects
    vx is velocity in the x direction and is constant
    dx is displacement traveled each frame in the x direction
    t is the time incrament for each frame of the simulation   
*/ 
  if (launch)
  {
    vyf=vy+g*t;
    dy=dy+vy*t+.5*(vyf-vy)*t;
    vy=vyf;
    dx=dx+vx*t;
    if(dy<height-10)
    {
    time=time+t;
    }
  }
  if (dy>(height-10))  //If we hit the bottom, stop falling
  {
    dy=height-10;
    vy=0;
    vx=0;
    if (dx<770)      //Decides where to print the range so it's on the screen
    {
    text(int(dx-10)+"m", dx+15, height-2);
    }
    else
    {
    text(int(dx-10)+"m", dx-90, height-2);
    }    
  }

  if (dx>(width-10))  //if we hit the far wall, bounce
  {
    dx=width-10;
    vx=vx*(-r);
  }
}

void mouseDragged()  //If the velocity slider is dragged, update the initial velocity
{
 if (mouseX > 25 && mouseX < 115  && mouseY > 25 && mouseY < 50 && dx<11)
  {
    vi=mouseX-25;
  }
}

void mouseReleased()  //Launch the ball whenever the mouse is released
{
  if (dx<11)
  {
    vx=vi;
    launch=true;
    time=0;
  }
}

void mousePressed()  //when the mouse is pressed reset the simulation
{
  launch=false;
  dx=10;
  dy=dyi;
  vy=0;
}


//void keyPressed()
//{
//  save("Default.png");
//}
Comments