none
Gravity Physics RRS feed

  • Question

  • I'm trying to create a physics simulation. However I can't seem to get my gravity code to work. Any suggestions on what I'm doing wrong?

    PUBLISH CODE : LVX011

    Saturday, November 9, 2013 12:02 PM

Answers

  • Nice program.

    Gravity is just an acceleration down, which is an increase in Y velocity.  We can also add friction to slow the balls a bit.

    Sub MoveBalls
      For i = 1 To Ball_Number
        Ball_vY[i] = Ball_vY[i]+0.02
        Ball_vX[i] = 0.999*Ball_vX[i]
        Ball_vY[i] = 0.999*Ball_vY[i]
        Ball_X[i] = Ball_X[i] + Ball_vX[i]
        Ball_Y[i] = Ball_Y[i] + Ball_vY[i]
        Shapes.Move(Ball[i], Ball_X[i], Ball_Y[i])
      EndFor
      energyInc = 0
    EndSub
    

    Also I would reset the ball positions on the walls as they hit.  This prevents them slowing falling through the floor for example.

        If (Ball_X[i] + Ball_Diameter) > GraphicsWindow.Width Then
          Ball_vX[i] = -Ball_vX[i]
          Ball_X[i] = GraphicsWindow.Width-Ball_Diameter
        EndIf
    

    etc.

    This effect of the collision calculation occurring as the shapes have already penetrated some distance also causes the balls to strangely rotate about each other or get stuck.

    We could use smaller velocities to reduce this, but the fix below moves the balls after a collision so the don't get trapped.

            Ball_vX[i] = (Relative_vX[i] + Cx) 
            Ball_vY[i] = (Relative_vY[i] + Cy)
            Ball_vX[j] = (Relative_vX[j] + Cx)
            Ball_vY[j] = (Relative_vY[j] + Cy)  
            
            Ball_X[i] = Ball_X[i] + Nx * (Ball_Diameter-Distance)
            Ball_Y[i] = Ball_Y[i] + Ny * (Ball_Diameter-Distance)
            Ball_X[j] = Ball_X[j] - Nx * (Ball_Diameter-Distance)
            Ball_Y[j] = Ball_Y[j] - Ny * (Ball_Diameter-Distance)
          EndIf
    

    With these mods 30 balls, gradually settle quite well at the bottom after a while.

    • Marked as answer by SquarePie Sunday, November 10, 2013 11:20 AM
    Saturday, November 9, 2013 5:11 PM
    Moderator