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.
Code:
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);
background(200);
rectMode(CENTER);
Locations();
respawn();
}
void draw() {
background(200);
obst1.Actions();
obst2.Actions();
obst3.Actions();
obst4.Actions();
obst5.Actions();
obst6.Actions();
//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) {
//background(200);
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");
nextWave();
}
}
}
void nextWave() {
camera.z = 3000;
setup();
moving = true;
updateObject();
}
void Camera() {
camera(camera.x, camera.y, camera.z, object.x, object.y, object.z, 0, 0.001, 0);
}
void Actions() {
moveCar(8);
if (hit == false && restart == false) {
updateObject();
}
hitObject();
Camera();
}
}
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.