# Collision of Circles and Lines

Update: oh silly me, it seems like the code is working after all. I just wasn’t proficient at the game enough at first.

Hello! I want to keep track of scores every time the “tail end” of a line touches a bouncing circle.

``````PVector location;
PVector velocity;
PVector gravity;
ArrayList<PVector> list = new ArrayList();
int col1;
int col2;
int col3;
int ran;
float locRanX;
float locRanY;
float previousX;
float previousY;
boolean controlPressed;
float xDistance;
float yDistance;
float gatekeeper;

void setup() {
size(640,360);
controlPressed = false;
locRanX = (float)(Math.random()*2);
locRanY = (float)(Math.random()*2);
location = new PVector(random(width),random(height));
velocity = new PVector(locRanX+0.1, locRanY);
gravity = new PVector(0,(float)(Math.random()*0.7)+0.2);
col1 = (int)(Math.random()*254)+1;
col2 = (int)(Math.random()*254)+1;
col3 = (int)(Math.random()*254)+1;
ran = (int)(Math.random()*2);
previousX = 0;
previousY = 0;
}

void draw() {
if (list.size()>=740){
xDistance = Math.abs(location.x-list.get(0).x);
yDistance = Math.abs(location.y-list.get(0).y);
if (xDistance<48 && yDistance<48 && gatekeeper==0){
System.out.println("h");
gatekeeper=1;
}
}
if (ran==1){
background(255);
}
else {
background(0);
}

if ((location.x < 0)) {
velocity.x = velocity.x * -1;
col1 = (int)(Math.random()*254)+1;
col2 = (int)(Math.random()*254)+1;
col3 = (int)(Math.random()*254)+1;
}
if((location.x> width)){
velocity.x = velocity.x * -1;
col1 = (int)(Math.random()*254)+1;
col2 = (int)(Math.random()*254)+1;
col3 = (int)(Math.random()*254)+1;
gravity = new PVector(0,(float)(Math.random()*0.6)+0.2);
}
if (location.y > height) {
velocity.y = velocity.y * -0.88;
location.y = height;

}
if (location.y < 0) {
velocity.y = velocity.y * -0.6;
location.y = 0;
gatekeeper=0;
}

if (mousePressed){
velocity.y = velocity.y * 1.05;
}

if (ran!=1){
stroke(255);
fill(127);
}
else{
stroke (0);
fill(60);
}
strokeWeight(2);
ellipse(location.x,location.y, 48, 48);

for(PVector currentPV : list) {
stroke(col1, col2, col3);
if(list.indexOf(currentPV)>0){
line (currentPV.x, currentPV.y, previousX, previousY);
}
previousX = currentPV.x;
previousY = currentPV.y;
}

if(list.size()>740)list.remove(0);
}
void keyPressed() {
if (key == CODED) {
if (keyCode == CONTROL) {
println("control");
controlPressed = true;
}
} else {
if (controlPressed && keyCode == 83) {
save("line-######.png");
}
}
}

void keyReleased() {
if (key == CODED) {
if (keyCode == CONTROL) {
controlPressed = false;
}
}
}
``````

In theory, it sounds easy: I just have to check the x and y coordinates of the first element in the list after it starts deleting the extra points on the line (to prevent memory from being eaten). The “gatekeeper” variable makes sure that the player can only earn one point from one collision (not continuously earn points as long as the point is in the circle). The gatekeeper resets every bounce.

However, the code only seem to work only the first time it collides, as I cannot get more than one “h” (just a testing thing) at any run.