2016-04-01 126 views
0

我使用lerp()函數來移動我的圓圈,但它不起作用。取決於我的amt參數是lerp()函數的什麼地方,這個圓總是在某個地方結束。如果我把0.5放在線上,那麼這個圓就放在線的中間,但我看不到它移動,圓也不能沿着圓的長度移動。那麼,任何人都可以幫助我使圓圈向下移動嗎?動畫圈跨線移動

float x1,y1,x2,y2; 
float cx,cy; 
float x4,y4; 

void setup() { 
    size(600,600); 
    x1 = 200; 
    y1 = 150; 
    x2 = 300; 
    y2 = 250; 
    cx = 450; 
    cy = 200; 
} 

void draw() { 
    background(60); 
    stroke(220); 
    line(x1,y1,x2,y2); 
    noFill(); 
    noStroke(); 
    // calculate the point 
    float k = ((y2-y1) * (cx-x1) - (x2-x1) * (cy-y1)) 
/((y2-y1)*(y2-y1) + (x2-x1)*(x2-x1)); 
    float x4 = cx - k * (y2-y1); 
    float y4 = cy + k * (x2-x1); 
    stroke(0); 
    line(cx,cy,x4,y4); //line connecting circle and point on line 

    float x = lerp(cx, x4, .1); 
    float y = lerp(cy, y4, .1); 

    fill(255, 0, 175); 
    ellipse(x4,y4, 8,8); 

    fill(175, 0, 255); 
    ellipse(x, y, 50, 50); 
} 
+0

我不確定要理解你的問題,但我是對的,你想要沿着黑色線圈動畫圈?如果是這樣,你必須改變draw函數中amt參數的值。每次迭代只需使用一個變量即可。 – michaPau

+0

謝謝!我得到它的工作! –

回答

1

你需要使用一個變量傳遞到lerp()功能amount值。然後,只需增加該變量隨着時間的推移動畫:

float amount = 0; 
float speed = .001; 

void setup() { 
    size(500, 500); 
} 

void draw() { 

    float startX = 0; 
    float startY = 0; 
    float endX = width; 
    float endY = height; 
    float currentX = lerp(startX, endX, amount); 
    float currentY = lerp(startY, endY, amount); 

    background(0); 
    ellipse(currentX, currentY, 20, 20); 

    amount += speed; 

}