Mouse Tail...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ali Rizwan
    Banned
    Contributor
    • Aug 2007
    • 931

    Mouse Tail...

    Hi all,
    Use this code to make mouse tail using some image.

    You need to add an image control and a timer control to form.
    Load some image to image control.

    Image name : ImgBall
    Index : 0 (important)

    Under timercontrol use this code
    [CODE=vb]Private Sub Timer1_Timer()
    Animate
    End Sub[/CODE]

    Add this code under form load event::::
    [CODE=vb] Private Sub Form_Load()

    Call MUSICSND

    Dim I As Integer
    For I = ImgBall.UBound + 1 To 7
    Load ImgBall(I)
    ImgBall(I).Visi ble = True
    ImgBall(I).Top = ImgBall(I - 1).Top + 11
    Next I
    ImgBall(0).Visi ble = False
    Call InitVal
    Call InitBall
    Timer1.Interval = 20
    Timer1.Enabled = True

    End Sub[/CODE]

    Add this code under form mousemove event
    [CODE=vb]Private Sub Form_MouseMove( Button As Integer, Shift As Integer, X As Single, Y As Single)
    MoveHandler CLng(X), CLng(Y)
    Animate
    End Sub[/CODE]

    Now add a module and write this code in module :::::

    [CODE=vb]Option Explicit

    Public Type Vec2D
    X As Long
    Y As Long
    End Type

    Public Type AnimBall
    Vec As Vec2D
    dx As Double
    dy As Double
    Img As Image
    End Type

    Dim nBalls As Integer
    Dim Xpos, Ypos
    Dim DeltaT As Double
    Dim SegLen
    Dim SpringK
    Dim Mass
    Dim Gravity
    Dim Resistance
    Dim StopVel As Double
    Dim StopAcc As Double
    Dim DotSize As Long
    Dim Bounce As Double
    Dim bFollowM As Boolean
    Dim balls() As AnimBall

    Function InitVal()
    ' Some of the variables are still unknown to me
    nBalls = 7 ' numbers of ball
    Xpos = Ypos = 0 ' evaluate position
    DeltaT = 0.01 '
    SegLen = 10# ' it seem like the distance between the
    ' mouse pointer and the ball
    ' it's quite intersting to change the value
    ' and see the effect
    SpringK = 11 ' spring constant,
    ' if large, the longer and higher the tail
    ' will swing
    Mass = 1 'mass of the ball
    Gravity = 40 ' gravity coeff,
    ' if large, the balls are more difficult
    ' to move upward
    Resistance = 9 ' resistivity of the ball to move itself
    ' from a location, the larger the more difficult to
    ' move
    StopVel = 0.1
    StopAcc = 0.1
    DotSize = 11 ' the size of the ball in pixel
    Bounce = 0.95 ' bouncing coeff,
    bFollowM = True ' animation flag
    End Function


    ' must only be called after load all imgBall
    Function InitBall()
    Dim I As Integer
    ReDim balls(nBalls)

    For I = 0 To nBalls
    balls(I) = BallSet(MAINPAG E.ImgBall(I))
    Next I

    For I = 0 To nBalls
    balls(I).Img.Le ft = balls(I).Vec.X
    balls(I).Img.To p = balls(1).Vec.Y
    Next I
    End Function

    ' initialize a ball
    Function BallSet(Img As Image) As AnimBall
    BallSet.Vec.X = Xpos
    BallSet.Vec.Y = Ypos
    BallSet.dx = BallSet.dy = 0
    Set BallSet.Img = Img
    End Function

    ' initialize a vector variable
    Function VecSet(X As Long, Y As Long) As Vec2D
    VecSet.X = X
    VecSet.Y = Y
    End Function

    ' update position when mouse move
    Function MoveHandler(X As Long, Y As Long)
    Xpos = X
    Ypos = Y
    End Function

    ' calculate the spring force of the balls chain
    Function SpringForce(I As Integer, J As Integer, ByRef spring As Vec2D)
    Dim tempdx, tempdy, tempLen, springF
    tempdx = balls(I).Vec.X - balls(J).Vec.X
    tempdy = balls(I).Vec.Y - balls(J).Vec.Y
    tempLen = Sqr(tempdx * tempdx + tempdy * tempdy)
    If (tempLen > SegLen) Then
    springF = SpringK * (tempLen - SegLen)
    spring.X = spring.X + (tempdx / tempLen) * springF
    spring.Y = spring.Y + (tempdy / tempLen) * springF
    End If
    End Function

    ' main routine of this animated balls
    ' call on mouse move or every 20ms
    Function Animate()
    Dim iH, iW
    Dim start As Integer
    Dim I As Integer
    Dim spring As Vec2D
    Dim resist As Vec2D
    Dim accel As Vec2D
    ' enable the animation
    If (bFollowM) Then
    balls(0).Vec.X = Xpos
    balls(0).Vec.Y = Ypos
    start = 1
    End If

    For I = start To nBalls
    spring = VecSet(0, 0)

    If (I > 0) Then
    Call SpringForce(I - 1, I, spring)
    End If

    If (I < (nBalls - 1)) Then
    Call SpringForce(I + 1, I, spring)
    End If
    resist = VecSet(-balls(I).dx * Resistance, -balls(I).dy * Resistance)
    accel = VecSet((spring. X + resist.X) / Mass, _
    (spring.Y + resist.Y) / Mass + Gravity)

    balls(I).dx = balls(I).dx + DeltaT * accel.X
    balls(I).dy = balls(I).dy + DeltaT * accel.Y

    If (Abs(balls(I).d x) < StopVel And _
    Abs(balls(I).dy ) < StopVel And _
    Abs(accel.X) < StopAcc And _
    Abs(accel.Y) < StopAcc) Then
    balls(I).dx = 0
    balls(I).dy = 0
    End If

    balls(I).Vec.X = balls(I).Vec.X + balls(I).dx
    balls(I).Vec.Y = balls(I).Vec.Y + balls(I).dy

    ' checking for boundary conditions
    iW = MAINPAGE.ScaleW idth
    iH = MAINPAGE.ScaleH eight

    ' check bottom
    If (balls(I).Vec.Y >= iH - DotSize - 1) Then
    If (balls(I).dy > 0) Then
    balls(I).dy = Bounce * (-balls(I).dy)
    End If
    balls(I).Vec.Y = iH - DotSize - 1
    End If

    ' check right
    If (balls(I).Vec.X >= iW - DotSize) Then
    If (balls(I).dx > 0) Then
    balls(I).dx = Bounce * (-balls(I).dx)
    End If
    balls(I).Vec.X = iW - DotSize - 1
    End If

    ' check left
    If (balls(I).Vec.X < 0) Then
    If (balls(I).dx < 0) Then
    balls(I).dx = Bounce * (-balls(I).dx)
    End If
    balls(I).Vec.X = 0
    End If
    ' check top
    If (balls(I).Vec.Y < 0) Then
    If (balls(I).dy < 0) Then
    balls(I).dy = Bounce * (-balls(I).dy)
    End If
    balls(I).Vec.Y = 0
    End If

    balls(I).Img.Le ft = balls(I).Vec.X
    balls(I).Img.To p = balls(I).Vec.Y
    Next I
    End Function[/CODE]

    Regards
    >> ALI <<
    Last edited by debasisdas; Mar 7 '08, 06:58 AM. Reason: added code=vb tags
Working...