開始位置: (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;
}