Script structure wrong?
in
Programming Questions
•
11 months ago
Hi guys
Im trying to simulate the behavior of the unicellular organism Slime Mould. This organism looks for sources of food and other slime cells. Each has a domain of vision that depends on a determined angle and depth of vision. If a food source is sensed, it moves towards the food. If no food is seen but other cells, moves towards the geometric center of them. If no food nor cells are seen, it follows the same movement.
My script is not working because I dont know how to script a certain part. Once a cell has found food, how can I make them look for other sources of food? They get stuck and I don't know what else to try, I'm quite desperate.
Any advice is welcome. Thanks in advance!
CLASS SLIME CELL
CLASS FOOD
RUN THE SCRIPT
Im trying to simulate the behavior of the unicellular organism Slime Mould. This organism looks for sources of food and other slime cells. Each has a domain of vision that depends on a determined angle and depth of vision. If a food source is sensed, it moves towards the food. If no food is seen but other cells, moves towards the geometric center of them. If no food nor cells are seen, it follows the same movement.
My script is not working because I dont know how to script a certain part. Once a cell has found food, how can I make them look for other sources of food? They get stuck and I don't know what else to try, I'm quite desperate.
Any advice is welcome. Thanks in advance!
CLASS SLIME CELL
- class SlimeCell {
- int visionExtend;
- int visionAngle;
- PVector location;
- PVector direction;
- SlimeCell [] allCells;
- ArrayList neighbors;
- Food [] allFood;
- ArrayList food;
- boolean isOutside;
- SlimeCell() {
- location = new PVector(random(width), random(height));
- direction = new PVector(random(-1, 1), random(-1, 1));
- neighbors = new ArrayList();
- food = new ArrayList();
- isOutside = false;
- }
- PVector returnLocation() {
- return location;
- }
- void getNeighbors(SlimeCell [] allCells, Food [] allFood) {
- visionExtend = 100;
- visionAngle = 135;
- if(allFood.length > 0){
- for (int i = 0; i < allFood.length; i++) {
- PVector a = allFood[i].returnLocation();
- float distance1 = dist(location.x, location.y, a.x, a.y);
- PVector foodCellVect = PVector.sub(a, location);
- float angle1 = PVector.angleBetween(direction, foodCellVect);
- if ((distance1<=visionExtend) && (distance1>0) && (angle1<visionAngle/2)) {
- food.add(allFood[i]);
- }
- }
- }
- for (int i = 0; i < allCells.length; i++) {
- PVector b = allCells[i].returnLocation();
- float distance = dist(location.x, location.y, b.x, b.y);
- PVector cellCellVect = PVector.sub(b, location);
- float angle = PVector.angleBetween(direction, cellCellVect);
- if ((distance<=visionExtend) && (distance>0) && (angle<visionAngle/2)) {
- neighbors.add(allCells[i]);
- }
- }
- }
- void getDirection() {
- float threshold = 45;
- if (food.size()==0) {
- if (neighbors.size()>0) {
- int sumX = 0;
- int sumY = 0;
- SlimeCell oneNeighbor;
- for (int i = 0; i < neighbors.size(); i++) {
- oneNeighbor = (SlimeCell) neighbors.get(i);
- sumX += oneNeighbor.location.x;
- sumY += oneNeighbor.location.y;
- }
- int xCoord = sumX/(neighbors.size());
- int yCoord = sumY/(neighbors.size());
- PVector vectorDirection = new PVector(xCoord, yCoord);
- direction = PVector.sub(vectorDirection, location);
- }if (neighbors.size()==0){
- direction = direction;
- }
- }if (food.size()>0){
- Food aim = (Food) food.get(0);
- PVector foodLoc = aim.returnLocation();
- float distance = dist(location.x, location.y, foodLoc.x, foodLoc.y);
- if(distance > threshold){
- direction = PVector.sub(foodLoc, location);
- }if(distance < threshold){
- direction = direction;
- }
- }
- }
- void mover() {
- direction.normalize();
- direction.mult(2);
- location = PVector.add(location, direction);
- if (location.x<0) isOutside = true;
- else if (location.x>width) isOutside = true;
- else if (location.y<0) isOutside = true;
- else if (location.y>height) isOutside = true;
- if (isOutside) {
- location.x = random(width);
- location.y = random(height);
- direction.x = random(-1, 1);
- direction.y = random(-1, 1);
- isOutside = false;
- }
- }
- void render() {
- //COPY LOCATION AND DIRECTION TO DRAW LINE
- PVector locationVector = location.get();
- PVector directionVector = direction.get();
- directionVector.normalize();
- directionVector.mult(visionExtend);
- PVector lineDir = PVector.add(directionVector, locationVector);
- //RENDER CELL
- noStroke();
- fill(0);
- ellipse(location.x, location.y, 5, 5);
- /*
- //RENDER LINE
- line(location.x, location.y, lineDir.x, lineDir.y);
- PVector vectorDirection = PVector.add(location, lineDir);
- */
- //RENDER SENSING SPACE
- noFill();
- strokeWeight(0.5);
- stroke(160);
- PVector xAxis = new PVector(1,0);
- float dirAxisAngle = PVector.angleBetween(xAxis, direction);
- translate(location.x, location.y);
- if(lineDir.y >= location.y){
- rotate(1*dirAxisAngle);
- arc(0, 0, visionExtend*2, visionExtend*2, radians(-visionAngle/2), radians(visionAngle/2));
- rotate(-dirAxisAngle);
- }else{
- rotate(-1*dirAxisAngle);
- arc(0, 0, visionExtend*2, visionExtend*2, radians(-visionAngle/2), radians(visionAngle/2));
- rotate(1*dirAxisAngle);
- }
- translate(-location.x,-location.y);
- }
- }
CLASS FOOD
- class Food {
- int x;
- int y;
- int rad;
- PVector location;
- Food(int x, int y) {
- location = new PVector(x, y);
- }
- PVector returnLocation() {
- return location;
- }
- void render() {
- rad = 10;
- fill(255, 100, 0);
- ellipse(location.x, location.y, rad, rad);
- }
- }
RUN THE SCRIPT
- int cellNumber;
- SlimeCell [] cellsList;
- Food [] foodList;
- void setup() {
- size(1000, 800);
- smooth();
- //FOOD SOURCES PLACEMENT
- foodList = new Food[3];
- foodList[0] = new Food(width/4, height/2);
- foodList[1] = new Food(width/2, height/4);
- foodList[2] = new Food(3*width/4, height/2);
- //CELLS CREATION
- cellNumber = 100;
- cellsList = new SlimeCell [cellNumber];
- for (int i = 0; i<cellNumber; i++) {
- cellsList[i] = new SlimeCell();
- }
- }
- void draw() {
- background(255);
- for (int i = 0; i < foodList.length; i++) {
- foodList[i].render();
- }
- for (int i = 0; i < cellsList.length; i++) {
- cellsList[i].getNeighbors(cellsList, foodList);
- cellsList[i].getDirection();
- cellsList[i].mover();
- cellsList[i].render();
- }
- }
1