Sorry to ask so many questions in such a short space of time, but I'm really struggling with this. I require the "pirates" in my code to intersect the "fuel cells" and when they do the cells must disappear. No matter how I try it, the pirates just go straight through the fuel as if no collision is recorded. Both the pirates and fuel cells are generated by arrays.
Here's the Pirate class
- //Create pirate class
class Pirate {
float xPos;
float yPos;
float xSpeed;
float ySpeed;
//Pirate radius
int pr = 20;
Pirate(float tempXPos, float tempYPos, float tempXSpeed, float tempYSpeed) {
xPos = tempXPos;
yPos = tempYPos;
xSpeed = tempXSpeed;
ySpeed = tempYSpeed;
}
//Hit function for pirates when they collide with the player
void hit() {
xPos = random(width);
yPos = random(60, height);
}
//Display function for pirates
void display() {
image(imgCar, xPos, yPos);
pr = 20;
xPos = xPos + xSpeed;
yPos = yPos + ySpeed;
if (xPos > width) { //Pirates reset if they go off screen
xPos = 0;
}
if (xPos < 0) {
xPos = width;
}
if (yPos > height) {
yPos = 45;
}
if (yPos < 45) {
yPos = height;
}
}
//Specify intersect conditions for player and pirates
boolean intersect(Player a) {
float distance = dist(xPos, yPos, a.x_, a.y_);
if (distance <= pr + a.r) {
return true;
}
else {
return false;
}
}
//Specify intersect conditions for pirates and fuel
boolean intersect(Fuel f) {
float distance = dist(xPos, yPos, f.xF, f.yF);
if (distance <= pr + f.fr) {
return true;
}
else {
return false;
}
}
}
And the Fuel class
- //Create fuel class
class Fuel {
float xF;
float yF;
//Fuel radius
int fr = 20;
Fuel(float posX, float posY) {
xF = posX;
yF = posY;
}
//Create show function for fuel
void showFuel() {
image(imgFuel, xF, yF);
}
//Specify intersect conditions for fuel and player
boolean intersect(Player a) {
float distance = dist(xF, yF, a.x_, a.y_);
if (distance < fr + a.r) {
return true;
}
else {
return false;
}
}
//Create collect function for when player picks up fuel
void collect() {
xF = (random(width));
yF = (random(60, height));
fuel = fuel + 100;
}
//Create lose function for when pirates collide with fuel
void lose() {
xF = 1000;
yF = 1000;
}
}
And the code I'm trying to use to control behaviour upon collision
- //Pirate starting positions
for (int i=0; i<pirates.length; i++) {
int pSpeedx = (int)random(8);
int pSpeedy = (int)random(8);
pirates[i] = new Pirate(random(width), random(60, height), pirateSpeeds[pSpeedx], pirateSpeeds[pSpeedy]);
}
- //Fuel Cell starting positions
for (int i=0; i<cells.length; i++) {
cells[i] = new Fuel(random(width), random(60, height));
} - //Show pirates
for (int i = 0; i < pirates.length; i++) {
pirates[i].display();
} - //Show fuel cells
for (int i=0; i<cells.length; i++) {
cells[i].showFuel();
}
- if (pirates[i].intersect(cells[i])) {
cells[i].lose();
}