0

我有一个对象:起始位置:(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;
}
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 宽度的盒子里
- 由于您的对象每次碰到边界时都会改变方向,我们可以考虑一下
  同方向跑进两倍大的盒子(boxWidth-WX)*2/VX
- 最后,n 次移动后的新位置应根据以下公式计算:
   (X0+n) mod (boxWidth-WX)*2/VX 为您提供两倍大的盒子中的位置,
   并检查位置是否大于 boxWidth 实际位置
   将是 boxWidth-foundPosition

于 2013-09-16T16:28:53.380 回答
0

如果我们只取 x 方向,那么对象会在一段时间后返回相同的状态(相同的位置和相同的速度)(2*boxWidth/Vx)

所以不断地从时间中减去上面的量,直到时间的值大于这个量。如果您正在使用整数,那么您也可以应用余数运算符。

一旦你得到最后的时间数字,处理它就很简单了。您最多只需要检查一次反弹。

xFinal = xInitial + Vx * t.

如果xFinal > boxWidth || xFinal < 0这意味着有反弹并相应地进行。

对于 y 方向也是如此。

于 2013-09-16T16:29:08.997 回答