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 › Seamlessly random floating movement
Page Index Toggle Pages: 1
Seamlessly random floating movement (Read 1379 times)
Seamlessly random floating movement
Apr 4th, 2009, 12:40pm
 
Hi,

I am trying to make a piece of text or shape move randomly around the stage as if it has a life of its own. My big sleepy head hasn't been able to figure out this Sad I would very much appreciate some help on how to do this. Apologies if I have missed another thread that answers this.
Re: Seamlessly random floating movement
Reply #1 - Apr 4th, 2009, 1:21pm
 
Its not really "floating" but maybe brownian motion is something for you:
http://processing.org/learning/topics/brownian.html
Re: Seamlessly random floating movement
Reply #2 - Apr 4th, 2009, 1:23pm
 
thats brownian motion too, but maybe its a better one for your purpose :


Wanderer[] wanderer = new Wanderer[1];

void setup()
{
 size(500, 500);
 background(50);
 fill(200);
 noStroke();
 
 for (int i=0; i<wanderer.length; i++)
 {
   wanderer[i] = new Wanderer(random(width), random(height));
 }
}

void draw()
{
 background(50);
 
 for (int i=0; i<wanderer.length; i++)
 {
   wanderer[i].stayInsideCanvas();
   wanderer[i].move();
   ellipse(wanderer[i].getX(), wanderer[i].getY(), 2, 2);
 }
}
class Wanderer
{
 float x;
 float y;
 float wander_theta;
 float wander_radius;
 
 // bigger = more edgier, hectic
 float max_wander_offset = 0.3;
 // bigger = faster turns
 float max_wander_radius = 3.5;
 
 Wanderer(float _x, float _y)
 {
   x = _x;
   y = _y;
   
   wander_theta = random(TWO_PI);
   wander_radius = random(max_wander_radius);
 }
 
 void stayInsideCanvas()
 {
   x %= width;
   y %= height;
 }
 
 void move()
 {
   float wander_offset = random(-max_wander_offset, max_wander_offset);
   wander_theta += wander_offset;
   
   x += cos(wander_theta);
   y += sin(wander_theta);
 }
 
 float getX()
 {
   return x;
 }
 
 float getY()
 {
   return y;
 }
}
Re: Seamlessly random floating movement
Reply #3 - Apr 4th, 2009, 2:36pm
 
thank you! that is pretty much what i was looking for! i should be able to get a better grasp of what i am trying to do now! Big help. thanks again!
Re: Seamlessly random floating movement
Reply #4 - Apr 6th, 2009, 9:20am
 
IMHO, you'll get more aesthetic results from movement based on the noise() function:

http://processing.org/reference/noise_.html
Page Index Toggle Pages: 1