Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In order to achieve the bouncing you need to make two things:</p> <ol> <li>Collision detection</li> <li>update position &amp; velocity of bullet</li> </ol> <p><strong>Step 1:</strong></p> <p>If you want to detect collisions correctly you have to calculate the intersection of the rectangle border with a line that connects the current bullet position with the calculated next bullet position (as if there were no obstacle). If there are multiple intersection points, take the first one that hits the border. In Box2D you would use a <a href="http://www.google.de/search?q=raycast%20box2d" rel="nofollow noreferrer">raycast</a> to get the intersection.</p> <p>That answer here will help you to determine if there are intersections: <a href="https://stackoverflow.com/questions/1354472/detect-if-line-segment-intersects-square/1354575#1354575">Detect if line segment intersects square</a></p> <p>Now you need to know at which side of the rectangle the collision occured. The following approach assumes you already know there is a collision, but don't know where:</p> <p><img src="https://i.stack.imgur.com/E98Hb.png" alt="Collision detection"></p> <p>When the bullet's position is in region B, D, E or G its obvious which border will be hit.</p> <p>In any other case it's not so clear. Let me explain it for region H:</p> <ol> <li>compute the vector h (from bullet's position to lower right corner of rectangle)</li> <li>compute the angle a out of the velocity vector v (using tangens)</li> <li>compute the angle b out of h (using tangens)</li> <li>compare a and b: if a &lt; b collision is at the right side, if a &gt; b collision is at the bottom side</li> </ol> <p>Proceed similarly in case of region A, C or F.</p> <p><strong>Step 2:</strong></p> <p>If you have the intersection point, set the new position of the bullet to the intersection point (if you were not able to get the intersection point, update the position as if there were no obstacle, but that would be a bit inaccurate). If the collision occured on the bottom or top, invert the y-component of the velocity. If the collision occured on the right or left, invert the x-component of the velocity.</p> <p>Good luck!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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