Hello, i am trying to code a program that simulates nature. I am trying to use the variable Bee.pos inside of the class Food, but when i do, it gives me “Cannot make a static reference to the non-static field Bee.pos”. Any help is appreciated.
Code:
Bee arnold;
Bee samantha;
Food food;
void setup() {
size(1000, 600);
arnold = new Bee(215, 80, 0, "male");
samantha = new Bee(500, 200, 0, "female");
food = new Food(200, 200);
}
void draw() {
background(113, 98, 225);
arnold.display();
arnold.update();
samantha.display();
samantha.update();
food.display();
}
Bee class:
class Bee {
PVector pos = new PVector();
PVector vel = new PVector();
PVector acc = new PVector();
PVector targetDir = new PVector();
PVector target = new PVector();
boolean hungry = false;
boolean findMate = false;
boolean alive = true;
float age;
float hunger = 1000;
float circlePos;
String sex;
Bee(int setposx, int setposy, float setage, String setsex) {
pos = new PVector(setposx, setposy);
age = setage;
sex = setsex;
}
void display() {
if (alive == true) {
strokeWeight(2);
fill(255, 255, 0);
if (sex == "male") {
stroke(0, 0, 255);
} else if (sex == "female") {
stroke(235, 152, 173);
}
ellipse(pos.x, pos.y, 20, 20);
stroke(0);
circlePos = atan2(vel.x, vel.y);
ellipse(sin(circlePos % 360) * 10 + pos.x, cos(circlePos % 360) * 10 + pos.y, 5 * 2, 5 * 2);//Head
line(sin(circlePos % 360) * 15 + pos.x, cos(circlePos % 360) * 15 + pos.y, sin(circlePos % 360-50) * 100 + pos.x, cos(circlePos % 360-50) * 100 + pos.y);
line(sin(circlePos % 360) * 15 + pos.x, cos(circlePos % 360) * 15 + pos.y, sin(circlePos % 360+50) * 100 + pos.x, cos(circlePos % 360+50) * 100 + pos.y);
line(sin(circlePos % 360-50) * 100 + pos.x, cos(circlePos % 360-50) * 100 + pos.y, sin(circlePos % 360+50) * 100 + pos.x, cos(circlePos % 360+50) * 100 + pos.y);
text(hunger, pos.x, pos.y+20);
}
}
void update() {
hunger --;
if (hunger < 0) {alive = false;}
if (hungry == true) {
targetDir = PVector.sub(target, pos);
} else {
if (pos.x > 0 && pos.y > 0 && pos.x < width && pos.y < height) {
targetDir = new PVector(random(-0.5, 0.5), random(-0.5, 0.5));
} else {
if (pos.x<0) {
targetDir.x=0.5;
} else
if (pos.y<0) {
targetDir.y=0.5;
} else
if (pos.x>width) {
targetDir.x=-0.5;
} else
if (pos.y>height) {
targetDir.y=-0.5;
}
}
}
targetDir.normalize();
targetDir.mult(0.5);
acc = targetDir;
vel.add(acc);
vel.limit(5);
pos.add(vel);
}
}
Food Class:
class Food {
PVector pos = new PVector();
Food(int setposx, int setposy) {
pos.x = setposx;
pos.y = setposy;
}
void display() {
stroke(0);
strokeWeight(2);
fill(255, 255, 0);
ellipse(pos.x, pos.y, 5, 5);
}
void update() {
if (dist(Bee.pos.x, Bee.pos.y, pos.x, pos.y) < 15) {
}
}
}