Dribbling Basketball Code

Hey Y’all,

Here is my ball bouncing off the wall(s)! There are 2 Examples of the Code Working. One is as a Normal Basketball the next is as a Rainbow Ball.

Please Feel Free to ask any questions. Thanks! (By the Way the Video will be Choppy, the code doesn’t run choppy though).

 

Dribbling_Basketball

final float g= 9.807;
final float time_to_run = 10000;//Changes How Long the Program Runs(Keep Value >10000)
float iniX;
float iniY;
float xPos;
float yPos;
float xDir = 1;
float yDir = 1;
int curMinHeight = 0;
float time;
float time_of_run;
float speedconstraint;
int time_check = 0;
boolean lets_go = false;
float yUpdate;

void setup(){
size(800,600,P3D);//Includes 3D for Sphere
background(#FFFFFF);
xPos= random(10,width/2);//Picks a Random Point to Start Bouncing Ball
iniX = xPos;//Saves the Initial points
yPos=random(200,height/2);
iniY = yPos;
time = millis();//Keeping Track of Time
}
void draw(){
println(time_to_run-time);//CountDown Timer
drawBall();//Draw Ball
updateBall();//Update Ball Position
yUpdate = gravityBall(yPos,yDir);//Changes Speed of Ball
yDir = yUpdate;
checkBounds();//Sees where Ball is
stopball();//Stops Ball if Time comes
// if((mouseX >= xPos+100 || mouseX <= xPos-100) && (mouseY >= yPos+100 || mouseY <= yPos-100)){
// println(“got it”);
}
void drawBall(){
background(#000000);
fill(#EF5328);//Basketball Orange
//fill(color(random(0,255),random(0,255),random(0,255)));//Rainbowsnake
smooth();
lights();//For 3D
translate(xPos,yPos,0);// Pust Ball in Correct Position
sphere(50);
//ellipse(xPos,yPos,50,50);
//line(xPos,yPos,50,50);
}
void updateBall(){
xPos = xPos+xDir;
yPos = yPos+yDir;
}
void checkBounds(){
if(xPos> width ){
xDir = xDir*-1;// Change Direction of Ball
}
if (xPos<0){
xDir = xDir*-1;//Change Direction of Sign
}
if (yPos> height){
yDir = yDir*-1; //change Direction of Sign
time = millis();//Updates Time
}
if (yPos<curMinHeight){//Lowers the Ceiling so Ball Drops Better
yDir = yDir*-1; // change direction of sign
}
}
float gravityBall( float y, float yd){
float temp;
speedconstraint = ((100000-time)/100000);// Arbitrary Rate for Ball to Slow Down
if(lets_go){//
temp = (.1* sqrt(2*g*(millis()-time))) * speedconstraint;//Basically Uses the Distance Traveled Since Hitting the Bottom to Adjust Speed
}
else
temp = (.1* sqrt(2*g*(millis()-time))) * speedconstraint*((time/time_to_run)+1);//Same as Above but Increases Speed as the Ceiling Gets Lower
if(yd<0) temp = temp*-1;//Makes sure is not NAN
return temp;
}
void stopball(){
curMinHeight = round((time/time_to_run)*height);//Current Ceiling Height
if (curMinHeight>=height/10) lets_go=true;//Tells the Ball to Speed Up
if(curMinHeight>=height){//Stops the Ball
yPos= height;
yDir = 0;
xDir = 0;
//Tells How Far the Ball has Traveled
println(“Starting Position:” , iniX , iniY);
println(“Ending Position:” , xPos, yPos);
println(“X Distance Traveled”, abs(iniX-xPos));
}
}

One response to “Dribbling Basketball Code

  1. Rainbow ball > orange ball.