Controlling more than one animation?

Everything related to 3D programming
Mythros
Enthusiast
Enthusiast
Posts: 306
Joined: Mon Aug 19, 2013 3:28 pm

Controlling more than one animation?

Post by Mythros »

Hi all. I have a slightly animation code, as per the PB manual, and I would like to know what I'm doing wrong here. Why I can't make my player jump while PRESSING the space key, then when he lands, fade slowly back into idle animation.

I'm pretty sure I mimic'd the code's structure pretty well.

Code:

Code: Select all

;
; ------------------------------------------------------------
;
;   PureBasic - Animation
;
;    (c) 2012 - Fantaisie Software
;
; ------------------------------------------------------------
;
;Cursor = Move Robot
;Speed animation = PageUp and PageDown 

IncludeFile "Screen3DRequester.pb"

#ANIM_FADE_SPEED = 7.5  ; animation crossfade speed in % of full weight per second

Enumeration 1
  #Idle
  #Walk
  #Jump
EndEnumeration

#NUM_ANIMS = 3 ; number of animations

Define.f KeyX, KeyY, MouseX, MouseY, Angle, Speed = 1.0, TimeSinceLastFrame 
Global RobotMove, RobotJump, FadeIn, FadeOut, Anim

Global Dim Anim.s(#NUM_ANIMS)

Declare fadeAnimations(deltaTime.f)
Declare.f CurveAngle(Actuelle.f, Target.f, P.f)
Declare.f WrapPi(Angle.f)

Macro Clamp(num, min, max)
  If num<min
    num=min
  ElseIf num>max
    num=max
  EndIf  
EndMacro

If InitEngine3D()
  
  Add3DArchive("Data/Textures"        , #PB_3DArchive_FileSystem)
  Add3DArchive("Data/Models"          , #PB_3DArchive_FileSystem)
  Add3DArchive("Data/Scripts"         , #PB_3DArchive_FileSystem)
  Add3DArchive("Data/Packs/Desert.zip", #PB_3DArchive_Zip)
  Parse3DScripts()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  If Screen3DRequester()
    
    WorldShadows(#PB_Shadow_Modulative, 0, RGB(180, 180, 255))
    
    ;Ground
    ;
    CreateMaterial(0, LoadTexture(0, "Dirt.jpg"))
    CreatePlane(0, 1500, 1500, 40, 40, 15, 15)
    CreateEntity(0,MeshID(0),MaterialID(0))
    EntityRenderMode(0, 0)
    
    ;Mesh
    ;
    LoadMesh(1, "Robot.mesh")
    
    ; Entity
    ;
    CreateEntity(1, MeshID(1), #PB_Material_None, 0, 0, -50)
    EntityAnimationBlendMode(1, #PB_EntityAnimation_Average)
    
    ; Animation
    ;
    animNames$ = "Idle,Walk,Jump"
    
    ; populate our animation List
    For i = 1 To #NUM_ANIMS
      Anim(i) = StringField(animNames$, i, ",")
      Fadein  = #False
      FadeOut = #False
    Next
    
    ; SkyBox
    ;
    SkyBox("Desert07.jpg")
    
    ; Camera
    ;
    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0, 50, 100, 180, #PB_Absolute)
    CameraLookAt(0, EntityX(1), EntityY(1) + 40, EntityZ(1))
    
    
    CreateLight(0, RGB(255, 255, 255), -40, 100, 80)
    AmbientColor(RGB(80, 80, 80))
    
    Repeat
      Screen3DEvents()
      
      If ExamineMouse()
        MouseX = -MouseDeltaX()/10 
        MouseY = -MouseDeltaY()/10
      EndIf
      
      RobotMove = #False
      RobotJump = #False
      Angle = EntityYaw(1)
      If ExamineKeyboard()
        
        If KeyboardPushed(#PB_Key_Up) And KeyboardPushed(#PB_Key_Left)
          MoveEntity(1, -1 * Speed, 0, -1 * Speed)
          Angle = -180
          RobotMove = #True
          
        ElseIf KeyboardPushed(#PB_Key_Up) And KeyboardPushed(#PB_Key_Right)
          MoveEntity(1, 1 * Speed, 0, -1 * Speed)
          Angle = 45
          RobotMove = #True
          
        ElseIf KeyboardPushed(#PB_Key_Down) And KeyboardPushed(#PB_Key_Left)
          MoveEntity(1, -1 * Speed, 0, 1 * Speed)
          Angle = -90
          RobotMove = #True
          
        ElseIf KeyboardPushed(#PB_Key_Down) And KeyboardPushed(#PB_Key_Right)
          MoveEntity(1, 1 * Speed, 0, 1 * Speed)
          Angle = -45
          RobotMove = #True
          
        ElseIf KeyboardPushed(#PB_Key_Left)
          MoveEntity(1, -1 * Speed, 0, 0)
          Angle = 180
          RobotMove = #True
          
        ElseIf KeyboardPushed(#PB_Key_Right)
          MoveEntity(1, 1 * Speed, 0, 0)
          Angle = 0 
          RobotMove = #True
          
        ElseIf KeyboardPushed(#PB_Key_Up)
          MoveEntity(1, 0, 0, -1 * Speed)
          Angle = 90 
          RobotMove = #True
          
        ElseIf KeyboardPushed(#PB_Key_Down)
          MoveEntity(1, 0, 0, 1 * Speed)
          Angle = -90
          RobotMove = #True
        EndIf
        
        If KeyboardPushed(#PB_Key_PageUp) And Speed < 1.0
          Speed + 0.05
        ElseIf KeyboardPushed(#PB_Key_PageDown) And Speed > 0.1 
          Speed - 0.05
        EndIf
        
        If KeyboardPushed(#PB_Key_Space)
          RobotJump = #True
        EndIf
        
      EndIf
      
      RotateEntity(1, 0, CurveAngle(EntityYaw(1, #True), Angle, 4 * TimeSinceLastFrame), 0)
      
    If RobotJump
      Anim = #Jump
      Fadein = #Jump
      FadeOut = #Idle
      StartEntityAnimation(1, Anim(Anim), #PB_EntityAnimation_Once)
    EndIf
      
    If RobotMove
      If Anim <> #Walk
        Anim = #Walk
        Fadein  = #Walk
        FadeOut = #Idle 
        StartEntityAnimation(1, Anim(Anim), #PB_EntityAnimation_Manual)
      EndIf
    Else
      If Anim <> #Idle
        Anim = #Idle
        Fadein  = #Idle
        FadeOut = #Walk 
        StartEntityAnimation(1, Anim(Anim), #PB_EntityAnimation_Manual)
      EndIf
    EndIf
      
    AddEntityAnimationTime(1, Anim(Anim), TimeSinceLastFrame)
    fadeAnimations(TimeSinceLastFrame / 1000.0)
      
    RotateCamera(0, MouseY, MouseX, 0, #PB_Relative)
      
    TimeSinceLastFrame = RenderWorld() * Speed
      
    FlipBuffers()
    
  Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  
  EndIf
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized",0)
EndIf

End

Procedure fadeAnimations(deltaTime.f)
  Protected.f newWeight
  
  If FadeIn
    
    ; slowly fade this animation in Until it has full weight
    newWeight = GetEntityAnimationWeight(1, Anim(FadeIn)) + deltaTime * #ANIM_FADE_SPEED
    Clamp(newWeight, 0, 1)
    SetEntityAnimationWeight(1, Anim(FadeIn), newWeight)
    If newWeight >= 1 
      FadeIn = #False
    EndIf  
    
  EndIf
  
  If FadeOut
    
    ; slowly fade this animation out Until it has no weight, And then disable it
    newWeight.f = GetEntityAnimationWeight(1, Anim(FadeOut)) - deltaTime * #ANIM_FADE_SPEED
    Clamp(newWeight, 0, 1)
    SetEntityAnimationWeight(1, Anim(FadeOut), newWeight)
    If newWeight <= 0
      StopEntityAnimation(1, Anim(FadeOut))
      FadeOut = #False
    EndIf
  EndIf
  
EndProcedure

Procedure.f WrapPi(Angle.f) 
  Angle + 180
  Angle - Round(Angle * 1/360.0, #PB_Round_Down) * 360
  Angle - 180
  ProcedureReturn Angle
EndProcedure

Procedure.f CurveAngle(Actuelle.f, Target.f, P.f)
  Delta.f = WrapPi(Target-Actuelle)
  If P > 1000 : P = 1000 : EndIf
  Valeur.f = Actuelle + (Delta * P / 1000)
  ProcedureReturn WrapPi(Valeur)
EndProcedure 
Thank You!
User avatar
Bananenfreak
Enthusiast
Enthusiast
Posts: 519
Joined: Mon Apr 15, 2013 12:22 pm

Re: Controlling more than one animation?

Post by Bananenfreak »

I don´t know, what you mean, but do you mean this:

Code: Select all

    If RobotJump
      Anim = #Jump
      Fadein = #Jump
      FadeOut = #Idle
      StartEntityAnimation(1, Anim(Anim), #PB_EntityAnimation_Once)
      
    ElseIf RobotMove
      If Anim <> #Walk
        Anim = #Walk
        Fadein  = #Walk
        FadeOut = #Idle 
        StartEntityAnimation(1, Anim(Anim), #PB_EntityAnimation_Manual)
      EndIf
      
    Else
      If Anim <> #Idle
        Anim = #Idle
        Fadein  = #Idle
        FadeOut = #Walk 
        StartEntityAnimation(1, Anim(Anim), #PB_EntityAnimation_Manual)
      EndIf
      
    EndIf
Use this instead of your Piece of code.
-Please use Enable Explicit.

I can´t see a jumping Player... But the Animation for it ^^
Image
Mythros
Enthusiast
Enthusiast
Posts: 306
Joined: Mon Aug 19, 2013 3:28 pm

Re: Controlling more than one animation?

Post by Mythros »

Thanks ALOT, BananenFreak! This helped me figure out what was wrong and fixed it appropriately! :D Kudos to you! :)
Post Reply