none
PMT149 Bouncing Balls with Realistic Collision Physics LitDev RRS feed

  • 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 = (xm-x)*(xm-x)+(ym-y)*(ym-y)
          If (dist < radius*radius) Then
            u = 0
            v = 0
            Xvel[i] = u
            Yvel[i] = v
          EndIf

    I 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

    Tuesday, May 16, 2017 2:49 PM
    Answerer

Answers

  • Because dist = (xm-x)*(xm-x)+(ym-y)*(ym-y) 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).

    Tuesday, May 16, 2017 5:00 PM
    Moderator

All replies

  • Because dist = (xm-x)*(xm-x)+(ym-y)*(ym-y) 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).

    Tuesday, May 16, 2017 5:00 PM
    Moderator
  • Wasn't even thinking of it that way. Forgot about distance being squared. So, then I was thinking why isn't this just using the radius? Thanks again for your help.

    JR

    Tuesday, May 16, 2017 5:39 PM
    Answerer