// I will try and figure out that lerp() function again
float ballX, ballY;
float angle = random(0, TWO_PI);
float ballSpeedX = 3, ballSpeedY = 3;
boolean wallHit = false;
//----------------------------------------------------------------------------------------------------------
void setup() {
size(600, 400);
ballX = width/4;
ballY = height/4;
}
void draw() {
background(150);
drawBall();
moveBall();
}
//----------------------------------------------------------------------------------------------------------
void drawBall() {
final int BALL_SIZE = 20;
final int ballColor = #FA9D12;
fill(ballColor);
ellipse(ballX, ballY,
BALL_SIZE, BALL_SIZE);
}
void moveBall() {
//
if (!wallHit) {
ballX = ballX+ballSpeedX*cos(angle);
ballY = ballY+ballSpeedY*sin(angle); //Moves ball
if (ballX >= width-10 || ballX <= 10 ||
ballY >= height-10 || ballY <= 10) {
wallHit = true; //Stops ball when it hits a wall
}
} else {
angle = atan2((mouseY-ballY), (mouseX-ballX));
float currX = ballX + width*2 * cos(angle);
float currY = ballY + width*2 * sin(angle);
// line(ballX, ballY,
// currX, currY); //Makes small target line
dottedLine( ballX, ballY,
currX, currY );
}
}
void dottedLine(float x1, float y1,
float x2, float y2) {
// dotted line
float steps=30;
for (int i = 0; i <= steps; i++) {
float x = lerp(x1, x2, i/steps);
float y = lerp(y1, y2, i/steps);
point(x, y);
ellipse(x, y,
5, 5);
}
}
void mousePressed() {
wallHit = false;
angle = atan2((mouseY-ballY), (mouseX-ballX));
}
//
1 Like