課題
今、processing3の3Dマッピングを使って、水(?)の落下を再現したいと考えております。
まず2dでellipseを描写したところうまくいきました。以下コードです。
int numDrops = 4000; drop[] drops = new drop[numDrops]; float maxRad = 10; float gravity = 9.8/20; void setup(){ size(480, 720); background(255); smooth(); frameRate(15); for(int i = 0; i < numDrops; i++){ int c = color(0, random(200, 255), random(200, 255), random(100, 255)); drops[i] = new drop(random(200, width-200), random(0, height/4), random(2, maxRad), c, i); } } void draw(){ background(0); for(int j = 0; j < numDrops; j++){ drops[j].display(); drops[j].move(); drops[j].update(); } if(keyPressed){ if(key == 's'){ saveFrame("frames/frame_####.tif"); } } } class drop{ float posx, posy; float velx, vely; float rad; color clr; int idno; drop(float x, float y, float r, color c, int id){ posx = x; posy = y; velx = random(-4,4); vely = sqrt(2*gravity*posy); rad = r; clr = c; idno = id; } void display(){ fill(clr); noStroke(); ellipse(posx, posy, rad*2, rad*2); } void move(){ posx += velx; posy += vely; vely = sqrt(2*gravity*posy); } void update(){ if((posx >= width+rad) || (posy >= height+rad)){ color c = color(0, random(200, 255), random(200, 255), random(100, 255)); drops[idno] = new drop(random(200, width-200), random(0, height/4), random(2, maxRad), c, idno); } } }
これを本来の目的である三次元描写にしようとし、以下のコードに書き換えました。
int numDrops = 200; drop[] drops = new drop[numDrops]; float maxRad = 10; float gravity = 9.8/20; void setup(){ size(480, 720,P3D); background(0); smooth(); frameRate(15); for(int i = 0; i < numDrops; i++){ int c = color(0, random(200, 255), random(200, 255), random(100, 255)); drops[i] = new drop(random(200, 280), random(0, 150),random(200,240), random(2, maxRad), c, i); } noStroke(); } void draw(){ background(0); lights(); for(int j = 0; j < numDrops; j++){ drops[j].display(); drops[j].move(); drops[j].update(); } translate(240,360,-150); rotateX(map(mouseY, 0, height, PI, -PI)); rotateY(map(mouseX, 0, width, -PI, PI)); } class drop{ float posx, posy, posz; float velx, vely, velz; float rad; color clr; int idno; drop(float x, float y,float z, float r, color c, int id){ posx = x; posy = y; posz = z; velx = random(-4,4); vely = sqrt(2*gravity*posy); velz = random(-4,4); rad = r; clr = c; idno = id; } void display(){ fill(clr); noStroke(); translate(posx,posy,posx); sphere(rad*2); // ellipse(posx, posy, rad*2, rad*2); } void move(){ posx += velx; posy += vely; posz += velz; vely = sqrt(2*gravity*posy); } void update(){ if((posx >= 480+rad) || (posy >= 720+rad)){ color c = color(0, random(200, 255), random(200, 255), random(100, 255)); drops[idno] = new drop(random(200, 240), random(0, 150),random(200, 240), random(2, maxRad), c, idno); } } }
このコードで実行してみても動きませんでした。。。
考え方的にはあっていると思っているので私ではどうにもできそうにないです。わかる方いらしゃったらご教授ねがいます。
よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。