About Iterators on objects
in
Programming Questions
•
2 years ago
Hi ! I'm back again with my little script. As i mentioned
here :
I try to create an animation that I saw few month earlier on a website : you click somewhere, a circle begin to grow, then you click somewhere else, another circle begin to grow and when they meet, a sound is made.
My problem was with the circles position, then it was with instances generation, but before being lost, i found this topic who helped me to create objects automatically :
http://processing.org/discourse/yabb2/YaBB.pl?num=1212683829
It uses Iteration without using hashmap, it was perfect for me since i needed to create multiples instances of the same animated class by clicking. BUT now I have to setup the collision to make the circles inverse theyre growth when hitting the others circles. With the iteration presented in the code below (I withdrawn sound code parts for you all to test it) i don't know how to retrieve informations about the objects created to calculate distances to set up any collision function, could you help me ?
Here is the code :
- Vector cercles;
- void setup() {
- size(400, 400);
- ac = new AudioContext();
- cercles = new Vector();
- }
- void draw() {
- background(192, 64, 0);
- Iterator i = cercles.iterator();
- while (i.hasNext())
- {
- Cercle cercle = (Cercle)i.next();
- cercle.display();
- cercle.drive();
- }
- }
- void mousePressed()
- {
- cercles.add(new Cercle(1,1,0));
- }
- class Cercle {
- // data
- float startX, startY;
- float largeur;
- float hauteur;
- float swap;
- float vitesse;
- // constructor
- Cercle (float largeur, float hauteur, float swap){
- startX = mouseX;
- startY = mouseY;
- vitesse = 0.7;
- }
- // fonctions
- void display() {
- ellipseMode(CENTER);
- ellipse(startX,startY,largeur,hauteur);
- }
- void drive() {
- if(largeur < 60)
- {
- if(swap == 0)
- {
- largeur+=vitesse;
- hauteur+=vitesse;
- }
- else
- {
- largeur-=vitesse;
- hauteur-=vitesse;
- if(largeur <= 1)
- { swap = 0; }
- }
- }
- if(largeur >= 60)
- {
- swap = 1;
- largeur-=vitesse;
- hauteur-=vitesse;
- }
- }
- }
Thanks

1