CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2005
    Posts
    19

    Question Timer isn't working right

    .NET 2.0

    I am making a simple program that deals with the physics of a ball that drops and bounces. Right now, the conditions are ideal, no energy is lost.

    The problem Im having is with the timer. The timer is set to 50 milliseconds. The value of the variable vT1 goes up 0.05 every time the a tick even is handled. But the value of vT1 isn't going up fast enough to keep up with a real clock.

    Code:
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles Timer1.Tick
            'THE CEILING is 32 and the GROUND is 532
            'Gravitational Acceleration is 32.1741 feet per second
            ball1.vD += ball1.vV * 0.05  '(Distance = Velocity * Time)
            ball1.vH = 500 - (ball1.Pos.Y - 32)
            ball1.vGPE = ball1.vM * vG * ball1.vH '(GPE = mgh)
            ball1.vKE = (ball1.vM * ball1.vV ^ 2) / 2 '(KE = mV^2 / 2)
            ball1.vEnergy = ball1.vGPE + ball1.vKE
            ball1.vT1 += 0.05
            ball1.vT2 += 0.05
    
            ShowINFO()
    
            'COLLISION DETECTION (THE GROUND)
            If ball1.vPoint + ball1.vD > 531 Then
                ball1.Pos.Y = 532
                ball1.vDirectionY = "up"
                ball1.vD = 0
                ball1.vT2 = 0
                'ball1.vV += ball1.vG * 0.01
            End If
    
            'MAXIMUM GPE, TIME TO COME BACK DOWN
            If ball1.vV <= 0 And ball1.vH >= 0 Then
                ball1.vDirectionY = "down"
                ball1.vPoint = ball1.Pos.Y
                ball1.vD = 0
                ball1.vT2 = 0
            End If
    
            'MOVE DOWN
            If ball1.vDirectionY = "down" Then
                ball1.Pos.Y = ball1.vPoint + ball1.vD
                ball1.vV += vG * 0.05
            End If
    
            'MOVE UP
            If ball1.vDirectionY = "up" Then
                ball1.Pos.Y = 532 - ball1.vD
                ball1.vV -= vG * 0.05
            End If
    
            'MAKE SURE VELOCITY IS NOT NEGATIVE
            If ball1.vV < 0 Then
                ball1.vV = 0
            End If
            'IF ENERGY IS 0 THEN STOP LOOP/TIMER
            If ball1.vEnergy = 0 Then
                Timer1.Enabled = False
            End If
    
            Me.Invalidate()
    
        End Sub

  2. #2
    Join Date
    Dec 2002
    Posts
    305

    Re: Timer isn't working right

    Use larger values for the increment of vT1 (try 0.5, 5 ms, ...) and if necessary increase timer interval also from 50 ms. Good luck.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured