We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Having a hard time coding a parabola
Page Index Toggle Pages: 1
Having a hard time coding a parabola (Read 3068 times)
Having a hard time coding a parabola
Jun 17th, 2010, 10:59am
 
Hello,

What I'm trying to do is basically a cannonball that goes up into the air and then comes back down. I'm thinking of doing this by making a cannonball class and passing it the initial x position, the angle at which the cannonball is fired, and the speed at which it is fired.

The following code would determine the speed in either direction:

xSpeed = speed * cos(radians(angle));
ySpeed = speed * sin(radians(angle));

I would then use the following formula to determine the x coordinate and y coordinate for the cannonball:

d = vt + 0.5(at^2)

where d = displacement, v = velocity, a = acceleration, and t = time.

Finding the x coordinate is pretty straightforward; since the acceleration in the x direction is zero, the formula becomes:

d = vt

which you could code as

x += xSpeed;

It's the y coordinate that has me completely stumped, specifically the t^2 part. I have no idea how to implement this in code. Any help would be immensely appreciated. Thanks!
Re: Having a hard time coding a parabola
Reply #1 - Jun 17th, 2010, 12:02pm
 
Ok so I took the code from pg 251 of Ira Greenberg's book, modified it a bit, and this is what I got:

Quote:
float x;
float y;
float speed;
float angle;
float xSpeed;
float ySpeed;
float accel = -.98;

void setup(){
  size(800, 400);
  background(0);
  smooth();
  noStroke();

  angle = 80;
  speed = 20;
  xSpeed = speed * cos(radians(angle));
  ySpeed = speed * sin(radians(angle));
}

void draw(){
  fill(0,50);
  rect(0,0,width,height);
  fill(255);

  x += xSpeed;
  
  // double assignment creates y acceleration
  ySpeed += accel;
  y += ySpeed;

  ellipse(x, y, 10, 10);
}


Which works, but I still can't quite wrap my head around this part:

Quote:
  // double assignment creates y acceleration
  ySpeed += accel;
  y += ySpeed;


Maybe I just need to take a break.
Re: Having a hard time coding a parabola
Reply #2 - Jun 17th, 2010, 7:20pm
 
Well, t^2 is t squared, which is t times t.  If you want to take d = vt + 0.5(at^2) and implement that equation directly, it would look like:
Code:
y = v * t + 0.5 * a * t * t; 



As far as why
Code:
  // double assignment creates y acceleration
ySpeed += accel;
y += ySpeed;

gets the same effect... well, it's calculus.  If speed is changing linearly with time (constant acceleration), then integrating that over time gives a displacement that changes proportionally to time-squared.  If you've had calculus, this should more or less make sense.  If you haven't had calculus, then it probably won't. :/

Re: Having a hard time coding a parabola
Reply #3 - Jun 17th, 2010, 8:17pm
 
Quote:
If speed is changing linearly with time (constant acceleration), then integrating that over time gives a displacement that changes proportionally to time-squared.  If you've had calculus, this should more or less make sense.  If you haven't had calculus, then it probably won't. :/


Thanks Smitty! My calculus has turned into a huge pile of rust over the last decade or so, which I greatly regret. I think I'll sign up for a refresher course.
Re: Having a hard time coding a parabola
Reply #4 - Jun 18th, 2010, 6:43am
 
Glad to help!  You can probably skip the calc refresher if all you're interested in is ballistic objects, but it might be handy for other things.

Also, one minor thing: if you want your numbers for the second method to be "exact", you'll want to do this:

Code:
  // double assignment creates y acceleration
y += ySpeed + 0.5*accel;
ySpeed += accel;

// instead of:
// ySpeed += accel;
// y += ySpeed;


This will correctly calculate the position for a uniform constant acceleration, whereas the other method (commented out) treats it as a series of acceleration "bursts", one at each frame of the simulation.

In most cases the difference won't be noticeable to the naked eye, but for large accelerations it can make a difference.  It's also nice if you need the numbers to come out exactly right.
Page Index Toggle Pages: 1