我有一个对象:起始位置:(X0,Y0)和速度:(Vx,Vy)
它被困在一个盒子里:boxWidth, boxHeight
当物体碰到盒子的边界时,它会翻转方向。
ie: object has speed: (1,3) now it hit the top of box now it's speed will be: (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;
}