How to shoot a ball in an arc?

edited November 2017 in How To...

right now i have it shooting straight up and down, but how would i get it to shoot in an arc across the canvas?

float time;
float g= -9.8;
final float TIME_STEP= time+ .2;
float calcBallX, calcBallY;
int v=50;


void setup() {
  size(1000, 1000);
  time = 0.0;
}

float calcBallX(float time, float initV, float angle) {
  angle=PI/2;
  initV = v*cos(angle);
  calcBallX= 40+(v*time*initV);
  return(calcBallX);
}
float calcBallY(float time, float initV, float angle) {
  angle=PI/2;
  initV= v*sin(angle);
  calcBallY=500-((time*initV+(0.5*(g*(sq(time))))+40));
  return(calcBallY);
}
void draw() {

  background(255);
  fill(0);
  ellipse(calcBallX(time, 100, PI/2), 
    calcBallY(time, 100, PI/2 ), 
    10, 10);
  time += TIME_STEP;
}

Answers

  • float px = 20;
    float py = 380;
    float vx = 5;
    float vy = -12;
    float ax = 0;
    float ay = 0.32;
    
    void setup() {
      size(400, 400);
    }
    
    void draw() {
      simulate();
      render();
    }
    
    void simulate() {
      vx+=ax;
      vy+=ay;
      px+=vx;
      py+=vy;
    }
    
    void render() {
      background(0);
      noStroke();
      fill(0, 255, 0);
      ellipse(px, py, 20, 20);
    }
    
  • how would I do it using gravity?

  • edited November 2017

    Gravity? You mean acceleration in the positive y direction? Yeah, the above sketch has that. It's the ay variable.

  • i have to do it using the formulae in my code

  • If you have certain restrictions on how you have to do things, you should post them. All of them. Now.

  • right now i have it shooting straight up and down, but how would i get it to shoot in an arc across the canvas?

    So you want to change the angle? Have you tried changing the angle?

    Lines 14 and 20. Pi / 2 is straight up. Try other values...

    (I'd make angle a constant at the top of the file. Defining it on two places just means twice as much editing when you want to change it)

  • @koogs I noticed that too, but the problem is deeper than just that. For one thing, the calcBallX/Y function also redefine the angle and initV values passed to them...

  • edited November 2017

    Wait. Why are 14 and 20 even there? Angle is being passed on as an argument in both methods and you are then ignoring the value. Delete lines 14 and 20, change the angle in lines 29 and 30

  • Ha, yes.

    Argument should be v, initv should be local... The 100 should be a constant.

  • edited November 2017

    TfGuy44 already gives the answer, here is a implementation, for copy +paste. back to basics without using trig functions, if you get the concept, you ready to use advanced formulas.

    float pX, pY, vX=7.0, vY, g=0.981;
    void setup() {
      size(400, 400);
    }
    void draw() {
      if (pY>=350.0) {
        vY+=map(vY, 0.0, 0.5, 0.5, -0.3599); // kind of sin() vY*=-0.5; 
        vX/=2.0;
        pY=350.0;
      };
      vY+=g;
      ellipse(pX+=vX, pY+=vY, 15, 15);
    }
    
Sign In or Register to comment.