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"); }