We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Help with collision
Page Index Toggle Pages: 1
Help with collision (Read 733 times)
Help with collision
Nov 4th, 2009, 9:56am
 
I am new to processing and have been tinkering with the flock demo. I would like to repel the 'dots' with the mouse but am unsure as to how I should do this. I know I need to create a collision and have added an ellipse around the mouse but can not repel the dots successfully. Can anyone help?

The Dot is created in a seperate tab-

class Dot{ // define a new class

 float diameter;
 color c;

 PVector pos; // position
 PVector vel; // velocity
 PVector acc; // acceleration

 float mouseFactor = 0.05; // strength of mouse attraction //karen change from 0.1

 // cohesion variables
 float cohesionFactor = 0.05; //karen change from 0.2
 float cohesionRadius =  40;

 // alignment variables
 float alignFactor = 0.1;
 float alignRadius =  40;

 // separation variables
 float separationFactor = 10; // karen change from 1.5
 float separationRadius =  25; // karen change from 25

 // speed limit
 float maxVel = 2; //karen change from 5
 
 // friction
 float frictionFactor = 0.95;


 Dot(PVector _pos, float _diameter){ // constructor function has same name as class
   // constructor gets called when we make a new Dot
   pos = _pos;
   diameter = _diameter;
   vel = new PVector(random(-5,5),random(-5,5),0); // random initial velocity
   c = color(random(100,200),random(100,200),random(100,200)); // random initial colour //karen changed from 100-255 less pastel
   acc = new PVector(0,0,0); // set acceleration to 0
 }

 void run(){ // the main function - calls all the other behaviours and updates position and acceleration

     steerMouse(); // steer to mouse factor
   bounce();
   cohesion(); // cohesion factor, radius
   separation(); // separation factor, radius
   alignment();
   friction();
   speedLimit();

   render();

   vel.add(acc); // update the velocity vector - add the current acceleration vector
   pos.add(vel); // update the position - add the current velocity vector
   acc.mult(0); // reset acceleration to 0

 }

 void render(){ // draw the dot
   fill(c);
   ellipse(pos.x,pos.y,diameter,diameter);
 }



 void bounce(){
   if (pos.x > width || pos.x < 0){ // if x pos is greater than right hand edge OR less than left hand edge
     vel.set(vel.x*-1,vel.y,0); // bounce off the edge
   }
   if (pos.y > height || pos.y < 0) { // if y pos is greater than bottom edge OR less than top edge
     vel.set(vel.x,vel.y*-1,0);
   }

 }

 void steerMouse(){
   PVector mousePos = new PVector (mouseX,mouseY,0); // make a new vector based on the mouse position
   PVector toMouse = PVector.sub(mousePos,pos); // make a vector that point towards the mouse by subtracting vectors
   toMouse.normalize(); // scales the magnitude of the vector to 1
   toMouse.mult(mouseFactor); // multiplies the vector by the mouseFactor argument - which defines the strength of the mouse influence
   acc.add(toMouse); // add the toMouse force to our current acceleration
 }

 void separation(){
   for (int i=0; i<Dots.size(); i++){ // run through the arraylist
     Dot Neighbour = (Dot) Dots.get(i); // get each Dot
     float distance = PVector.dist(pos,Neighbour.pos);
     if (distance < separationRadius){ // if this Dot is within our separation range then
       PVector toNeighbour = PVector.sub(pos,Neighbour.pos); // make a vector from the neighbour
       toNeighbour.normalize(); // scale it to 1
       // now scale it according to distance, so that it's strong at close range, and reduces to 0 at the separation radius
       // then multiply it by the separationFactor, which allows us to control the overal strength of the separation force
       toNeighbour.mult(((separationRadius-distance)/separationRadius)*separationFactor
);
       acc.add(toNeighbour); // add the separation force to the acceleration
     }
   }
 }

 void cohesion(){ // each agent steers towards the average position of its neighbours
   PVector avePos = new PVector(); // PVector to store average position
   int neighbourcount = 0; // int to count the number of neighbours
   for (int i=0; i<Dots.size(); i++){
     Dot Neighbour = (Dot) Dots.get(i);
     float distance = PVector.dist(pos,Neighbour.pos);
     if (distance < cohesionRadius){
       avePos.add(Neighbour.pos);
       neighbourcount++;
     }
   }
   avePos.div(neighbourcount); // divide by num neighbours to get the average - the center position of our near neighbours
   PVector toCenter = PVector.sub(avePos,pos); // make a vector to that point
   toCenter.normalize(); // scale it to 1
   toCenter.mult(cohesionFactor); // multiply by cohesion factor
   acc.add(toCenter);    // add the cohesion force to the acceleration
 }


 void alignment(){
   PVector aveVel = new PVector(); // PVector to store average velocity of our neighbours
   for (int i=0; i<Dots.size(); i++){
     Dot Neighbour = (Dot) Dots.get(i);
     float distance = PVector.dist(pos,Neighbour.pos);
     if (distance < alignRadius){ // if the dot is within our alignment range
       aveVel.add(Neighbour.vel); // add its velocity to the average
     }
   }
   aveVel.normalize();
   aveVel.mult(alignFactor);
   acc.add(aveVel);  
 }


 void speedLimit(){
   if (vel.mag() > maxVel){ // if you're going faster than the speed limit
     vel.normalize(vel).mult(maxVel); // scale your velocity back to the limit
   }
 }

 void friction(){
   vel.mult(frictionFactor); // multiply velocity by the friction factor
 }

}



This is the primary tab


ArrayList Dots = new ArrayList();

void setup(){
 size(800,800);
 background(255);
 noStroke();
 smooth();
}
//add ellipse around mouse
void draw(){
 background(255);
 noFill();
 noStroke();
 ellipse(mouseX,mouseY,30,30);

 for (int i=0; i<Dots.size(); i++){ // step through the arraylist - size() gets the size of the arraylist
   Dot tempD = (Dot) Dots.get(i); // get an object out of the list, put it in
Re: Help with collision
Reply #1 - Nov 4th, 2009, 11:18am
 
Collision detection is a FAQ - i.e. do a search of the forums (just make sure you set 'This post was made in the last...' to something older than a week).  Alternatively look at topics.  In particular 'circle collision'...
Re: Help with collision
Reply #2 - Nov 5th, 2009, 6:31am
 
You can look at Keith Peters Actionscript 3 Animation book, it is all explain there and the code is easy to translate,

You can also have a look here
http://www.shiffman.net/teaching/nature/collisions/

Hope that helps
rS
Page Index Toggle Pages: 1