Jordan Parsons
- Second Year Architect

Processing – (Not so) Glitchy Gravity

Posted By: Jordan Parsons

Well I’m trying to wrap my head around vectors in processing because I am not giving up on my flocking simulation and vectors seem to be key to the navigation. So I am following some tutorials by Daniel Shiffman. They are some very nice tutorials and I worked on adapting his gravity tutorial, so that the objects rotate, and move side to side and decelerate when they hit walls. I am having some serious issues with glitching when they settle to the floor and every now an then one goes right through a wall. I can’t really explain it. As always any tips or thoughts are appreciated and welcome. By the way hit space to smack the boids up in the air. http://www.jordanparsons.com/processing/gravity_v1/

Edit:
Thanks to Adam Thomas I made a series of changes to the code, and its much more stable. I also added a holder class so that I can implement the boids with a for loop instead of by hand. The new source is bellow, the old is still avaliable if you check the old app above. Here is the new version.

http://www.jordanparsons.com/processing/gravity_v2/

Gravity

Gravity:

int bcolors,bcolor,color_ball;
Ball_h ball_h;

void setup(){
  smooth();
  ellipseMode(CENTER);
  size(300,700);
  background(100);
  ball_h = new Ball_h();

  for (int i = 0; i < 4; i++) {
    PVector a = new PVector(0,random(.1,.5));
    PVector v = new PVector(random(1,8),random(10,25));
    PVector l = new PVector(random(1,8),height-random(0,50));
    color_ball = int(random(0,255));
    ball_h.addBall(new Ball(a,v,l,color_ball));
  }
}
void draw(){
  background(100);
  ball_h.run();
}
void drawVector(PVector v, PVector loc, float scayl) {
  pushMatrix();
  float arrowsize = 4;
  // Translate to location to render vector
  translate(loc.x,loc.y);
  stroke(0);
  // Call vector heading function to get direction (note that pointing up is a heading of 0) and rotate
  rotate(v.heading2D());
  // Calculate length of vector & scale it to be bigger or smaller if necessary
  float len = v.mag()*scayl;
  // Draw three lines to make an arrow (draw pointing up since we've rotate to the proper direction)
  line(0,0,len,0);
  line(len,0,len-arrowsize,+arrowsize/2);
  line(len,0,len-arrowsize,-arrowsize/2);
  popMatrix();
}

Ball:

class Ball{
  PVector loc;
  PVector acc;
  PVector vel;
  PVector flat;
  int once, twice, bcolors, bcolor, rotate_on;
  float x_loss, y_loss,x_pos,y_pos, turn;

  Ball(PVector a, PVector v, PVector l,int bcolors){
    acc=a.get();
    bcolor=bcolors;
    vel=v.get();
    loc=l.get();
    once=1;
    rotate_on=1;
    x_loss=.75;
    y_loss=2;
    PVector flat = new PVector(width/2,height/2);
  }
void run(ArrayList balls){
    update();
    render();
  }
  void update(){
    if(keyPressed) {
      if (key == ' '){
        vel.y=-10;
        rotate_on=1;
        if(vel.x==0){
          vel.x=6;
          acc.y=random(.1,.5);
          once=1;
        }
      }
    }

    if(loc.x>width){
      vel.x*=-1; // change direction
      vel.x+=x_loss; // dampen the velocity
      loc.x=width; // set location to outside the collision area
    }
    if(loc.x<0){
      vel.x*=-1;
      vel.x-=x_loss;
      loc.x=1;
    }

    if(loc.y>height){
      vel.y*=-1;
      vel.y+=y_loss;
      vel.x-=x_loss; // also dampen the x movement a bit
      loc.y=height;

      // may as well only do this test when you are bouncing on the ground
      // remember that your velocities can be negative too!
      if(loc.y>height-3){
        //println("yes");
        //acc.x=0;
        // vel.x=0;
        //vel.y=0;
        if(vel.y<.9 && vel.y>-.9){
          if(vel.x<2 && vel.x>-2){
            vel.x=0;
            vel.y=0;
            acc.y=0;
            rotate_on=0;
            // println("yessssssss");
          }
        }
      }
    }

    vel.add(acc);
    loc.add(vel);
    //println(vel.get());

    x_pos=loc.x;
    y_pos=loc.y;
    turn = vel.heading2D();
  }
  void render(){
    ellipseMode(CENTER);
    stroke(0);
    fill(bcolor,150);
    pushMatrix();
    translate(loc.x,loc.y);
    if(rotate_on==1){
      rotate(turn);
    }
    ellipse(0,0,30,10);
    popMatrix();
    drawVector(vel,loc,5);

  }
}

Balls:

class Ball_h{
  ArrayList balls;

  Ball_h(){
    balls = new ArrayList();
  }
  void run(){
    for(int n=0; n < balls.size(); n++){
      Ball b = (Ball) balls.get(n);
      b.run(balls);
    }
  }
  void addBall(Ball b){
    balls.add(b);
  }
}

3 Comments »

      Comment by adam
      March 31, 2009 @ 2:05 am

    Hey, found you on #processing.

    Had a play with your code. Here are a few of the changes I made to simplify the changes in x velocity

    set your x_loss to be between 0 and 1. eg. 0.90
    this will set the x velocity to 90% of the old velocity

    this dampening should mean that eventually you will match the condition to clamp the velocities and acceleration

        if(loc.x>width){
          vel.x*=-1; // change direction
          vel.x*=x_loss; // dampen the velocity
          loc.x=width; // set location to outside the collision area
        }
        if(loc.xheight){
          vel.y*=-1;
          vel.y*=y_loss;
          vel.x*=x_loss; // also dampen the x movement a bit
          loc.y=height;
    
          // may as well only do this test when you are bouncing on the ground
          // remember that your velocities can be negative too!
          if(vel.y-.9){
            if(vel.x-.5){
              acc.x=0;
              vel.x=0;
              vel.y=0;
            }
          }
        }
    
     

      Comment by adam
      March 31, 2009 @ 2:11 am

    arg. silly me! not escaping the < > and it would appear that <code&gt doesn’t work either. let me try again.

    
        if(loc.x>width){
          vel.x*=-1; // change direction
          vel.x*=x_loss; // dampen the velocity
          loc.x=width; // set location to outside the collision area
        }
        if(loc.x<0){
          vel.x*=-1;
          vel.x*=x_loss;
          loc.x=(1);
        }
    
        if(loc.y>height){
          vel.y*=-1;
          vel.y*=y_loss;
          vel.x*=x_loss; // also dampen the x movement a bit
          loc.y=height;
    
          // may as well only do this test when you are bouncing on the ground
          // remember that your velocities can be negative too!
          if(vel.y<.9 && vel.y>-.9){
            if(vel.x<.5 && vel.x>-.5){
              acc.x=0;
              vel.x=0;
              vel.y=0;
            }
          }
        }
    
     

      Comment by Jordan Parsons
      March 31, 2009 @ 8:45 am

    Thanks for the edits, I’ll have to try them out after class today and I’ll let you know how it goes.

    By the way I use this for syntax highlighting.
    <pre class=”brush: java”>

    </pre>

     

RSS feed for comments on this post. TrackBack URL

Leave a comment



© 2009 Jordan Parsons
Grab the feed