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 & HelpPrograms › Hit the Edge of the Window
Page Index Toggle Pages: 1
Hit the Edge of the Window (Read 822 times)
Hit the Edge of the Window
Feb 11th, 2007, 1:38pm
 
I write a little prog that moves particles in a vector field getting his direction from a perlin noise field.
No problemo until now.

I want the particles going out of the window to appear on the other side:

   if (x<0){
     x=width;
   }
   if (x>width){
     x=0;
   }
   if (y<0){
     y=height;
   }
   if (y>height){
     y=0;
   }

but the particles seems to stock an the edges for a momend and then come out in a lump.

i tried to put them in a distance of 5px to the edge, but this does not fix it.

Please help me.
Re: Hit the Edge of the Window
Reply #1 - Feb 11th, 2007, 2:15pm
 
The pixels in the x direction on your applet are numbered from 0 to width - 1. This is because for "width" amount of pixels we have to count 0.

If you're using a vector field, my guess is that you're letting the particles drop off of the v-field at x=width or y=height. You've created a pixel width trench at the right bottom edge of the applet.

Try these rules for wrapping instead:

Code:

if(x < 0) x = width - 1;
if(x > width - 1) x = 0;
if(y < 0) y = height- 1;
if(y > height- 1) y = 0;


If that doesn't work, it must be another part of your code. Which means you'll need to post more code.
Re: Hit the Edge of the Window
Reply #2 - Feb 11th, 2007, 2:34pm
 
thank you!

I thought, the last x is width... so i step out of thew field...
Re: Hit the Edge of the Window
Reply #3 - Feb 11th, 2007, 4:36pm
 
I tried, but it does not work:(
i tried also larger distances from left and right edge...

Here is the Code:

import processing.opengl.*;

int anzahl=10000;
float[][] P = new float[anzahl][2];

float pns=0.005; //Perlin noise size
float ps=10; //Patternsize Vectorfield
float w;//Winkel
float vx,vy;
float x=100,y=100,z;
float v0=1; //Geschwindigkeit


void setup(){
size(800,800,OPENGL);
background(0);
pointrand();
}

void draw(){

fill(0,100);
rect(0,0,width,height);
z+=5;
if (mousePressed && (mouseButton == RIGHT)) {
pointrand();
}
if (mousePressed && (mouseButton == LEFT)) {
feld();
}
for (int i=0;i<anzahl;i++){
x=P[i][0];
y=P[i][1];
w=noise(x*pns,y*pns,z*pns)*TWO_PI;
vx=sin(w);
vy=cos(w);
x+=vx*v0;
y+=vy*v0;

if(x < 0) x = width - 1;
if(x > width - 1) x = 0;
if(y < 0) y = height- 1;
if(y > height- 1) y = 0;

P[i][0]=x;
P[i][1]=y;
stroke(w/TWO_PI*255,255-w/TWO_PI*255,0,100);
line(x,y,x+vx*5,y+vy*5);
}
}

void feld(){
for(int x1=0;x1<width;x1+=ps){
for(int y1=0;y1<width;y1+=ps){
w=noise(x1*pns,y1*pns,z*pns)*TWO_PI;
stroke(w/TWO_PI*255,150);
line(x1,y1,x1+ps*sin(w),y1+ps*cos(w));
}
}
}

void pointrand(){
for (int i=0;i<anzahl;i++){
P[i][0]=random(0,width);
P[i][1]=random(0,height);
}
}
Re: Hit the Edge of the Window
Reply #4 - Feb 11th, 2007, 5:23pm
 
I'm not an expert on vector fields (it looks really pretty though), but my guess is that seeing as the bunching isn't a consistent effect it might be down to conflicting directions of the field at either side of the sketch.

The particles drift off stage left and hit vectors pushing them back the other way. Thus you get a bundle of particles caught at the side that are released as a group when the vector field spins round to let them free.

If you're going to wrap around you'll need to interpolate the direction of the vectors on opposite edges. That's my theory anyway. Sorry I haven't got a solution for you - perhaps somebody else has some thoughts on this?
Page Index Toggle Pages: 1