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 › object that sways up and down
Page Index Toggle Pages: 1
object that sways up and down (Read 970 times)
object that sways up and down
May 25th, 2010, 2:46am
 
Hi. I'm trying to make a dinosaur that moves up and down but I embarrassed to say I cant seem to figure out the math to do it. Could someone please take a look at my code?

(the dinosaur shoots straight back up but I'm trying to make it rise up in gradual increments)

Thank you!

Code:
 
float a = 0;
float b = 0;
void setup()
{
 size(500, 500);
 background(255);
 smooth();

 fill(100);
 stroke(0);
}

void draw(){
 
  a = a+1;
  b = b+.5;
 
  if (a>700){a=0;}
  if (b>50){b=0;}

   
  background(255);
  fill(200);
 
beginShape();
curveVertex(-200+a, 500+b); // the first control point
curveVertex(-210+a,500+b);
curveVertex(-140+a,300+b);
curveVertex(-70+a,230+b);
curveVertex(-10+a,220+b);
curveVertex(40+a,225+b);
curveVertex(70+a,240+b);
curveVertex(80+a,290+b);
curveVertex(40+a,290+b);
curveVertex(10+a,280+b);
curveVertex(-10+a,270+b);
curveVertex(-40+a,320+b);
curveVertex(-50+a,380+b);
curveVertex(-20+a,500+b);
curveVertex(-50+a, 240+b); // is also the last control point
endShape();


fill(255);
ellipse(50+a,260+b,8,18);
ellipse(70+a,260+b,8,18);
endShape();

 }


Re: object that sways up and down
Reply #1 - May 25th, 2010, 2:57am
 
to make things move up and down, or left and right, or to make objects pulse etc. its always a good idea to use a sin function.

so you can say b = sin(frameCount)

using frameCount is just the same as declaring a variable and counting it up every frame. this would result in b changing from -1, to 1. but as we need it to be bigger, we multiply it with 60. To adjust the speed you can divide frameCount by X, in this case 10.  and to move the whole head down, i added 100 to it.

btw, you can easily change the position of your shape by using translate(a,b). its easier than adding a and b to every vertex.

Code:
float a = 0;
float b = 0;
void setup()
{
 size(500, 500);
 background(255);
 smooth();

 fill(100);
 stroke(0);
}

void draw(){
 
  a = a+2;
  b = 100+sin(frameCount/10.0)*60;
 
  if (a>700){a=0;}
 

   
background(255);
fill(200);

translate(a,b);

beginShape();
curveVertex(-200, 500); // the first control point
curveVertex(-210,500);
curveVertex(-140,300);
curveVertex(-70,230);
curveVertex(-10,220);
curveVertex(40,225);
curveVertex(70,240);
curveVertex(80,290);
curveVertex(40,290);
curveVertex(10,280);
curveVertex(-10,270);
curveVertex(-40,320);
curveVertex(-50,380);
curveVertex(-20,500);
curveVertex(-50, 240); // is also the last control point
endShape();


fill(255);
ellipse(50,260,8,18);
ellipse(70,260,8,18);
endShape();

 }
Page Index Toggle Pages: 1