2012-07-15 84 views
2

我的問題是讓物品以相同的速度和時間在不同的屏幕上移動。此時該物品不移動或快速移動。我爲該物品指定了兩個點並指定了最高速度。在不同分辨率下移動時保持同步?

Var _oneStep是我嘗試同步分辨率。

//Get a fraction of screen size? 
_oneStep = ScreenWidth/100; 

//Delta being last update time. 
public void MoveItem(double Delta) 
{ 
    //Keep at Max speed. 
    if(Math.abs(_xVelocity) > _maxSpeed || Math.abs(_yVelocity) > _maxSpeed) 
    { 
     _accelerationSpeed = 0.0001f * (_maxSpeed/_oneStep); 
    } 
    //Or Speed up. 
    else 
    { 
     _accelerationSpeed = 0.00030f * (_maxSpeed/_oneStep); 
    } 
    //Destination is above 10% distance or below speed up or down 

    _xVelocity += _accelerationSpeed * Math.cos(Math.toRadians(_degrees)); 
    _yVelocity += _accelerationSpeed * Math.sin(Math.toRadians(_degrees)); 

    _currentPosition.x += (((_xVelocity * _accelerationSpeed) + _oneStep) * Delta); 
    _currentPosition.y += (((_yVelocity * _accelerationSpeed) + _oneStep) * Delta); 

    CheckAtDestination(); 
} 

一些數字:

System.out.println("X: "+ _xVelocity + " Y: " + _yVelocity); = X: 1.0018943990787657 Y: 1.0109980390961948 
System.out.println("XPos: "+ _currentPosition.x + " YPos: " + _currentPosition.x); = XPos: 36 YPos: 36 
System.out.println("Degrees: " + _degrees); = Degrees: 80.22677351282063 
System.out.println(Delta); = 0.3309303333093033 

如果增量變爲1以上同樣會發生什麼?奇怪的結果產生了嗎?

回答

0

上面的代碼工作,只要確保_oneStep var是screenWidth或Height(取決於您的對象大小,可能取決於屏幕)。

So _oneStep = ScreenWidth/100;並且Delta需要被添加到任何移動中。