Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Without answering your question, I'd like to comment on your implementation strategy and recommend a new approach. You represent the velocity of the ball in polar coordinate form, as <code>ball.angle</code> and <code>ball.speed</code>.</p> <p>I think that this is going to be generally inconvenient for you. For example, in your collision code you end up calling <code>atan2</code> to turn the vector (<code>dx</code>, <code>dy</code>) into an angle, and then you call <code>sin</code> and <code>cos</code> to turn the angle back into a vector again. (Also, should you ever try to generalize your code to three dimensions, you will find yourself in a world of pain.) So, unless you have particular requirements that necessitate polar coordinates, I recommend that you do what everyone else does, namely represent the velocity of the ball in Cartesian coordinates as the vector (<code>vx</code>, <code>vy</code>).</p> <p>I also recommend changing your physics approach from a <em>static</em> one ("is object A currently colliding with object B?") to a <em>dynamic</em> one ("will object A collide with object B during its next movement step?"). In a static physics system you often end up with objects intersecting each other at the end of a movement step, and then you have to figure out the best way to get them to separate again, which is hard to get right.</p> <p>If you do both of these, it is straightforward to bounce the ball without any trigonometry.</p> <p>Step 1. Transform circle/circle collision into point/circle collision using <a href="http://en.wikipedia.org/wiki/Minkowski_addition" rel="nofollow noreferrer">Minkowski addition</a>:</p> <p><img src="https://i.stack.imgur.com/tNbA0.png" alt="Original problem at left shows small circle moving inside a large circle. Transformed problem at right shows point moving in circle whose radius is the difference between the radii of the circles in the original problem."></p> <p>Step 2. Consider a time segment in which the ball starts at <strong>p</strong> = (px,py) and moves by <strong>v</strong> = (vx,vy). Does it intersect with the circle? You can use a <a href="http://garethrees.org/2009/02/17/physics/#section-4" rel="nofollow noreferrer">standard line segment/circle test</a> for this except that the sense of the test is reversed.</p> <p>Step 3. Find the point of collision <strong>c</strong> = (cx,cy). The ball bounces off the circle in the same way as it would bounce off the line <strong>t</strong> tangent to the circle at this point. For a circle centred at the origin, the tangent vector is just (−cy,cx) and I'm sure you can work out how to compute it for other circles.</p> <p><img src="https://i.stack.imgur.com/YrjYW.png" alt="Figure described above"></p> <p><a href="https://stackoverflow.com/questions/573084/how-to-calculate-bounce-angle/573206#573206">See this answer</a> for how to calculate the new path of the ball based on coefficients of friction and restitution.</p> <p>Step 4. Don't forget that the ball may still have some distance to move along the new vector <strong>w</strong>. If the time step is large enough or the velocity high enough it may collide again during the same time segment.</p>
 

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