random in draw
in
Programming Questions
•
1 year ago
Hi,
I'm making an animation in Processing. I have some random points moving randomly in the background and some points that go forming a solid. Since I need to view the animation in stereo, I have to render the left vision, save the frames and then make a movie out of them. I need to make this also for the right vision.
So I need to run the same animation with the same random points at least twice. This is possible using the random class and not the random() method. But the problem is that I also have a random call in the draw method, but if I change random() to random.nextInt() it doesn't work properly.
Here's my code:
- import processing.opengl.*;
- int xsize=1000;
- int ysize=1000;
- int zsize=1000;
- float halfxsize;
- float halfysize;
- float halfzsize;
- float vel=10;
- float velr=10;
- float solidx=width/2;
- float solidy=height/2;
- float solidz=1;
- float solidxrotation=-PI/6.0;
- float solidyrotation=-PI/6.0;
- float solidzrotation=0;
- float density=0.004;
- int numr=500;
- int nums;
- int ballsize=5;
- float sradius=100;
- PVector[] randomp; //punti random
- PVector[] solidp; //punti solido
- //variabili sfera
- int spheresup;
- float[] xsphere;
- float[] ysphere;
- float[] zsphere;
- float randomMvmtRange=3;
- float solidMvmtRange=3;
- float[] randomMvmt;
- float[] solidMvmt;
- void setup() {
- randomp = new PVector[numr];
- halfxsize = xsize/2;
- halfysize = ysize/2;
- halfzsize = zsize/2;
- float stheta;
- float sphi;
- spheresup = (int)(4*PI*sradius*sradius*density);
- xsphere = new float[spheresup];
- ysphere = new float[spheresup];
- zsphere = new float[spheresup];
- Random randsolidxsize = new Random(10);
- Random randsolidysize = new Random(20);
- Random randsolidzsize = new Random(30);
- Random randstheta = new Random(1);
- Random randsphi = new Random(2);
- Random randrandxsize = new Random(1);
- Random randrandysize = new Random(2);
- Random randrandzsize = new Random(3);
- for(int i=0; i<numr; i++) {
- randomp[i] = new PVector(randomF(randrandxsize, -xsize/2, xsize/2), randomF(randrandysize, -ysize/2, ysize/2), randomF(randrandzsize, -zsize/2, zsize/2));
- }
- solidp = new PVector[spheresup];
- nums = spheresup;
- for (int i=0; i<solidp.length; i++) {
- solidp[i] = new PVector(randomF(randsolidxsize, -xsize/2, xsize/2), randomF(randsolidysize, -ysize/2, ysize/2), randomF(randsolidzsize, -zsize/2, zsize/2));
- stheta = TWO_PI * randomF(randstheta, 0, 1);
- sphi = acos(2 * randomF(randsphi, 0, 1) - 1);
- xsphere[i] = cos(stheta)*sin(sphi)*sradius;
- ysphere[i] = sin(stheta)*sin(sphi)*sradius;
- zsphere[i] = cos(sphi)*sradius;
- }
- randomMvmt = new float[numr];
- solidMvmt = new float[nums];
- for(int i=0; i<numr; i++) {
- randomMvmt[i] = random(-1,1) * randomMvmtRange;
- }
- for(int i=0; i<nums; i++) {
- solidMvmt[i] = random(-1,1) * solidMvmtRange;
- }
- colorMode(HSB);
- size(screen.width, screen.height, OPENGL);
- camera(0, 0, 500, 0, 0, 0, 0, 1, 0);
- }
- void draw() {
- background(0);
- initDisplacement();
- renderSphere(sradius);
- renderBackground();
- }
- void initDisplacement() {
- Random randk = new Random(1);
- int k;
- for (int i=0; i<numr; i++) {
- //k = randk.nextInt(numr-4);
- k = (int)random(0, numr-4);
- randomp[i].x+=randomMvmt[k];
- randomp[i].y+=randomMvmt[k+1];
- randomp[i].z+=randomMvmt[k+2];
- //collision detection
- if (randomp[i].x>halfxsize) {
- randomp[i].x=halfxsize;
- randomp[i].x*=-1;
- }
- if (randomp[i].y>halfysize) {
- randomp[i].y=halfysize;
- randomp[i].y*=-1;
- }
- if (randomp[i].z>halfzsize) {
- randomp[i].z=halfzsize;
- randomp[i].z*=-1;
- }
- if (randomp[i].x<-halfxsize) {
- randomp[i].x=halfxsize;
- randomp[i].x*=-1;
- }
- if (randomp[i].y<-halfysize) {
- randomp[i].y=halfysize;
- randomp[i].y*=-1;
- }
- if (randomp[i].z<-halfzsize) {
- randomp[i].z=halfzsize;
- randomp[i].z*=-1;
- }
- }
- for (int i=0; i<nums; i++) {
- k = (int)random(0, nums-4);
- solidp[i].x+=solidMvmt[k];
- solidp[i].y+=solidMvmt[k+1];
- solidp[i].z+=solidMvmt[k+2];
- //collision detection
- if (solidp[i].x>halfxsize) {
- solidp[i].x=halfxsize;
- solidp[i].x*=-1;
- }
- if (solidp[i].y>halfysize) {
- solidp[i].y=halfysize;
- solidp[i].y*=-1;
- }
- if (solidp[i].z>halfzsize) {
- solidp[i].z=halfzsize;
- solidp[i].z*=-1;
- }
- if (solidp[i].x<-halfxsize) {
- solidp[i].x=halfxsize;
- solidp[i].x*=-1;
- }
- if (solidp[i].y<-halfysize) {
- solidp[i].y=halfysize;
- solidp[i].y*=-1;
- }
- if (solidp[i].z<-halfzsize) {
- solidp[i].z=halfzsize;
- solidp[i].z*=-1;
- }
- }
- }
- void renderBackground() {
- background(0);
- pushMatrix();
- //random points
- for (int i=0; i<numr; i++) {
- pushMatrix();
- translate(randomp[i].x, randomp[i].y, randomp[i].z);
- fill(0, 0, 80);
- noStroke();
- sphereDetail(3);
- sphere(3);
- popMatrix();
- }
- //solid points
- pushMatrix();
- rotateX(solidxrotation);
- rotateY(solidyrotation);
- rotateZ(solidzrotation);
- for (int i=0; i<nums; i++) {
- pushMatrix();
- translate(solidp[i].x, solidp[i].y, solidp[i].z);
- fill(0, 0, 100);
- noStroke();
- sphereDetail(3);
- sphere(3);
- popMatrix();
- }
- popMatrix();
- popMatrix();
- }
- void renderSphere(float sradius) {
- for (int i=0; i<spheresup; i++) {
- if(solidp[i].x>xsphere[i]) solidp[i].x-=vel;
- if(solidp[i].x<xsphere[i]) solidp[i].x+=vel;
- if(solidp[i].y>ysphere[i]) solidp[i].y-=vel;
- if(solidp[i].y<ysphere[i]) solidp[i].y+=vel;
- if(solidp[i].z>zsphere[i]) solidp[i].z-=vel;
- if(solidp[i].z<zsphere[i]) solidp[i].z+=vel;
- }
- }
- float randomF(Random rand, float minv, float maxv) {
- return minv + rand.nextFloat() * (maxv - minv);
- }
I want the points in the background to keep moving in [-3, 3] in the same way for at least two runs.
If I use random() the number is choosed randomly every frame, but if I use rand.nextInt() for every frame I get the same numbers, (frame 1: 13 45 234 65 etc. frame 2: 13 45 234 65) and the points keep going away... Hope I explain myself, to see it just uncomment
k = randk.nextInt(numr-4);
Is there any way I can get the same movement twice? Do I need to put a noLoop() somewhere? Am I hopeless?
Thanks.
1