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 & HelpSyntax Questions › Snow angel motion
Page Index Toggle Pages: 1
Snow angel motion (Read 658 times)
Snow angel motion
Nov 27th, 2008, 6:17pm
 
Hi

I am trying a processing sketch in which a character is making a snow angel (basically moving his arms and legs up and down) when the mouse moves up and down. The legs and arms are going to be drawn with a line().

I can't figure out how to make that arms / legs movement.

Here is a basic sketch I've been working on for one arm (it doesn't work though). Basically, I want to make the last two coordinates of the line move along with the mouse. When mouseY is in the upper part of the screen, the arm should be up, when it's at the bottom, the arm should be down. I want it to flow continually, and go back up when the mouse moves back up.

Could anybody give me any pointers as to how to make this happen? I'm really stuck...


void setup()  {
 size(1000,400);
 smooth();
 strokeWeight(5);
 strokeJoin(ROUND);
}

void draw()  {
 background(255);
 
 stroke(0);
 fill(0);

 
 if (mouseY < 20)  {
    line(200,200,200,150);
   
}
if (mouseY > 20 && mouseY < 40) {
  line(200,200,220,170);

}
if (mouseY > 40 && mouseY < 60) {
  line(200,200,190,240);

}
}
Re: Snow angel motion
Reply #1 - Nov 27th, 2008, 9:28pm
 
That's cute.

This might help you get started:

void setup()  {
 size(1000,400);
 smooth();
 strokeWeight(5);
 strokeJoin(ROUND);
}

void draw()  {
 background(255);
 
 stroke(0);
 fill(0);

/* an arm */
pushMatrix();
translate(200, 200);

float xx = map(mouseY, 0, height, 0, TWO_PI);
rotate(xx);
line(0, 50, 0, 0);

popMatrix();

 /* another arm (make these two a function!) */
pushMatrix();
translate(150, 200);

float xx2 = map(mouseY, 0, height, 0, TWO_PI);
rotate(-xx2);
line(0, 50, 0, 0);
popMatrix();
}

Re: Snow angel motion
Reply #2 - Nov 27th, 2008, 9:32pm
 
Hi!

Not sure i get correctly what you're after but here's my attempt at helping you. It basically interpolates between 0 and PI/2 the angle the arm shall be making compared to the horizontal so that it gradually moves from fully up (aka vertical facing upwards) to fully down (aka vertical facing downwards) depending on whether the mouse is above or below the middle of the screen (as compared to an horizontal axis).

Quote:
float arm_length = 100;
float x, y, angle;

void setup()  {
  size(1000,400);
  smooth();
  strokeWeight(5);
  strokeJoin(ROUND);
}

void draw()  {
  background(255);

  stroke(0);
  fill(0);

  translate(width/2,height/2); // put origin at "arm attachment"
  angle = lerp(0,HALF_PI,abs(height/2.0 - mouseY)/(height/2));
  if (mouseY < height/2) // arm shall be down
    angle *= -1;
  x = arm_length * cos(angle);
  y = arm_length * sin(angle);
  line(0,0,x,y);





Hopefully, that can give you some idea as to how to go about it.
Re: Snow angel motion
Reply #3 - Nov 28th, 2008, 2:02am
 
Thanks to both of you for your help. I definitely found a way to make my piece work (or almost).

My only problem right now is that I had snowflakes falling which I made happen this way:

float diam;
float x;
float y;

void setup()  {
 size (1000,400);
 background(255);
 frameRate(5);
 smooth();
}

void draw()  {
diam = random(20);
 x = random(width);
 y = random(height);

 stroke(0);
 fill(255);
 ellipse(x,y,diam,diam);
}

The problem is that if I put the background in draw, the snowflakes will instantly disappear  but if I put it in draw, the arms don't dissapear with the motion.. Any thoughts?
Re: Snow angel motion
Reply #4 - Nov 28th, 2008, 3:30am
 
Hmm, that's something I've not done before.

But what about using an off-screen image buffer?  I was kind of lazy in making this, but this might be a step in the right direction.  Unfortunately, smoothing is not supported with this method.  And "snowflaky" look is an artifact (change line thickness to correct for it).


(code removed: please see below for improved version of the same code)

Re: Snow angel motion
Reply #5 - Nov 28th, 2008, 3:51am
 
Just to let you know that this isn't a cheat, this won't draw the gray outline.

(Code removed: see below for consolidated version of the code - I realize it is occupying too many pixels on this thread)
Re: Snow angel motion
Reply #6 - Nov 28th, 2008, 4:11am
 
Here's the new code to allow antialiasing. Java2D seems to support smoothing, so it looks nicer.

Sorry, I just couldn't leave it alone - other people's problems are always more interesting ;)

