I am trying to do intersections but nothing i do will allow it to work

I have 2 cars and if they touch I want it to pick a new spot, the intersection command says it only uses integers but the random command i have says it only uses floats. i fixed this by adding a rounding command but i can’t seem to get the intersection command down. i have the code here and will bold the part that is not working. I would love some help if anyone is willing.

//Feb 15th 2019
//Frogger

//images
PImage up_frog;
PImage down_frog;
PImage left_frog;
PImage right_frog;
PImage car;
PImage car2;

//movement variables
int x = 225;
int y = 650;

//car movement 
int car1X = 358;
int car2X = 0;
float car1ResetPoint;
float car2ResetPoint;

//changing direction images 
int direction = 3; // 1 = up, 2 = left, 3 = up, 4 = right

//socre 
int score = 0;




void setup() {
  //set size
  size(500, 700);
  //load images
  up_frog = loadImage("Frog.png");
  down_frog = loadImage("Frog_Down.png");
  left_frog = loadImage("Frog_Left.png");
  right_frog = loadImage("Frog_Right.png");
  car = loadImage("car.png");
  car2 = loadImage("car.png");
}



void draw() {

  //move the cars x position
  car1X = car1X + 2;
  car2X = car2X + 2;

  //make 1st grass 
  fill(0, 255, 0); //green
  rect(-1, 600, 501, 100);

  //make 2nd grass
  fill(0, 255, 0); //green
  rect(-1, 300, 501, 100);

  //make 3rd grass
  fill(0, 255, 0); //green
  rect(-1, -1, 501, 100);

  //make 1st road 
  fill(0); //black
  rect(-1, 400, 500, 200);

  //make 2nd road 
  fill(0); //black
  rect(-1, 100, 500, 200);

  //make 1st road lines
  for (int road = -1; road <= 701; road += 80) {
    fill(255, 255, 255); //white
    rect(road, 495, 40, 20);
  }

  //make 2nd road lines
  for (int road = -1; road <= 701; road += 80) {
    fill(255, 255, 255); //white
    rect(road, 190, 40, 20);
  }


  //frog directions
  if (direction == 1) {
    image(down_frog, x, y, 50, 55);
  }

  if (direction == 2) {
    image(left_frog, x, y, 50, 55);
  }

  if (direction == 3) {
    image(up_frog, x, y, 50, 55);
  }

  if (direction == 4) {
    image(right_frog, x, y, 50, 55);
  }

  //teleport car 1 back
  if (car1X >= 580) {
    car1ResetPoint = random(-400, -150);
    int round = round(car1ResetPoint);
    car1X = round;
  } 

  //move car 1
  image(car, car1X, 105, 150, 80);

  //teleport car 2 back
  if (car2X >= 580) {
    car2ResetPoint = random(-400, -150);
    int round = round(car2ResetPoint);
    car2X = round;
  }


  //teleport car2 back
  image(car2, car2X, 105, 150, 80);

  //if car spawns ontop of eachother send it back
  if (car.intersect(car2)) {
    car2ResetPoint = random(-400, car1ResetPoint - 150);
    image(car2, car2X, 105, 150, 80);
  }

  // go back from end
  if (y < 0) {
    y = 650;
    //add 1 to score 
    score = score + 1;
  }

  //down wall
  if (y > 700) {
    y = y - 60;
  }

  //left wall
  if (x < -25) {
    x = x + 60;
  }

  //right wall
  if (x > 505) {
    x = x - 60;
  }






  get(mouseX, mouseY);




  //display help button
  fill(255, 255, 255);
  //help button 
  ellipse(485, 11, 20, 20);
  //detect if mouse is in circle   
  if (mousePressed == true) {
    if (mouseX > 475 & mouseX < 500 & mouseY > 2 & mouseY < 20) {
      //display help box 
      javax.swing.JOptionPane.showMessageDialog(null, "Arrow keys to move & if you get to the end it will add 1 to your score and send you back to the start");
      //switch mouse pressed to false so it can happen again 
      mousePressed = false;
    }
  }

  //help text
  fill(0); //black
  textSize(8);
  text("Help", 476, 14);





  //display socre
  fill(0);//black
  textSize(20);
  text("Score: " + score, 10, 20);
}

void keyPressed() {
  //movement
  if (key == CODED) {
    //move up
    if (keyCode == UP) {
      y = y - 50;
      direction = 3;
    } //move down
    else if (keyCode == DOWN) {
      y = y + 50; 
      direction = 1;
    }//move left 
    else if (keyCode == LEFT) {
      x = x - 50;
      direction = 2;
    }//move right 
    else if (keyCode == RIGHT) {
      x = x + 50;
      direction = 4;
    }
  }

  //help button collision
}
1 Like

Can you please edit your post to format your code?

Where are you getting the intersect() function? I don’t see it in the reference anywhere.

so is it bad i don’t understand what that means???

if you copy that code from some other project you need to copy much more…

if you write that code it means you would have created a
class Car
made a
Car car,car2;
and that class contains a method
class Car {

boolean intersect(){}
}

but your “car” is a PImage not a object of class Car ( what is missing)
and that not knows that method.

if you get the idea from
https://forum.processing.org/one/topic/trying-to-figure-out-how-to-intersect-an-image-with-and-ellipse.html
you should dig deeper…

anyhow a warning ( as it happened to me too often )
when you search for code examples you find lots of,
esp. in a forum,
and it is so easy to copy and paste…
but pls read the details first,

do not copy code from questions
only from solutions!


pls go back to your above first post and repair the code posting!

in processing IDE use [ctrl][t] to format your code there
copy and
in forum post use the

</>

code formatter and paste into it.

1 Like

okay thanks. I am still really new so I don’t know these things