Loading...
Logo
Processing Forum
you can find the working sketch online at:  http://openprocessing.org/sketch/85451

Basically I have a class that creates and ellipse. This ellipse will chase the mouse with a little bit of logic.

Then I make five of those ellipses. 

QUESTION:

how do I keep multiple ellipses from following the same path? So as their projectories are different and they approach the mouse from different sides as much as possible?

Its ok if its for a little bit but  I want to keep them from bunching up and overlaying. Do I have to keep track of all their locations and adjust accordingly? or can the variables or "forces" added to them be tweaked enough to make this happen?

here is the code if you do not want to go to the link above:

'''''''''''''''''''''''main''''''''''''''''''''''''''''''''''
Copy code
  1. //data variables
  2.   ArrayList ellipsoids = new ArrayList();
  3.   PVector mouse, direction, loc, vel, accel;
  4.   int numObjects = 5;
  5.   
  6.   
  7. void setup(){
  8.   size(500,500);
  9.   
  10.   //initialize and interate the ellipsoid class into the array list
  11.   for (int i = 0; i < numObjects; i ++){
  12.     //defines initial starting location and velocity
  13.     PVector loc = new PVector(random(0, width), random(0,height), 0);
  14.     PVector vel = new PVector(random(-1,1),random(-1,1), 0);
  15.     PVector accel = new PVector(0,0);
  16.     PVector mouse = new PVector(random(0,width),random(0,height));
  17.     //Ellipsoids(PVector tempMouse,PVector tempLocation, PVector tempVelocity, PVector tempAcceleration)
  18.     Ellipsoids tempEllipsoids = new Ellipsoids(mouse, loc, vel, accel);
  19.     ellipsoids.add(tempEllipsoids);
  20.   }//for
  21.   
  22. }//setup

  23. void draw(){
  24.   background(100); 
  25.   toScreen();
  26. }//draw

  27. void toScreen(){
  28.   //pull each ellipsoid out of the array and draw it to screen
  29.   for(int j = 0; j < ellipsoids.size(); j++){
  30.     Ellipsoids ellips = (Ellipsoids) ellipsoids.get(j);
  31.     ellips.run();//method in class that starts other methods
  32.   }//for
  33. }//toScreen


''''''''''''''''''''''class''''''''''''''''''''''''''''''''''''''

Copy code
  1. class Ellipsoids{
  2.   //our class of two dimensional aliens
  3.   //here to play tag with our mice
  4.   //avoid them!! all this class does is 
  5.   //draw the ellipse with the given
  6.   //variables from the main code
  7.   
  8.   //data variables
  9.   //Some PVectors to store our coordinates in for easy access
  10.   PVector location,mouse,direction,acceleration,velocity;
  11.   //floats to transfer data from PVectors and do math
  12.   float x,y,dx,dy;
  13.   float boost = 0.5;
  14.   boolean isFar = true;
  15.   boolean isShort = false;
  16.   
  17.   //constructor
  18.   Ellipsoids(PVector tempMouse,PVector tempLocation, PVector tempVelocity, PVector tempAcceleration){
  19.     //initialize object data
  20.     mouse = new PVector(tempMouse.x,tempMouse.y);
  21.     location = new PVector(tempLocation.x,tempLocation.y);
  22.     velocity = new PVector(tempVelocity.x,tempVelocity.y);
  23.     acceleration = new PVector(tempAcceleration.x,tempAcceleration.y);
  24.   }//constructor
  25.   
  26.   void run(){
  27.     //run all the other methods
  28.     trackMouse();
  29.     move();
  30.     tests();
  31.     updateFar();
  32.     updateShort();
  33.     render();
  34.   }//run
  35.   
  36.   //this continually tracks and updates the mouse PVector
  37.   //so we can use that information in other methods.
  38.   void trackMouse(){
  39.       mouse = new PVector(mouseX,mouseY);
  40.       //also update the difference between mouse and object
  41.       direction = PVector.sub(mouse,location);
  42.       direction.normalize();//simplify PVector
  43.   }//trackMouse
  44.   
  45.  //update PVectors for movement
  46.   void move(){  
  47.       direction.mult(boost);//multiply by scalar
  48.       acceleration = direction;//transfer data from one to other
  49.       
  50.       velocity.add(acceleration);//adds new accel data to velocity
  51.       velocity.limit(10);//limits how large velocity can get
  52.       location.add(velocity); //adds velocity to location 
  53.       velocity.mult(0.9);//keeps object from going in circles around mouse
  54.   }//updateFar
  55.   
  56.   //how close is the object to mouse
  57.   //change booleans appropriately
  58.   void tests(){
  59.     //are we 'x' distance away?
  60.     if(abs(mouse.x - location.x) >= 100 && abs(mouse.y - location.y) >= 100){
  61.       isShort = false;
  62.       isFar = true;
  63.     }//if
  64.     //did the mouse just move in a straight line up,down,left,right?
  65.     if(abs(mouse.x - location.x) >= 0 && abs(mouse.y - location.y) > 100){
  66.       isShort = false;
  67.       isFar = true;
  68.     }//if
  69.     if(abs(mouse.x - location.x) > 100 && abs(mouse.y - location.y) >= 0){
  70.       isShort = false;
  71.       isFar = true;
  72.     }//if
  73.     //are we within 'x' amount of distance?
  74.     if(abs(mouse.x - location.x) < 100 && abs(mouse.y - location.y) < 100){
  75.       isFar = false;
  76.       isShort = true;
  77.     }//if
  78.   }//tests
  79.   
  80.   //update the PVectors for when object is far from mouse
  81.   void updateFar(){
  82.     if(isFar){
  83.       boost = 0.5;
  84.       println("isFar");
  85.     }//if
  86.   }//updateClose
  87.   
  88.   //update the PVectors for when object is closer to mouse
  89.   void updateShort(){
  90.     if(isShort){  
  91.       boost = 1.5;
  92.       println("isShort");
  93.     }//if
  94.   }//updateClose
  95.   
  96.   void render(){
  97.     noFill();
  98.     stroke(0);
  99.     ellipse(location.x,location.y,10,10);
  100.   }//render
  101.   
  102.   
  103. }//classEllipsoids

Replies(2)

My idea would be to make a dumb variable! 

It stores how slow an Ellipsoids would realize isFar = true already.

Thus, when mouse moves far away from them, Ellipsoids w/ less dumb would start chasing the mouse location before those w/ higher dumb!  

this is a really good idea. I'm going to look into this. it makes sense to change the timing of the state changes, or even make a couple more conditions. thank you! I'll try to update if anything works out for me.