0

開始位置: (X0,Y0) および速度: (Vx, Vy) を持つオブジェクトがあります。

ボックス内に閉じ込められています: boxWidth、boxHeight

オブジェクトがボックスの境界に当たると、方向が反転します。

例: オブジェクトの速度: (1,3) ボックスの上部にヒットすると、速度は (1,-3) になります。ボックスの右側にヒットすると、速度は (-1) になります。 、-3)

ポイントクラスのスケルトンはすでに作成済みです。

「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;
}
4

3 に答える 3

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!
于 2013-09-16T17:03:57.323 に答える
0
問題を X を探し、次に Y を探すと考えてください。
X0 を初期位置、VX を歩行ステップとする
(これは絶対に変わらないと思います)、
WX はオブジェクトの幅、boxWidth はボックスの幅


単純化:
- (boxWidth-WX) のボックスでオブジェクトの幅が 0 であると見なすことができます
- オブジェクトが 1 の速度で実行されていると見なすことができます
  (boxWidth-WX)/VX 幅のボックス内
- あなたのオブジェクトは境界にぶつかるたびに方向を変えるので、それを考慮することができます
  同じ方向に 2 倍の大きさのボックス (boxWidth-WX)*2/VX に実行されます。
- 最後に、n 移動後の新しい位置は、以下に基づいて計算されます。
   (X0+n) mod (boxWidth-WX)*2/VX は、2 倍の大きなボックスでの位置を示します。
   位置が実際の位置の boxWidth よりも大きいかどうかを確認します
   boxWidth-foundPosition になります

于 2013-09-16T16:28:53.380 に答える
0

x - 方向のみを取ると、オブジェクトは時間の経過後に同じ状態 (同じ位置と同じ速度) に戻ります(2*boxWidth/Vx)

したがって、時間の値がこの量よりも大きくなるまで、時間から上記の量を引き続けます。整数を扱っている場合は、剰余演算子も適用できます。

最終的な時間の数字を取得すると、それを処理するのは簡単になります。最大で 1 回のバウンスを確認するだけで済みます。

xFinal = xInitial + Vx * t.

xFinal > boxWidth || xFinal < 0バウンスがあることを意味する場合は、それに応じて続行します。

y 方向についても同様です。

于 2013-09-16T16:29:08.997 に答える