// More Note: this code doesn't work properly (Does anyone know why?).  I've reposted the working code in the post below.
// Note: I modified the code to be shorter
float diam;
float x;  
float y;
PGraphics pg, pg2;
void setup()  {
 size (1000,400);
 background(255);
 frameRate(60);
 smooth();

/* layer1: snow */
 pg = createGraphics(1000, 400, JAVA2D);
/* layer2: angel */
 pg2 =  createGraphics(1000, 400, JAVA2D);
 
}

void draw()  {
diam = random(20);
 x = random(width);
 y = random(height);
 pg2.beginDraw();
 pg2.background(255,255);
 
 pg.stroke(0);
 pg.fill(255);
 pg.beginDraw();
 pg.smooth();
 pg.ellipse(x,y,diam,diam);
 
 pg.endDraw();
 
 ///uncomment the line below (remove "//") to allow gray trail)
 //drawAnAngel(pg, 130);
 //////  

 drawAnAngel(pg2, 0);
 pg2.endDraw();
 
 image(pg2, 0, 0);
  image(pg, 0, 0);
}  

void drawAnAngel(PGraphics pa, color c){
 pa.strokeWeight(5);
 pa.strokeJoin(ROUND);
 pa.stroke(c);
 pa.fill(0);

/* an arm */
pa.pushMatrix();
pa.translate(200, 200);

float xx = map(mouseY, 0, height, 0, TWO_PI);
pa.rotate(xx);
pa.line(0, 50, 0, 0);

pa.popMatrix();

 /* another arm (make these two a function!) */
pa.pushMatrix();
pa.translate(150, 200);

float xx2 = map(mouseY, 0, height, 0, TWO_PI);
pa.rotate(-xx2);
pa.line(0, 50, 0, 0);
pa.popMatrix();
}

Re: Snow angel motion
Reply #7 - Nov 28th, 2008, 11:09am
 
sw01 wrote on Nov 28th, 2008, 4:11am:
other people's problems are always more interesting ;)

Lol! That's so true! And personally, I find more entertaining to solve these problems than a grid of sudoku... :-) (and sometime it is even faster!).
Re: Snow angel motion
Reply #8 - Nov 28th, 2008, 8:52pm
 
Please note that I have modified the code posted above for conciseness (I hope that's ok).  FYI, I had a duplicate code for SnowAngels: one for PApplet, and another for PGraphics.  I realized I can just do two "layers" using two Pgraphics objects.

The Angel portion is wiped in one of the PGraphics object with transparent background

pg2.background(255, 255);

this allows pg object to be seen once pg2 object is drawn.


< PhiLho: Indeed Smiley

I noticed that the code above doesn't work properly. I'm reposting the original code, with a little cleanup:


float diam;
float x;  
float y;
PGraphics pg;
void setup()  {
 size (1000,400);
 background(255);
 frameRate(60);
 smooth();
 pg = createGraphics(1000, 400, JAVA2D);
 
}

void draw()  {
diam = random(20);
 x = random(width);
 y = random(height);

 background(255);
 
 pg.stroke(0);
 pg.fill(255);
 pg.beginDraw();
 pg.smooth();
 pg.ellipse(x,y,diam,diam);
 
 pg.endDraw();
 //drawAnAngel2(pg, 130);
 
 image(pg, 0, 0);
 
 drawAnAngel(this, 0);
}  

void drawAnAngel2(PGraphics pa, color c){
 pa.strokeWeight(5);
 pa.strokeJoin(ROUND);
 pa.stroke(c);
 pa.fill(0);

/* an arm */
pa.pushMatrix();
pa.translate(200, 200);

float xx = map(mouseY, 0, height, 0, TWO_PI);
pa.rotate(xx);
pa.line(0, 50, 0, 0);

pa.popMatrix();

 /* another arm (make these two a function!) */
pa.pushMatrix();
pa.translate(150, 200);

float xx2 = map(mouseY, 0, height, 0, TWO_PI);
pa.rotate(-xx2);
pa.line(0, 50, 0, 0);
pa.popMatrix();
}

void drawAnAngel(PApplet pa, color c){
  pa.strokeWeight(5);
 pa.strokeJoin(ROUND);
 pa.stroke(c);
 pa.fill(0);

/* an arm */
pa.pushMatrix();
pa.translate(200, 200);

float xx = map(mouseY, 0, height, 0, TWO_PI);
pa.rotate(xx);
pa.line(0, 50, 0, 0);

pa.popMatrix();

 /* another arm (make these two a function!) */
pa.pushMatrix();
pa.translate(150, 200);

float xx2 = map(mouseY, 0, height, 0, TWO_PI);
pa.rotate(-xx2);
pa.line(0, 50, 0, 0);
pa.popMatrix();
}

Page Index Toggle Pages: 1