Hi all,
I am working on a game in processing where a Fish needs to eat some food.
I used object collision detection to know when the fish collided with a food object, and then want to delete that object. For this I used some boolean statements.
Unfortunately after I tell the program to not redraw the image after collision, it keeps redrawing and deleting the object.
What am I doing wrong?
Here is a screenshot of the situation;
you don’t really see it that well, but one of these ‘Corn objects’ where the fish collided with, is not deleted but redrawn and deleted every frame. (i think)
Here is my code;
/*
CREATIVE CHALLENGE 2: EGBERT THE FISH GAME_version_11
DATE: 23_10_2022
AUTHOR: TAISSIA VISSER
REFERENCES:
https://www.youtube.com/watch?v=jUmjbFsA2zw&ab_channel=Crazy-Logic
https://processing.org/examples/interpolate.html
INSPIRATION:
https://www.youtube.com/watch?v=NrwaKOsplZk&ab_channel=TheCodingTrain
*/
// Importing the serial library.
import processing.serial.*;
// Declare serial communication port
Serial myPort;
String inString;
int[] data = {0, 0, 1}; // Array of data received from the serial port
// Declare game characters
PImage Campus;
PImage EgbertRight;
PImage EgbertLeft;
PImage Corn;
PImage Plastic;
boolean dataValid = false;
Object A1;// creating objects by defining a new type
Object A2;
Object A3;
Object A4;
Object A5;
Object A6;
Object A7;
Object A8;
void setup() {
size (1920, 1081);
// printArray(Serial.list());
A1 = new Object(); // constructor, make a new object and store it in variable A
A2 = new Object();
A3 = new Object();
A4 = new Object();
A5 = new Object();
A6 = new Object();
A7 = new Object();
A8 = new Object();
String myPortName = Serial.list()[2];
myPort = new Serial(this, myPortName, 9600);
myPort.bufferUntil('\n');
Campus = loadImage("Artboard 1.png");
EgbertRight = loadImage("Egbertred2.png");
EgbertLeft = loadImage ("Egbertred2.2.png");
Corn = loadImage("corn.png");
Plastic = loadImage("plastic.png");
}
// initiating coordinate variables of joystick
float xPos; // X data received through serial
float pastxPos;
float yPos; // Y data received through serial
float pastyPos;
int Pressed;
float EgbertX; // the X position of the Egbert Image
float EgbertY; // the y position of the Egbert Image
void draw() {
background(Campus);
imageMode(CENTER);
A1.display(); // calling display function from the object class, showing multiple objects
A1.dissappear();
A2.display();
A3.display();
A4.display();
A5.display();
A6.display();
A7.display();
A8.display();
if (dataValid) {
// mark new data is processed
dataValid = false;
// and process data
// mapping the range of the X and Y input data of the Joystick to match the drawn pool
// (I have found the correct X and Y coordinates by a mousepressed function to print the coordinates of every corner)
if (mousePressed) {
println("X position is", mouseX, "and Y position is", mouseY);
}
xPos = int(map(data[0], 0, 1023, 220, 1684));
yPos = int(map(data[1], 0, 1023, 220, 855));
}
// translating the origin of egbert to the centre of the png of Egbert,
// for correct mapping of the object. (Because the Png of Egbert has white space around it).
// interpolating between the X of Y position of Egbert, and the movement of the joystick
// moving 3% of the way to the joystick location each frame
// to make sure the movement of the joystick is not so sensitive
EgbertX = lerp(EgbertX, xPos, 0.03);
EgbertY = lerp(EgbertY, yPos, 0.03);
// the game character Egbert the fish is positioned at the X and Y coordinates
// this data is received by the serial and was initialised with xPos and yPos.
//println(" x positie van egbert =", EgbertX);
image(EgbertLeft, EgbertX, EgbertY);
}
// will get called every time data comes in
// storing the data string here
void serialEvent(Serial myPort) {
String inString = myPort.readStringUntil('\n');
if (inString != null) {
// removing the whitespace
inString = trim(inString);
// Splitting the string at the commas and
// seperating the variables out into an array
data = int(split(inString, ","));
// mark new data is available
dataValid = true;
}
//println(inString);
}
class Object {
// template for creating an object
// the data for the object, every object in this class will have this data.
// every object in this class will have a objectlocation x and y
float objectlocationX;
float objectlocationY;
boolean Collision = false; // did the fish collide with any objects?
//float Measure = dist(xPos, yPos, objectlocationX, objectlocationY);
boolean showObject = true;
// constructor that will return the object
Object () {
objectlocationX= random(215, 1680); // the objectlocation x is random between these to values
objectlocationY = random(215, 840); // the objectlocation y is also random between these to values
}
// the functionality of the object
// all objects have the ability to excecute this function
// displays the right images when calling the class
void display() {
//stroke(0);
//fill(200);
//ellipse(objectlocationX, objectlocationY, 100, 100);
if (showObject) {
imageMode(CENTER);
image(Corn, objectlocationX, objectlocationY);
//image(Plastic, objectlocationX, objectlocationY);
}
}
void dissappear() {
// checking weather Egbert 'overlaps' or 'collides' with one of the objects
// is so, then erase that object,
// if the distance between the object location x,y and Egbert, is smaller than 30,
// then they 'overlap'.
float distance = dist(objectlocationX, objectlocationY, EgbertX, EgbertY);
if (distance < 30) {
Collision = true;
}
// if the fish and an object collide, then erase that object.
if (Collision) {
showObject =! showObject;
println("lekker");
//fill(3);
//ellipse(objectlocationX, objectlocationY, 100, 100);
}
}