We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi Forum!
I've been trying to code a program where I have 12 different .svg snowflakes and am trying to get them to "snow" and rotate. The plan is to get them to start above the canvas and fall at a speed depending on the size/scale that will be randomized as well as rotating each flake. So far I've honesty dont have much.
PShape[] flake_shapes;
float[] x;
float[] y;
float[] size;
float[] spin;
float[] spinspeed;
int n = 100;
void setup()
{
size( 500, 500 );
x = new float[ n];
y = new float[ n];
size = new float[ n];
spin = new float[ n];
spinspeed = new float[ n];
flake_shapes = new PShape[ 12 ];
for ( int idx = 0; idx < 12; ++idx ) {
flake_shapes[idx] =
loadShape( "snowflake" + (idx+1) + ".svg" );
}
frameRate(6);
}
void draw()
{
background( 200, 200, 255 );
updateFlakes();
//drawFlakes();
}
void updateFlakes() {
for ( int i = 0; i < n; i++ ) {
x[ i] = random( 0, width);
y[ i] = random( 0, - 100);
size [ i] = random( 0, 1);
spin[ i] = random( 0, 15);
spinspeed[ i] = random( 0, 2);
// Just a quick test to draw anything at all.
for ( int idx = 0; i < 12; i++) {
shape( flake_shapes[i], 0, 0 );
}
}
}
void drawFlakes() {
// Just a quick test to draw anything at all.
for ( int i = 0; i < 12; i++) {
shape( flake_shapes[i], 0, 0 );
}
}
and I'm wondering if you guys got any tips/hints?
Thanks
Comments
here
you should define your snowflakes in setup()
then you move them in update
and draw them in drawFlakes() {
btw
random must have the smaller number first, so it's random (-100,0) and not random( 0, - 100);
I couldn't use your shapes because I don't have them. But you how it's done from my sketch.
;-)
Hi Chris,
Thanks for the reply, I took what you told me and slapped my image into it and it worked. The only problems Im having is that for scaling each flake I decided to put it in drawFlakes, above the shape but then the flakes stop "snowing".
For rotating the flakes I believe I should be putting it into the update?
Thanks
you always have to distinguish between setting values (eg. x[3]) and using them
in general you set them in setup and use it in draw()
and modify it in your update
so for scaling: after line 62 pushMatrix();
scale(size[i]);
rect.......
popMatrix();
for rotating see reference on rotate
you rotate in drawflakes and change the rotation in update
rotating is more complicate....
do you have to?
because to rotate you must rotate around the origin
so it's like
pushMatrix();
translate (x[..,y[...);
rotate(....
shape(..., 0,0); // !!!!!! 0,0
popMatrix();
crazy
;-)
here
you got now 2 indpendent for loops
you might as well have one for-loop in draw() and call both functions from there (and the functions don't have a for-loop)
giving the index as parameter
;-)