We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi,
I'm trying to create an anthills program with some interactions between ants but I've already got some bad issues with this project. I've just done something to create anthills and to make ants move but... it's just creating one anthill and ants are motionless... Thank you !
Arnaud
var fourmisParColonie,nombreDeFourmiliere;
var antsSystem = [];
function setup()
{
createCanvas(windowWidth,windowHeight);
background(0);
fourmisParColonie = 20;
nombreDeFourmiliere = 4;
for(i = 0; i < nombreDeFourmiliere; i++)
{
antsSystem[i] = new Anthill(fourmisParColonie);
}
}
function draw()
{
for(i = 0; i < nombreDeFourmiliere; i++)
{
antsSystem[i].displayAnthill();
}
}
function Anthill(_fourmisParColonie)
{
this.fourmisPC = _fourmisParColonie;
this.antTab = [];
this.fourmiliere = createVector(random(windowWidth),random(windowHeight));
this.color = createVector(random(255),random(255),random(255));
for(i = 0; i < this.fourmisPC; i++)
{
this.antTab[i] = new Ant(this.fourmiliere);//création de chaque fourmi avec indication de la position de sa fourmilière
}
}
Anthill.prototype.displayAnthill = function()
{
background(0);
for(i = 0; i < this.fourmisPC; i++)
{
this.antTab[i].displayAnt(this.color);
}
}
function Ant(_fourmiliere)
{
this.lafourmilieredecetteAnt = createVector(_fourmiliere.x,_fourmiliere.y);
this.positionAnt = createVector(this.lafourmilieredecetteAnt.x + random(-30,30),this.lafourmilieredecetteAnt.y + random(-30,30));//spawn fourmi autour de sa fourmilière
this.destinationAnt = createVector(0,0);
this.deltaDestinationAnt = createVector(0,0);
this.timer1 = setInterval(this.definelookFor,random(200,5000));
this.timer2 = setInterval(this.lookFor,20);
}
Ant.prototype.definelookFor = function() //nouvel objectif
{
this.destinationAnt.x = random(-50,50);
this.destinationAnt.y = random(-50,50);
clearInterval(this.timer1);
this.timer1 = setInterval(this.definelookFor,random(200,5000));
}
Ant.prototype.lookFor = function() //aller vers cet objectif
{
this.vitesse = random(20);
this.deltaDestinationAnt.x = this.destinationAnt.x*this.vitesse/2000;
this.deltaDestinationAnt.y = this.destinationAnt.y*this.vitesse/2000;
this.position.add(this.deltaDestinationAnt);
clearInterval(this.timer2);
this.timer2 = setInterval(this.lookFor,20);
}
Ant.prototype.displayAnt = function(_color)
{
this.colorAnt = createVector(_color.x,_color.y,_color.z);
fill(this.colorAnt.x,this.colorAnt.y,this.colorAnt.z);
ellipse(this.positionAnt.x,this.positionAnt.y,10,10);
}
Answers
"Programming Questions" is for Java Mode only! Please edit and change it to "p5.js Programming Questions"! (%)
In order to succeed easier, you should have 2 concepts:
You can see the concept above in this "WaveTrios" online example:
http://studio.processingtogether.com/sp/pad/export/ro.9lTJ6qSlmCidk/latest
In which there's a Wave class, plus another WaveTrio which creates 3 Wave objects.
You can see it written in Java Mode, CoffeeScript Mode & P5.JS in the following forum link:
http://forum.processing.org/two/discussion/5615/coding-noob-needs-help-how-do-i-draw-an-expanding-circle-that-triggers-with-a-mouse-click
Note : on dit une fourmi, pas une fourmis... Ce n'est pas un nom invariant ! ;-)
Thank you but I think that is what I've done here, no ? I have Anthill() which creates some Ant(). I really don't undersand why it is not working...
PS: Sorry for the wrong section posting... PPS: @PhiLho ;)
I guess I've found the bug. In all of your
for ()
loops, the<
relational operator is "glued" to some variable name, like this:i<nombreDeFourmiliere
.You merely needs to separate both w/ a space and voilà:
i< nombreDeFourmiliere
! =:)Again thank you for your time ! I've tried to separate all my variable names of relational operators but sadly it still has the same result :(
In draw(), you call:
antsSystem[i].displayAnthill();
displayAnthill() calls:
this.antTab[i].displayAnt(this.color);
displayAnt() just... displays the ant. I see nowhere (in this chain of calls) where the coordinates of the ants are changed. That's why ants are motionless!
PS.: I suggest to choose between French and English identifiers. And I suggest to choose English, as it will be easier for non-French to help you.
[EDIT] Ah, I see the setInterval call. I am not familiar enough with P5.js to know if that's an usual way to do this. In classical Processing, we rather rely on the regular draw() calls to animate things.
[Another EDIT] You define a
positionAnt
property, but you update aposition
prop. Joy of dynamic languages...I've changed the variable names to have English identifiers. I think that the main problem is linked with scope :
Now ants are moving but I have just one anthill... I'm going to study this :
var self = this
&
&
this.positionAnt.add(self.deltaDestinationAnt);
Because I don't understand it well (someone helped me for it) and I think that it's my main misunderstanding in this project...
Yeah, the concept of "this" is messy in JS, and even though I have read on it several time, should I have to write serious JS again, I would need to read again on the topic!
Yup, that is the main reason I've taken so much to learn JS! And I'm still struggling to advance on it! =P~
Even though it's messy, more than 90% of times,
this
surprisingly behaves just like in Java.Trick is, the
this
inside somefunction
corresponds to the most immediate reference which invoked it.For example in
ref1.ref2.myMethod();
, thethis
inside myMethod() is the object ref2 points to.Previous reference ref1 doesn't count on it. Only the most immediate reference does!
Also, even though myMethod() happens to be a property of another object, its
this
will always be the invoker's reference, not the object "owner" of the property. That lies the danger! :-SSAlthough there's a Function method called bind() which clones the
function
it was called upon.But this time forever bound to a specific object reference, no matter the object which invokes it: \m/
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind
@PhiLho, you can read more about
this
operator at the link below: =:)https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this
I've tried to run your latest attempt but got no apparent error message.
So I've decided to reconstruct your sketch from the beginning, based on the original w/ both Ant & Anthill classes.
Also removed all those complicated stuffs like setTimeout() & bind().
Processing already invokes draw() at 60 FPS by default. We don't need other means in order to sync timers.
Now redefine(), your former definelookFor(), is synced to frameCount + frameRate()'s FPS! :-bd
Well, now it's running. Dunno whether it's the expected behavior. But you can at least carry on from there:
Hello again! Decided to adapt the P5.JS "Anthills" sketch above to Java & JS Modes too. :bz
So it's more evident how the way Java deals w/ classes translates to JS's prototype style! O:-)
It can also be viewed online below running under Processing.JS's framework: B-)
http://studio.processingtogether.com/sp/pad/export/ro.91A3Jfnrsldhp/latest
Woah ! Thank you, it's a very compact and clean sketch. I'll try to understand each line but I think I'll have some questions about it... It's clearly a better way to begin this project. Again, thank you and see you !