PFont f = createFont("Helvetica",32,true);
import processing.serial.*; // import the Processing serial library
Serial myPort; // The serial port
final float g= 9.807;
final float time_to_run = 10000;
final int ground_offset=100;
boolean finished = false;
boolean start_drop =false;
int i=0;
float xDir = 1;
//ball[] c = new ball[2];
ArrayList <ball> c= new ArrayList<ball>();
void setup(){
size(800,600,P3D);//Includes 3D for Sphere
// List all the available serial ports
println(Serial.list());
myPort = new Serial(this, Serial.list()[7], 9600);
// read bytes into a buffer until you get a linefeed (ASCII 10):
myPort.bufferUntil('\n');
//Start Fun
}
void draw(){
if(!start_drop){}
else{
background(255);
for(ball myBall:c){
myBall.run();
}
}
}
void keyPressed(){
if(key == ' '){
//else{
c.remove(0);
//}
}
}
void serialEvent(Serial myPort) {
// make sure we have a valid byte
if (myPort == null) {
return;
}
// read the serial buffer:
String myString = myPort.readStringUntil('\n');
// if you got any bytes other than the linefeed:
myString = trim(myString);
// split the string at the commas
// and convert the sections into integers:
float sensors[] = float(split(myString, ','));
if (sensors[0] > 0) {
xDir = map(sensors[0], 0, 255, -5, 5);
if(xDir == 0) xDir = 1;
if (!start_drop && sensors[1] ==255){
c.add(new ball(xDir));
start_drop = true;
}
if (sensors[1] == 255) {
c.add(new ball(xDir));
}
// print out the values you got:
print("xDir : " + xDir + "\t");
if(sensors[1] == 255) print("SPAWN BALL");
else print("NO SPAWN BALL");
// add a linefeed after all the sensor values are printed:
println();
}
// send a byte to ask for more data:
myPort.write("A");
}
class ball{
float iniX = random(100,width);
float iniY = random(150,height/2);
float randZ= random(100,300);
float xPos = iniX;
float yPos = iniY;
float xDir = 1;
float yDir = 1;
int curMinHeight = 0;
float time;
float initime = millis();
float time_of_run = time_to_run+initime;
float speedconstraint;
int time_check = 0;
boolean lets_go = false;
float yUpdate;
float maxSpeed=0;
boolean g_over = false;
ball(float _xDir){
xDir = _xDir;
}
void run(){
drawBall();//Draw Ball
updateBall();//Update Ball Position
yUpdate = gravityBall(yPos,yDir);//Changes Speed of Ball
yDir = yUpdate;
checkBounds();//Sees where Ball is
stopball();
}
void drawBall(){
// background(#000000);
//fill(#EF5328);//Basketball Orange
//fill(color(random(0,255),random(0,255),random(0,255)));//Rainbowsnake
fill(#837242);
noStroke();
smooth();
lights();//For 3D
pushMatrix();
translate(xPos,yPos,randZ);// Pust Ball in Correct Position
sphere(5);
popMatrix();
}
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-ground_offset){
yDir = yDir*-1; //change Direction of Sign
time = millis( )-initime;//Updates Time
maxSpeed= yDir+maxSpeed;
}
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 = abs((((time_of_run*10)-time)/(time_of_run*10)));// Arbitrary Rate for Ball to Slow Down
if(lets_go){//
temp = (.1* sqrt(2*g*(millis()-initime-time))) * speedconstraint;//Basically Uses the Distance Traveled Since Hitting the Bottom to Adjust Speed
}
else
temp = (.1* sqrt(2*g*(millis()-initime-time))) * speedconstraint*((time/time_of_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_of_run)*height)-round(maxSpeed);//round((time/time_of_run)*height);//Current Ceiling Height
if (curMinHeight>=height/5) lets_go=true;//Tells the Ball to Speed Up
if(curMinHeight>=height-ground_offset){//Stops the Ball
yPos= height-ground_offset;
yDir = 0;
xDir = 0;
g_over =true;
//Tells How Far the Ball has Traveled
// println("Starting Position:" , iniX , iniY);
// println("Ending Position:" , xPos, yPos);
// println("X Distance Traveled", abs(iniX-xPos));
}
}
//boolean explodeBall(ball other){
// float d = dist(xPos,yPos,other.xPos,other.yPos);
// if( d<10){
// return true;
// }
//}
}