Making an aiming line?

Hello!

I am attempting to make an “aiming line” but it has to be dashed.

How can I make a dashed line? I’ve seen the lerp() function mentioned but they always end up not perfectly straight and kind of whacky looking. I want mine to be straight because it is used as a sort of aiming line. Like on pool games you have your aiming line before you take a shot.

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));
    //angle= PI+angle;
    float currX = ballX + 50 * cos(angle);
    float currY = ballY + 50 * sin(angle);
    line(ballX,ballY,currX,currY); //Makes small target line  
  }
}
 
void mousePressed() {
  wallHit = false;
  angle = atan2((mouseY-ballY),(mouseX-ballX));
}

So far I have been able to make the code for the ball to follow the cursor with one little line but how can I make this go all the way to the end of the screen? I am guessing for-loops somehow but I cannot begin to think of exactly how to approach it…

Thanks a bunch!

1 Like

Could you post an mcve

A runnable small version with the line?

Just use lerp with amt values higher than 1

Apologies here is the entire code!

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 + 45 * cos(angle);
    float currY = ballY + 45 * sin(angle);
    line(ballX,ballY,currX,currY); //Makes small target line 
  }
}
 
void mousePressed() {
  wallHit = false;
  angle = atan2((mouseY-ballY),(mouseX-ballX));
}

I will try and figure out that lerp() function again



// 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

You can also use the dashed lines library. See recent discussion: