2013-09-16 67 views
0

我有一個具有一個對象: 開始位置:(X0,Y0) 和速度:(Vx的,VY)計算移動物體內盒

它被捕獲在一個盒子裏:boxWidth,boxHeight

當物件碰到箱子的邊框時,它會翻轉方向。

即: 對象有速度:(1,3) 現在它擊中框的頂部 現在它的速度將是:(1,-3) 現在可以說hitted箱 權它的速度將是:(-1,-3)

我已經爲點類做了一個sekeleton。

我需要一個功能,我就給你一個「n」的時間,並且在時間T將返回我當前的目標定位:

我的課:

class Point { 

protected $x0; 
protected $y0; 

protected $vx; 
protected $vy; 

protected $currentX; 
protected $currentY; 

protected $blockWide; 
protected $blockHeight; 


public function __construct($x0, $y0, $vx, $vy, $blockHeight, $blockWide) { 
    $this->x0 = $x0; 
    $this->y0 = $y0; 
    $this->vx = $vx; 
    $this->vy = $vy; 

    $this->blockHeight = $blockHeight; 
    $this->blockWide = $blockWide; 


    $this->currentX = $x0; 
    $this->currentY = $y0; 
} 

public function getLoc($time) { 
    $this->currentX = $this->getLocX($time); 
    $this->currentY = $this->getLocY($time); 
} 

protected function getLocX($time) { 
    $direction = 1; 
    $flips = 0; 
    $distance = $this->vx * $time; 

    if ($this->blockWide - $this->x0) 


    return 1; 
} 

protected function getLocY($time) { 
    return 0; 
} 

public function printAsPoint() { 
    echo '(',$this->currentX,',',$this->currentY,')'; 
} 

} 

我根本不有關於如何計算每次點到達邊界時將會發生的起始位置,速度和速度翻轉的想法。

一些代碼,你的訊息:

protected function getLocX($time) { 

    $corPos = $this->x0 + $time * $this->vx; 
    $modulo = $corPos%(2*$this->blockWide); 

    if($modulo > $this->blockWide) 
      $corPos = 2*$this->blockWide - $modulo; 
    if($modulo < $this->blockWide) 
      $corPos = $modulo; 
    if($modulo == $this->blockWide) 
      $corPos = $modulo; 
    return $corPos; 
} 
+0

爲什麼問題被標記爲java和javascript? – BackSlash

+0

@BackSlash我自己也想知道同樣的事情。這看起來像PHP。 – Sethen

+0

我想簡單地標記「編程」,但不能,這個問題不是針對特定語言的,而是針對所有開發人員的。 –

回答

0
 
Consider your problem as looking for the X and then for the Y, 
considering X0 the initial position, VX the walking step 
(i assume this doesn't change in absolute) , 
WX the width of your object and boxWidth the width of the box 


simplifications: 
- you can consider your object to be 0 width in a box of (boxWidth-WX) 
- you can consider your object running at the speed of 1 
    in a box of (boxWidth-WX)/VX width 
- Since your object changes direction each time it hits a border, we can consider it 
    runs in same direction into a twice bigger box (boxWidth-WX)*2/VX 
- finally, the new position after n moves shall be calculated on the base of : 
    (X0+n) mod (boxWidth-WX)*2/VX which gives you the position in a twice as big box, 
    and checking if the position is bigger than boxWidth the real position 
    will be boxWidth-foundPosition 

+0

是的,我已經把它分開來計算x&y。但問題是它可能具有負開始速度,因此它可以開始向兩個方向移動。 –

+0

如果VX是負數,你修改你的X0爲boxWidth-X0,你用abs(VX)計算,然後最終位置將是boxWidth-finalPositionFound – Gar

0

如果我們只在x - 方向,那麼對象返回(2*boxWidth/Vx)時間後相同的狀態(相同的位置和相同的速度)。

因此,從時間的值開始減去上述數量,直到時間值大於此數量。如果你正在使用整數,那麼你也可以應用餘數運算符。

一旦你得到最終的時間數字,它將很容易處理它。你只需要檢查最多一次反彈。

xFinal = xInitial + Vx * t. 

如果xFinal > boxWidth || xFinal < 0這意味着有反彈並相應地進行。

類似於y方向。

0

假裝沒有國界,那麼想象一下吧!

d = s * t

無國界:actual d = d

有邊界:actual d =

If moving to the right: 
    1. Subtract the distance to the right border from d. 
    2. Divide result by box-width. If the quotient is odd, the remainder 
     indicates how far you bounced back from the left border; if the quotient 
     is even, the remainder shows how far you bounced back from the right 
     border. (or something like that...perhaps you or others can correct me.) 

If moving to the left: 
    Reverse the ideas from 1. 

因爲從您的意見似乎正運動的想法可能會爲你工作,這裏有一個例子向相反方向運動:

Let us say s is -1 unit/sec (that is, moving to the left), 
      t is 6 seconds, 
      the object is 1 unit from the left border, 
      and the box width is 2 units. 

Total distance then is 1 * 6 = 6. We subtract 1 unit to get to the left border 
and have 5 units left. 

How many times do we bounce back and forth? We divide 5 by the box width: 
the quotient is 2, which means the ball went once all the way to the right, then 
back again to the left border. Since the quotient is even, we know we are back 
to the left border (if the quotient was odd we would be at the right border). 

Any remainder indicates the last distance bouncing back, in this case from the 
left border, but not bouncing as far as the right border. Our remainder in this 
case is 1, meaning we bounced back from the left border 1 unit, and that's our 
current location! 
+0

我添加了一些代碼的帖子,它似乎工作時,速度是積極的,但我如何將其逆轉到左邊? –

+0

@TzookBarNoy會不會是相反的?有點像......'1。從d.','2減去距離左邊界的距離。按框寬分隔結果。如果商數是奇數,則餘數表示您從右邊界彈回多遠;如果商數爲偶數,則剩餘部分顯示您距離左邊界反彈的距離。「 –

+0

您是否可以使用示例編輯您的文章...我使用文本dislektik。 –