计算框内的移动物体

时间:2013-09-16 16:18:11

标签: php algorithm language-agnostic

我有一个对象: 起始位置:(X0,Y0) 和速度:(Vx,Vy)

它被困在一个盒子里:boxWidth,boxHeight

当物体撞到盒子的​​边框时,它会翻转方向。

即: 物体有速度:(1,3) 现在它击中了盒子的顶部 现在它的速度将是:(1,-3) 现在让我们说它击中了盒子的右侧 它的速度将是:(-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;
}

3 个答案:

答案 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

答案 1 :(得分:0)

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

因此,从时间值大于此数量的时间开始减去上述数量。如果使用整数,那么也可以应用余数运算符。

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

xFinal = xInitial + Vx * t.

如果xFinal > boxWidth || xFinal < 0,则表示有反弹并相应地继续。

同样适用于y方向。

答案 2 :(得分:0)

假装没有边界,那么想象有!!/ / p>

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!