Results 1 to 5 of 5

Thread: How do I interpolate 2 animations

Threaded View

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2017
    Posts
    761

    Question How do I interpolate 2 animations

    Hello!

    I am dealing with the transitions between 2 states.

    I am trying to mimick a Unity transition.

    I am experiencing weird color flashes while I expect gradual transitions.
    This makes me wonder if I am not aware of a problem.

    Thank you for having a look at my code.

    This is the code of a cTransition class:

    Code:
    Public Sub UpdateTransition()
    
        If Not Me.IsActive Then
            Exit Sub
        End If
        
        Dim lNow&
        lNow = GetTickCount
    
        Dim lTaken&
        lTaken = lNow - m_lStartTime
    
        Me.Progress = lTaken / GetEffectiveDuration(Me.Duration)
        
        Debug.Print "----------"
        m_s = "Progress: " & Me.Progress & vbNewLine
        
        Clamp Me.Progress, 0, 1
    
        ' Update animations
        modAnim.UpdateAnimation m_StartAnimationConfig, lNow
        modAnim.UpdateAnimation m_TargetAnimationConfig, lNow
        
        If lTaken >= GetEffectiveDuration(Me.Duration) Then
            ' Transition is complete
            m_bIsTransitionComplete = True
            Me.IsActive = False
            Exit Sub
        End If
    
        ' Calculate current size using weighted interpolation
        Dim currentSizeStart As Double
        Dim currentSizeTarget As Double
        currentSizeStart = m_StartAnimationConfig.StartSizeFactor + _
                           (m_StartAnimationConfig.TargetSizeFactor - m_StartAnimationConfig.StartSizeFactor) * _
                           Ease(Me.Progress, m_StartAnimationConfig.FunctionType, m_StartAnimationConfig.ModeType)
    
        currentSizeTarget = m_TargetAnimationConfig.StartSizeFactor + _
                            (m_TargetAnimationConfig.TargetSizeFactor - m_TargetAnimationConfig.StartSizeFactor) * _
                            Ease(Me.Progress, m_TargetAnimationConfig.FunctionType, m_TargetAnimationConfig.ModeType)
    
        Me.CurrentSize = (currentSizeTarget * Me.Progress) + (currentSizeStart * (1 - Me.Progress))
    
        ' Calculate current alpha using weighted interpolation
        Dim currentAlphaStart As Double
        Dim currentAlphaTarget As Double
        currentAlphaStart = m_StartAnimationConfig.StartAlpha + _
                            (m_StartAnimationConfig.TargetAlpha - m_StartAnimationConfig.StartAlpha) * _
                            Ease(Me.Progress, m_StartAnimationConfig.FunctionType, m_StartAnimationConfig.ModeType)
    
        currentAlphaTarget = m_TargetAnimationConfig.StartAlpha + _
                             (m_TargetAnimationConfig.TargetAlpha - m_TargetAnimationConfig.StartAlpha) * _
                             Ease(Me.Progress, m_TargetAnimationConfig.FunctionType, m_TargetAnimationConfig.ModeType)
    
        Me.CurrentAlpha = (currentAlphaTarget * Me.Progress) + (currentAlphaStart * (1 - Me.Progress))
    
        ' Calculate current color using BlendColors
        Me.CurrentColor = BlendColors(m_TargetAnimationConfig.TargetColor, m_StartAnimationConfig.StartColor, Me.Progress)
    And this is sub to apply the current progress to the size and color of the objects that I want to animate:

    Code:
    Public Function GetEffectiveDuration(ByVal u As Double) As Double
    
        If g_Slowmo = 0 Then
            g_Slowmo = 1
        End If
        
        GetEffectiveDuration = u * g_Slowmo
    
    End Function
    
    Public Sub UpdateAnimation(ByRef u As cAnimationConfig, ByVal uCurrentTime As Long)
    
        Dim lDur&
        lDur = uCurrentTime - u.StartTime
    
        u.DontUseThisForEndResult_Progress = lDur / GetEffectiveDuration(u.Duration) 'GetEffectiveDuration is a helper function to make Duration bigger to slow down the animation for inspecting
        
        'clamp
        Clamp u.DontUseThisForEndResult_Progress, 0, 1
    
        If u.DontUseThisForEndResult_Progress > 1 Then
            u.DontUseThisForEndResult_Progress = 1
        ElseIf u.DontUseThisForEndResult_Progress < 0 Then
            u.DontUseThisForEndResult_Progress = 0
        End If
    
        If lDur >= GetEffectiveDuration(u.Duration) Then
        
            u.IsActive = False
    
            If u.IsOneShot Then
                'can not repeat
                Exit Sub
            Else
                u.IsActive = True 'start again
                u.StartTime = GetTickCount
                'reset and make it play again
                If u.PlayMirroredIfLooped Then
                    u.IsPlayingMirrored = Not u.IsPlayingMirrored
                End If
                u.DontUseThisForEndResult_Progress = 0
            End If
        End If
        
        u.EffectiveProgress = modAnim.GetAnimationProgress(u.DontUseThisForEndResult_Progress, u.IsPlayingMirrored)
        u.EasedEffectiveProgress = Ease(u.EffectiveProgress, u.FunctionType, u.ModeType)
        u.CurrentSize = u.SizeFactorAtStart + ((u.SizeFactorAtEnd - u.SizeFactorAtStart) * u.EasedEffectiveProgress)
        u.CurrentColor = BlendColors(u.StartColor, u.TargetColor, 1 - u.EffectiveProgress) 'dont use easing for color, instead use linear
        
    End Sub
    Last edited by tmighty2; Sep 30th, 2024 at 03:40 AM.

Tags for this Thread

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