2010-11-28 36 views
0

我有一個關於反向圓周運動的查詢。我有下面的代碼。情況1是順時針圓周運動,情況2是逆時針。情況1和2之間的切換是通過用戶觸摸完成的。當它從順時針切換到反時針時,它在定時器/ 1.5的sin()和cos()中使用相同的值。這導致反時針運動從我們星球上的不同位置開始。我想計算我的計時器所需的值,以使逆時針運動從順時針運動過程中離開的位置開始,從而實現平穩過渡。有沒有人知道正確的數學,因爲我的思想卡住了。謝謝反向圓周運動

只是對下面的代碼的一個小的解釋我正在計算我的機器人的位置,通過它所在的行星的x和y座標,並執行半徑爲行星半徑+ 55的圓形運動對我的精靈來說是一個舒適的高度)。

 case 1: 

     positionX = AttachedPlanet.positionX + sin(timer/1.5)*(AttachedPlanet.planetRadius + 55); 
     positionY = AttachedPlanet.positionY + cos(timer/1.5)*(AttachedPlanet.planetRadius + 55); 
     timer = timer + 0.02; 

     break; 
    case 2: 



     positionX = AttachedPlanet.positionX + cos(timer/1.5)*(AttachedPlanet.planetRadius + 55); 
     positionY = AttachedPlanet.positionY + sin(timer/1.5)*(AttachedPlanet.planetRadius + 55); 
     timer = timer + 0.02; 

回答

5

爲什麼不只是保持相同的功能,並切換到遞減計時器?如果您需要一些總時間,請保持第二個定時器始終上升。

+0

哦,男孩 - 我怎麼會錯過?謝謝回覆!我認爲我的頭被困在一個循環中:D。我決定使用相同的功能並添加一個額外的計時器。乾杯。 – STDE 2010-11-28 14:25:13

2

要切換方向,您需要減去sin/cos結果。所以像

dir = +1; //initialization 

if (switchDirection) //event handling 
    dir *= -1; 

//calculation 
positionX = AttachedPlanet.positionX + dir*cos(timer/1.5)*(AttachedPlanet.planetRadius + 55); 
positionY = AttachedPlanet.positionY + dir*sin(timer/1.5)*(AttachedPlanet.planetRadius + 55);