Continuous rotation in Processing -
i wrote program in procesisng renders opaque cubes random colour , rotation on top of each other, i'm looking individually continuously spin each cube while program running. here's code @ moment,
int boxval = 1; void setup(){ size (640, 320, p3d); framerate(60); } void draw(){ (int = 0; < boxval; i++){ translate(random(0,640), random(0,320), 0); rotatey(random(0,360)); rotatex(random(0,360)); rotatez(random(0,360)); fill(random(0,255),random(0,255),random(0,255),50); nostroke(); box(64,64,64); } }
here's screenshot if helps @ all,
this great time use object oriented programming! if understand question correctly, each cube rotate independently of other cubes. let's make cube class. think of each cube object handle individually.
class cube { float x, y, z; // position of cube float size; // size of cube color c; // color of cube float xangle, yangle, zangle; // current rotation amount of cube's x, y, z axes float xspeed, yspeed, zspeed; // how cube rotated in x, y, z axes // cube constructor - create cube , of parameters cube(float x_, float y_, float z_, float size_, color c_, float xspeed_, float yspeed_, float zspeed_) { x = x_; y = y_; z = z_; size = size_; c = c_; xspeed = xspeed_; yspeed = yspeed_; zspeed = zspeed_; xangle = yangle = zangle = 0; // starting position } // update cube // we're doing rotating each axis void update() { xangle += xspeed; yangle += yspeed; zangle += zspeed; } // draw cube screen void display() { pushmatrix(); // need translate(x, y, z); // position on screen rotatex(xangle); // rotation amounts rotatey(yangle); rotatez(zangle); fill(c); nostroke(); box(size); popmatrix(); // , // push , pop matrix allows individual cube rotation // otherwise rotate whole draw window, isn't you're looking } }
if each cube change color , position on screen still rotate independently, display()
function instead:
void display() { pushmatrix(); translate(random(0, width), random(0, height), random(-100, 100)); // random position on screen rotatex(xangle); rotatey(yangle); rotatez(zangle); fill(random(255), random(255), random(255), 50); // random color nostroke(); box(size); popmatrix(); }
understanding rotation , translation of elements in processing key. highly recommend this tutorial processing website if have not read it. incorporated concepts cube class.
since have more 1 cube drawn on screen, let's make array of cubes. chose 25 arbitrary number.
cube[] cube = new cube[25];
now in setup()
, we'll need create each cube , give parameters, position on screen, color, etc. here how accomplished.
for (int = 0; < cube.length; i++) { cube[i] = new cube(random(0, width), random(0, height), 0, // x, y, z position random(30, 80), color(random(255), random(255), random(255), 50), // size, color random(0.001, 0.020), random(0.001, 0.020), random(0.001, 0.020)); // xspeed, yspeed, zspeed }
now need draw cubes screen , update rotation of each one, happens in draw()
loop.
for (int = 0; < cube.length; i++) { cube[i].update(); cube[i].display() }
here whole program. it's important call background()
each time through draw()
loop display window cleared each frame. comment out see happen, noticed not in code snippet provided above. guess can effect though!
cube[] cube = new cube[25]; void setup() { size(640, 320, p3d); smooth(); framerate(60); (int = 0; < cube.length; i++) { cube[i] = new cube(random(0, width), random(0, height), 0, random(30, 80), color(random(255), random(255), random(255), 50), random(0.001, 0.020), random(0.001, 0.020), random(0.001, 0.020)); } } void draw() { camera(); lights(); background(50); (int = 0; < cube.length; i++) { cube[i].update(); cube[i].display(); } }
i'm not sure programming background is, getting hang of object oriented programming helpful in processing (and other oop languages), i'd recommend this oop tutorial processing website if need crash course. programming life changed when oop made sense.
Comments
Post a Comment