About the ball following
in
Programming Questions
•
2 years ago
Hi,guy:
I wrote the project for the test of the ball following,
I don't know how can to make the ball effect like falling star
thanks.~:)
boolean flagball=true; // playing ball game?
float ballpx=320; // position x
float ballpy=240; // position y
float ballvx=0.0; // velocity x
float ballvy=0.0; // velocity y
float ballgy=0.05; // gravitation
float ballsz=30.0; // size
float ballfv=1.0; // rebound factor
// x and y positions
// Declare two arrays with 50 elements.
int[] xpos = new int[30];
int[] ypos = new int[30];
void setup(){
size(640, 480, P2D);
smooth();
for (int i = 0; i < xpos.length; i ++ ) {
xpos[i] = 0;
ypos[i] = 0;
}
}
void draw(){
background(0);
// ball movement : not essential for optical flow
// Shift array values
for (int i = 0; i < xpos.length-1; i ++ ) {
// Shift all elements down one spot.
// xpos[0] = xpos[1], xpos[1] = xpos = [2], and so on. Stop at the second to last element.
xpos[i] = xpos[i+1];
ypos[i] = ypos[i+1];
}
ballpx[xpos.length-1] ++;
ballpy[ypos.length-1] ++;
//position and velocity
ballpx += ballvx;
ballpy += ballvy;
ballvy += ballgy;
// reflecton
if(ballpy>480-ballsz) {
ballpy=480-ballsz;
ballvy=-ballvy*ballfv;
}
// draw the ball
fill(0,0,255,200);
ellipse(ballpx,ballpy,ballsz,ballsz);
}
I wrote the project for the test of the ball following,
I don't know how can to make the ball effect like falling star
thanks.~:)
boolean flagball=true; // playing ball game?
float ballpx=320; // position x
float ballpy=240; // position y
float ballvx=0.0; // velocity x
float ballvy=0.0; // velocity y
float ballgy=0.05; // gravitation
float ballsz=30.0; // size
float ballfv=1.0; // rebound factor
// x and y positions
// Declare two arrays with 50 elements.
int[] xpos = new int[30];
int[] ypos = new int[30];
void setup(){
size(640, 480, P2D);
smooth();
for (int i = 0; i < xpos.length; i ++ ) {
xpos[i] = 0;
ypos[i] = 0;
}
}
void draw(){
background(0);
// ball movement : not essential for optical flow
// Shift array values
for (int i = 0; i < xpos.length-1; i ++ ) {
// Shift all elements down one spot.
// xpos[0] = xpos[1], xpos[1] = xpos = [2], and so on. Stop at the second to last element.
xpos[i] = xpos[i+1];
ypos[i] = ypos[i+1];
}
ballpx[xpos.length-1] ++;
ballpy[ypos.length-1] ++;
//position and velocity
ballpx += ballvx;
ballpy += ballvy;
ballvy += ballgy;
// reflecton
if(ballpy>480-ballsz) {
ballpy=480-ballsz;
ballvy=-ballvy*ballfv;
}
// draw the ball
fill(0,0,255,200);
ellipse(ballpx,ballpy,ballsz,ballsz);
}
1