Game, hitbox (if condition) ignored for number of loops

While running the code, it spawns 6 targets in front of you when you press right mousebutton. You move the 3d camera towards the objects, and if one is within a certain distance of the camera position on the x axis, you stop. If you press right mousebutton again, the loop will run again.

I don’t know why, but for some reason does the program ignore the hitboxes (if condition) of a number of obstacles, based on the amount of times the loop is refreshed: For the first time moving forward, only the hitbox of the 1st obstacle will register, the others won’t, and for the second time the program runs (without closing it) the hitbox of the second will also registers etc. I don’t know why it doesn’t look at all of the hitboxed at the first run, I hope somebody can help me.


boolean moving;
boolean needWipe = false;
boolean restart = true;

int size = 200;
boolean wavePassed = false;
boolean hit = false;

float locations[] = new float[6];

Obstacle obst1, obst2, obst3, obst4, obst5, obst6, cam;

void setup() {
  size(1900, 1000, P3D);


void draw() {

  //println(dist(obst3.xObst, 0, 0, 0));

void respawn() {
  obst1 = new Obstacle(size, locations[0]);
  obst2 = new Obstacle(size, locations[1]);
  obst3 = new Obstacle(size, locations[2]);
  obst4 = new Obstacle(size, locations[3]);
  obst5 = new Obstacle(size, locations[4]);
  obst6 = new Obstacle(size, locations[5]);

void Locations() {
  locations[0] = random(-3840, -2560 - size/2);
  locations[1] = random(-2560 + size/2, -1280 - size/2);
  locations[2] = random(-1280 + size/2, 0 - size/2);
  locations[3] = random(0 + size/2, 1280 - size/2);
  locations[4] = random(1280 + size/2, 2560 - size/2);
  locations[5] = random(2560 + size/2, 3840);

Class of the obstacles:

class Obstacle {

  float size, speed, xObst;
  float distance = 0;

  boolean moving = true;

  PVector camera = new PVector(0, 0, 3000);
  PVector object = new PVector(xObst, 0, 0);

  Obstacle(float s, float x) {
    size = s;
    xObst = x;

  void moveCar(float speed) {

    if (restart == false && hit == false) {
      moving = true;
    } else {
      moving = false;

    if (mousePressed && mousePressed == (mouseButton == RIGHT)) {      //Let's you drive when you hold a mousebutton
      restart = false;
      hit = false;
      moving = true;
      println("restarting car");
      camera.z = 3000;

    if (moving == true && restart == false && restart == false) {      //This part gives you the ability to move left and right while driving
      if (keyPressed && keyPressed == (keyCode == LEFT)) {
        xObst += speed;
      if (keyPressed && keyPressed == (keyCode == RIGHT)) {
        xObst -= speed;

    if (moving == true && restart == false && hit == false) {      //Moves the objects towards you with translate, as if you are driving towards them
      camera.z -= speed;
    //println("distance = ", camera.z - 800);

  void updateObject() {
    rect(xObst, 0, size, size * 3);

  void hitObject() {
    if (camera.z - 800 < 0 && moving == true) {
      if (dist(xObst, 0, 0, 0) < width/2 - 200 && moving == true) {
        println("you are dead, not big suprise");
        needWipe = true;
        hit = true;
        restart = true;
        moving = false;
      } else if (dist(xObst, 0, 0, 0) > width/2 - 200 && moving == true) { 
        println("Next Wave");

  void nextWave() {

    camera.z = 3000;
    moving = true;

  void Camera() {
    camera(camera.x, camera.y, camera.z, object.x, object.y, object.z, 0, 0.001, 0);

  void Actions() {
    if (hit == false && restart == false) {

the “void hitObject()” is responsible for checking whether or not an object is within the distance to stop moving, so its basically the hitbox.

I hope you can help me with this, as I can’t see why it doesn’t work.

This might not really do the trick, but what is that dist(xObj,0,0,0) Part for? The result is always xObj…

Also, Theres a repetition in the 3rd If condition in the moveCar function.

And you should never recall setup() like that. It‘s just for that, setting up.

As for why your Problem happens, i can‘t find a reason… actually it shouldn‘t even happen, since you restart literally everything with the same conditions, either none should work, or all, but adding one each loop doesn‘t make sense, since you don‘t have any variable that counts the number of „loops“.

In P3D it looks always better using lights (); at start of draw()

Generic remark to 3D: imagine you have a table and move stuff on it. That’s your x and z plane: x left and right. z back and forth. Height above the table would be y.

Instead of rect you should use box together with pushMatrix translate rotate box popMatrix

I don’t know why you have an array of locations. Are these the obstacles? Then the position belongs inside the class.

The dist() is obviously wrong; you want to compare the rect position against camera position. Not sure though whether we can move the camera this close to an object. Object would disappear.