Willkommen auf unserem Seminar-Blog

Immer auf dem aktuellen Stand bleiben

Dieser Seminar-Blog befindet sich noch im Aufbau und wird in den kommenden Tagen entsprechend verfeinert.

Member Login

Lost your password?

Registration is closed

Sorry, you are not allowed to register by yourself on this site!

You must either be invited by one of our team member or request an invitation by email at viad.info {at} zhdk {dot} ch.

Lektion 6 Aufgabe 3

10. November 2010

In this assignment we had to animate the forward and backward movement of the ball on the drawn line. Here I had alot of problems defining the PVector numbers and understanding how the work. At first they where initialized wrong so i had errors using the if else statement.
PVector     startPos = new PVector();
PVector     endPos = new PVector();
int         curTime = 0;
int         animSpeed = 5;
int         animTime = 2000;
boolean     drawFlag=false;

boolean forward = true;
PVector pos;
PVector dir;


void setup()
{
  size(640, 480);
  smooth();
}

void draw()
{
  background(51);

  // calc. the anim time
  curTime += animSpeed;
  if(curTime >= animTime){
    forward=!forward;
    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);

   if (forward) {
    // calculate the position of the circle on the line
    dir = PVector.sub(endPos,startPos);
    pos = PVector.add( startPos , PVector.mult(dir,normTime));
   
   } else {
     dir = PVector.sub(startPos,endPos);
     pos = PVector.add( endPos , 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;  Makes the line dissapear.
  println("released");
}