We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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
how would I do it using gravity?
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.
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...
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.
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.