We are about to switch to a new forum software. Until then we have removed the registration on this forum.
SO I am trying to create a basic ecosystem using p5js. At the moment, I am trying to make reproduction aka babies work. The problem I am having is that the babies don't seem to "spawn" at the location of their parents and instead spawn at the origin point. Also, at some point the program crashes to what I assume to be infinitely spawning babies for some reason. Below is my code, please tell me what you all think:
Mover class
var Mover=function(mass, x, y){
this.position = createVector(x, y);
this.velocity = createVector(1, 0);
this.acceleration = createVector(0, 0);
this.mass = mass;
this.bang=false;
this.fitness =0;
this.pregnant=false;
this.refractory=0;
this.directionTimer=0;
this.moverDirection= createVector(random(-1,1)*5,random(-1,1)*5);
this.applyForce = function(force) {
var f = p5.Vector.div(force,this.mass);
this.acceleration.add(f);
};
this.update = function() {
this.velocity= this.moverDirection;
this.velocity.add(this.acceleration);
this.position.add(this.velocity);
this.acceleration.mult(0);
if(this.directionTimer>=1000){
this.moverDirection=createVector(random(-1,1)*5,random(-1,1)*5);
this.directionTimer=0;
}
this.directionTimer ++;
};
this.display = function() {
stroke(0);
strokeWeight(2);
fill(255, 175);
ellipse(this.position.x, this.position.y, this.mass*16, this.mass*16);
};
this.checkEdges = function() {
if (this.position.x >= width) {
this.position.x = width;
this.velocity.x *= -1;
} else if (this.position.x < 0) {
this.velocity.x *= -1;
this.position.x = 0;
}
if (this.position.y > height) {
this.velocity.y *= -1;
this.position.y = height;
}else if(this.position.y<0){
this.velocity.y*=-1;
this.position.y=0;
}
}
this.collide = function(m){ //m is each of the mover objects
if(dist(m.position.x,m.position.y,this.position.x,this.position.y)<(80)){
//ridiculous number for testing purposes
this.bang =true;
fill(255,0,0);
}else{
this.bang=false;
}
}
};
sketch.js to run
var move;
var movers = [];
function setup() {
createCanvas(300,300);
move= new Mover(1,random(width),random(height));
for(var i=0; i<3; i++){
movers[i]= new Mover(1,random(width),random(height));
}
}
function draw(){
background(255);
for(var i=0;i<movers.length;i++){
movers[i].display();
movers[i].update();
movers[i].checkEdges();
}
for(var i=0;i<movers.length-1;i++){
for(var j=0;j<movers.length-1;j++){
if(i!==j){
movers[i].collide(movers[j]);
print(movers[i].bang);//check if pregnant and collided
if(movers[i].bang==true && movers[i].pregnant==false){
movers[i].pregnant=true;
movers[i].fitness++;
print(movers[i].fitness);
}
}if(movers[i].fitness>=100){//counter to produce baby
movers.push(new Mover(1,movers[i].x,movers[i].y));
movers[i].fitness=0;
movers[i].pregnant =0;
print("ye");
}
if(movers[i].pregnant=0)
{
movers[i].refractory++
}
if(movers[i].refractory>=400){
movers[i].pregnant=false;
movers[i].refractory=0;
print("pregnant");
}
}
}
};
Answers
I noticed in your code you were saying movers[i].x. You should've done movers[i].pos.x. The mover class doesn't have and x variable. Same for the y as well. Hope I could help. :-)