Note that there are some explanatory texts on larger screens.

plurals
  1. POcalculate moving object inside box
    text
    copied!<p>I have an object that has: start location: (X0,Y0) and speed: (Vx, Vy)</p> <p>it is trapped inside a box: boxWidth, boxHeight</p> <p>when the object hits the border of the box it will flip direction.</p> <p>ie: object has speed: (1,3) now it hits the top of the box now it's speed will be: (1,-3) now lets say it hitted the right of the box and it's speed will be: (-1, -3)</p> <p>I already made a sekeleton for the point class.</p> <p>I would need a function that I will give it an "n" time and t will return me the current object location after that time:</p> <p>my class:</p> <pre><code>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-&gt;x0 = $x0; $this-&gt;y0 = $y0; $this-&gt;vx = $vx; $this-&gt;vy = $vy; $this-&gt;blockHeight = $blockHeight; $this-&gt;blockWide = $blockWide; $this-&gt;currentX = $x0; $this-&gt;currentY = $y0; } public function getLoc($time) { $this-&gt;currentX = $this-&gt;getLocX($time); $this-&gt;currentY = $this-&gt;getLocY($time); } protected function getLocX($time) { $direction = 1; $flips = 0; $distance = $this-&gt;vx * $time; if ( $this-&gt;blockWide - $this-&gt;x0) return 1; } protected function getLocY($time) { return 0; } public function printAsPoint() { echo '(',$this-&gt;currentX,',',$this-&gt;currentY,')'; } } </code></pre> <p>I simply dont have an idea on how to calculate it with the starting location, speed, and speed flips that will happen each time the point will reach the borders.</p> <p>some code from your posts:</p> <pre><code>protected function getLocX($time) { $corPos = $this-&gt;x0 + $time * $this-&gt;vx; $modulo = $corPos%(2*$this-&gt;blockWide); if($modulo &gt; $this-&gt;blockWide) $corPos = 2*$this-&gt;blockWide - $modulo; if($modulo &lt; $this-&gt;blockWide) $corPos = $modulo; if($modulo == $this-&gt;blockWide) $corPos = $modulo; return $corPos; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload