Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 8 Next »

PVector     startPos = new PVector();
PVector     endPos = new PVector();
int         curTime = 0;
int         animSpeed = 5;
int         animTime = 2000;
boolean     drawFlag=false;
 
void setup()
{
  size(640, 480);
  smooth();
}
 
void draw()
{
  background(51);
 
  // calc. the anim time
  curTime += animSpeed;
  if(curTime >= animTime) {
     curTime = 0;
 }
  // calc. the current time in the animation
  float normTime = curTime * 1.0 / animTime;
 
  if(drawFlag)
  {
    stroke(255);
    line(startPos.x,startPos.y,
         endPos.x,endPos.y);
 
    // calculate the position of the circle on the line
    PVector dir = PVector.sub(endPos,startPos);
 
    PVector pos = PVector.add( startPos , PVector.mult(dir,normTime));
    ellipse(pos.x,pos.y, 20,20);
  }
 
}
 
void mousePressed()
{
  drawFlag = true;
  curTime = 0;
  startPos.set(mouseX,mouseY,0);
  endPos = startPos.get();
}
 
void mouseDragged()
{
  endPos.set(mouseX,mouseY,0);
}
 
void mouseReleased()
{
  drawFlag = false;
  println("released");
}

Exercise 11

Modified the example to do the following:

  1. The path of the animation is to be defined by mouse click
  2. The ball should move forward and backwards endlessly

Animation-2 Lösung