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 › noise - class or instance
Page Index Toggle Pages: 1
noise - class or instance (Read 339 times)
noise - class or instance
Dec 28th, 2007, 3:28am
 
Hi there.

I'm trying to create a class that wanders around the screen in a pseudo natural path. I'm taken daniel shiffman's walker class, and used perlin noise instead of random() to create a more natural effect. It all works as expected, but if I create 2 instances of the class they both move together.

Does noise() always produce the same numbers, or is there a way for it to create different numbers for each instance

Here's a simplification of the code:

Using random() Works as expected, each instance moves on it's own random path
Code:

void walk() {
int vx = int(random(3))-1;
int vy = int(random(3))-1;
x += vx;
y += vy;
}


Using noise() All instances move together
Code:

int jumpSize = 22;
float xoff = 0.0;
float xincrement = 0.1;
float yoff = 0.0;
float yincrement = 0.11;

void walk() {
float vx = (noise(xoff)*jumpSize) - jumpSize/2;
float vy = (noise(yoff)*jumpSize) - jumpSize/2;
x += vx;
y += vy;
xoff += xincrement;
yoff += yincrement;
}


I've looked through the documentation and the Processing book (I just got for christmas :) but can't find anything that would explain this behaviour. I suspect I need to define, or initiate noise, to ensure it's instance level, rather than class level.

Any pointers in the right direction?

Thanks
Re: noise - class or instance
Reply #1 - Dec 28th, 2007, 3:53am
 
Noise will always return the same value for the same argument (at least within a single run)

To get the effect you want, give each instance an "offset" value which is a random number, and add that to the argument for the noise function.
Re: noise - class or instance
Reply #2 - Dec 28th, 2007, 5:40pm
 
Thanks very much.

I just set the offset to a random number in the constructor and it's now working quite well.

Thanks for your help! Smiley
Page Index Toggle Pages: 1