compsciguy
YaBB Newbies
Offline
Posts: 5
USA
Re: Trouble with rotate
Reply #6 - Feb 23rd , 2009, 4:34am
Okay, here's my code. I would've posted it earlier, and I probably would've gotten some better help from you guys, but this is for school homework, and I doubt my instructor would like seeing my project here before it's done and turned in. //Bouncy Ball by compsciguy //Initializing variables int x; int y; int speedX; int speedY; int speed; int speedCounter = 0; int speedDirection = 1; //For speedDirection, 1=Up and 0=Down float circleWidth = 70.0; float circleHeight = 70.0; boolean stretched = false; float rotation; int Rbg; int Gbg; int Bbg; int Rball; int Gball; int Bball; int colorChanger = 0; //Setting up void setup() { size(768,512); frameRate(30); x = (width/2); y = (height/2); //Randomly choose colors for background and ball Rbg = int(random(0,255)); Gbg = int(random(0,255)); Bbg = int(random(0,255)); if(Rbg > 127) { Rball = Rbg - 128; } else { Rball = Rbg + 128; } if(Gbg > 127) { Gball = Gbg - 128; } else { Gball = Gbg + 128; } if(Bbg > 127) { Bball = Bbg - 128; } else { Bball = Bbg + 128; } smooth();//Apparently this enables anti-aliasing (avoids pixelized curves) } void draw() { //These lines randomly choose a starting direction for the ball; BETWEEN -2 and 2 because random does not include while(speedX == 0) { speedX = int(random(-2,2)); speedX *= 5; println(abs(speedX)); } while(speedY == 0) { speedY = int(random(-2,2)); speedY *= 5; } background(Rbg,Gbg,Bbg); //Makes the ball move across the screen x = x + speedX; y = y + speedY; //This will make the ball speed up or slow down after it has traveled 30 increments of its speed //speedCounter increments by 1 at the end of the draw function if(speedCounter == 30) { if(speedDirection == 1) { if(speedX > 0) { speedX++;//Travelling right, speeding up } else { speedX--;//Travelling left, speeding up } if(speedY > 0) { speedY++;//Travelling down, speeding up } else { speedY--;//Travelling up, speeding up } } if(speedDirection == 0) { if(speedX > 0) { speedX--;//Travelling right, slowing down } else { speedX++;//Travelling left, slowing down } if(speedY > 0) { speedY--;//Travelling down, slowing down } else { speedY++;//Travelling up, slowing down } } speedCounter = 0; println(abs(speedX)); } //Make the ball slow down if it is speeding up and the speed reaches 30 if(abs(speedX) == 30) { if(speedDirection == 1) { speedDirection = 0; } } //Make the ball speed up if it is slowing down and the speed reaches 1 if(abs(speedX) == 5) { if(speedDirection == 0) { speedDirection = 1; } } //When the ball's edge reaches a wall, it will change direction //I added some code here so that it would change direction when the EDGE of the //ball touched the side, instead of the middle of the ball //UPDATE: When I rotate the ball, it bounces when the edge is just past the wall; //This creates an effect like the ball is invisibly squishing into the wall before bouncing off if ((x > width - (circleHeight*(sqrt(2)/4))) || (x < 0 + (circleHeight*(sqrt(2)/4)))) { speedX = speedX * -1; colorChanger++; } if ((y > height - (circleHeight*(sqrt(2)/4))) || (y < 0 + (circleHeight*(sqrt(2)/4)))) { speedY = speedY * -1; colorChanger++; }