2012-10-18 46 views
0

我想實現兩點彼此旋轉。因此我使用旋轉矩陣。不過,現在我發現點之間的距離越來越大(請參閱緩存的視頻1)。但是,我的整個模擬過程中距離應該保持不變。兩個點圍繞同一中心旋轉但距離增長

這裏是我的代碼,我使用的計算速度:

其中P1和P2是兩個點。

double xPos = p0.x+p1.x; 
double yPos = p0.y+p1.y; 
//The center between p1 and p2 
xPos /=2; 
yPos /=2; 
//the rotating angle 
double omega = 0.1; 
//calculate the new positions 
double x0new = xPos + (p0.x-xPos)*std::cos(omega) - (p0.y-yPos)*std::sin(omega); 
double y0new = yPos + (p0.x-xPos)*std::sin(omega) + (p0.y-yPos)*std::cos(omega); 
double x1new = xPos + (p1.x-xPos)*std::cos(omega) - (p1.y-yPos)*std::sin(omega); 
double y1new = yPos + (p1.x-xPos)*std::sin(omega) + (p1.y-yPos)*std::cos(omega); 
//the speed is exatly the difference as I integrate one timestep 
p0.setSpeed(p0.x-x0new, p0.y-y0new); 
p1.setSpeed(p1.x-x1new, p1.y-y1new); 

然後,我將速度整合一次。我的計算有什麼問題?

更新 看來,我的積分是錯誤的。如果我直接設置職位,那麼他的工作很完美。但是我現在不什麼是錯的這個集成:

setSpeed(ux,uy){ 
    ux_=ux; 
    uy_=uy; 
} 
// integrate one timestep t = 1 
move(){ 
    x = x + ux_; 
    y = y + uy_; 
} 

Video of my behaviour

+0

'setSpeed'做什麼? – Andrey

+0

設置點的速度,然後集成一個步驟。 – tune2fs

+0

你能修改整合期來驗證我的理論嗎? (擴大它將增加擴展) –

回答

1

這段代碼沒有什麼明顯的錯誤,但未顯示的「速度」積分表明您可能在舊位置和新位置之間線性積分,這會使速度>標稱速度時軌道擴大並且當速度爲< nominal_speed時合同。

正如我懷疑。積分實際上是在點p0和p1之間的線段上的外推,這些線段應該距離原點固定的距離(物理模擬可能使得軌跡橢圓...)

因此,如果外推因子是0,新的位置將在計算的周界上。如果它是< 0(並且> -1),則會在預期的軌跡內插值。

  O  This beautiful ascii art is trying to illustrate the integration 
     / x is the original position, o is the new one and O is the 
    /___----- "integrated" value and the arc is a perfect circle :) 
     o--  Only at the calculated position o, there is no expansion. 
    --/ 
// 
// 
|/
x 
1

乍看之下,主要的原因是你更新p0p1在每次迭代座標。這會累積不準確的結果,這可能來自setSpeed

取而代之,您應該使用恆定的初始座標p0p1,但增加omega角度。

+0

我懷疑在雙倍的最後數字不準確會在一生中積累。 –

+0

他們可能來自'SetSpeed',該實現未提供 – Andrey