FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Programs
(Moderators: fry, REAS)
   boring brownian bug?
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: boring brownian bug?  (Read 272 times)
sinkblot

sinkblot
boring brownian bug?
« on: Apr 27th, 2004, 10:22pm »

Hey - I was just writing a simple object-based faux-brownian motion simulator, and for some reason all of my objects tend towards zero, even though it picks a random direction out of TWO_PI on each .update.  Also no matter where the starting coordinates.  Any ideas?
 
here's the code.
 
//____________object-based drawing engine skeleton
 
int num=3;
 
obj[] objs;
 
void setup(){
  size(500,500);
  background(255);
 
  objs = new obj[num];
 
  for(int j=0;j<num;j++)objs[j] = new obj(
  int(.5*width+1), //_x
  int(.5*height),  //_y
  random(TWO_PI),    //r1
  float(2)   //vel1
  );
 
}
 
void loop(){
 
  translate(.25*width,.25*height); //this is just to get a clear view of the "tends towards zero" behavior
  for(int j=0;j<num;j++)  objs[j].update(mouseX,mouseY);
}
 
class obj{
  int x,px,y,py;
  float r1;
  float vel1;
 
  obj(int _x, int _y, float _r1, float _vel1){
    this.x=_x;
 
    this.y=_y;
 
    this.r1=_r1;
    this.vel1=_vel1;
 
    this.draw();
  }
 
  void update(int newx,int newy){
 
    this.px=this.x;
    this.py=this.y;
    this.r1=random(0,TWO_PI);
 
    this.x+=this.vel1*cos(this.r1);
    this.y+=this.vel1*sin(this.r1);
    this.draw();
  }
 
  void draw(){
 
    line(this.x,this.y,this.px,this.py);
 
  }
 
}
 
TomC

WWW
Re: boring brownian bug?
« Reply #1 on: Apr 27th, 2004, 11:58pm »

Try using floats for x,px,y,py?  I think you are losing accuracy every time you add small floats onto your ints.
 
sinkblot

sinkblot
Re: boring brownian bug?
« Reply #2 on: Apr 28th, 2004, 1:37am »

perfect, thanks.  Thought it was something obvious I was missing.
 
Pages: 1 

« Previous topic | Next topic »