/* I am trying for so long to solve my assignment but I am stuck… need a little help please!
here is my assignment question:
Create a ball of predefined size that bounces around the canvas, reversing directions when
it contacts any edge of the canvas.
- The ball should start with its center 1/4 of the way from the top of the canvas and 1/4 of
the way from the left edge of the canvas. - The ball should never be able to exit the canvas by any amount.
• Modify your program so that the ball travels at a specific angle, instead of using predefined
X and Y speeds. - Use a global state variable to store the angle at which the ball is traveling. The
TOTAL_SPEED of the ball should be 3 pixels per frame. This will
require trigonometry! - Think of the TOTAL_SPEED of the ball as the length of a right angle
triangle, and then calculate the X and Y speeds of the ball
using the angle and TOTAL_SPEED . - Give the ball a random direction (angle) when the program starts.
- When the ball hits an edge, you can simply invert its angle (add PI).
*/
//Here is my code
final int TOTAL_SPEED=3;
float angle;
float bounces = 0;
float mousex;
float mousey;
float direction;
float m;
boolean ballMove;
float ballX,ballY; //Its initial position - somewhere “random”
final int B_SIZE = 30; //Its diameter - it will be a circle
final int BALL_COLOUR = #FFFF00; //Its colour - this is “leather brown” maybe
float ballSpeedX; //Move 3 pixels every 60th of a second
float ballSpeedY; //Move only 2 pixels every 60th of a second
void setup(){
size(1000,500);
ballX=width/4;
ballY=height/4;
ballSpeedX = 3;
ballSpeedY = 2;
ballMove = false;
//direction = PI;
}
void draw(){
background(0); //Erase the old ball
mouse();
drawBall();
println(mousex,mousey,mouseX,mouseY,ballMove);
}
void drawBall()
{ if (ballMove)
{moveBall();}
fill(BALL_COLOUR);
ellipse(ballX,ballY,B_SIZE/1.5,B_SIZE/1.5);
}
void moveBall()
{
if (ballX>(width-B_SIZE/2) || ballX<B_SIZE/2 || ballY>height-B_SIZE/2)
{
ballSpeedX=0;
ballMove=false;
}
if (ballY>height-B_SIZE/2 || ballY<B_SIZE/2)
{
ballSpeedY=0;
ballMove=false;
}
ballX+=ballSpeedX;
ballY+=ballSpeedYtan(0.5QUARTER_PI);
println(sin(0.9*QUARTER_PI));
}
void mouse()
{if (mousePressed)
{ mousex=mouseX;
ballMove = true;
}
}