Answered by:
PMT149 Bouncing Balls with Realistic Collision Physics LitDev
Question

In the above program "PMT149" Litdev has the following section that stops a ball from moving if the mouse is clicked when over top the ball.
If (iselect) Then
For i = 1 To nball
x = Xpos[i]
y = Ypos[i]
dist = (xmx)*(xmx)+(ymy)*(ymy)
If (dist < radius*radius) Then
u = 0
v = 0
Xvel[i] = u
Yvel[i] = v
EndIfI understand the method for calculating "dist", but don't understand why in the if statement below that calculation he is checking to see if "dist" is less than the radius squared of the ball. So, the question is: "Why use the radius squared?" Please explain the logic for doing that.
JR
Answers

Because dist = (xmx)*(xmx)+(ymy)*(ymy) is actually the separation squared, perhaps dist is better named dist2 or distSquared.
This is to save 2 square root calculations (unnecessry since if distSquared is less than radius*radius, then dist < radius  they are equivalent).
 Marked as answer by jricestkEditor Tuesday, May 16, 2017 5:37 PM
All replies

Because dist = (xmx)*(xmx)+(ymy)*(ymy) is actually the separation squared, perhaps dist is better named dist2 or distSquared.
This is to save 2 square root calculations (unnecessry since if distSquared is less than radius*radius, then dist < radius  they are equivalent).
 Marked as answer by jricestkEditor Tuesday, May 16, 2017 5:37 PM
