Enities Animation

Généralités sur la programmation 3D
Avatar de l’utilisateur
cederavic
Messages : 1338
Inscription : lun. 09/févr./2004 23:38
Localisation : Bordeaux

Enities Animation

Message par cederavic »

Le system d'animation actuel est... legerement buggé (acceleration de l'anim lorsqu'on change d'anim), j'ai donc fait ces quelques proc dans le besoin :
Declare AttachEntityAnimation(Entity, File.s)

Code : Tout sélectionner

Attache un "fichier de description d'animation" à l'entité Entity. Une entité peut avoir 100 animations au MAXIMUM!
---
Entity : Identifiant de l'entité
File.s : Chemin du fichier de description d'animation 
---
Renvois #True si l'opération à été effectuée avec succès, sinon renvois #False
Declare SetEntityAnimation(Entity, Anim.s)

Code : Tout sélectionner

Change l'animation courante de l'entité Enity par l'animation qui a pour nom Anim.s
---
Entity : Identifiant de l'entité
Anim.s : Nom de l'animation
---
Renvois #True si l'opération à été effectuée avec succès, sinon renvois #False
Declare.s GetEntityAnimation(Entity)

Code : Tout sélectionner

Renvois le nom de l'animation courante
Declare AnimateEntities()

Code : Tout sélectionner

Permet d'animer correctement les entité par rapport à leur fichier de description d'animation. Cette procédure est à appeler de préférence avant  l'affichage de la scène (donc juste avant RenderWorld())
Voilas le code des procs :

Code : Tout sélectionner

Structure AnimInf
  Start.l
  End.l
  Name.s
EndStructure

Structure EntityAnim
  Entity.l
  AnimTab.AnimInf[100]
  NbAnim.l
  CurrentAnim.l
  FPS.l
EndStructure

NewList EntityAnim.EntityAnim()
  
Procedure AnimateEntities()

  ForEach EntityAnim()
  
    CurrentAnim = EntityAnim()\CurrentAnim
    Entity      = EntityAnim()\Entity
    AStart      = EntityAnim()\AnimTab[CurrentAnim]\Start
    AEnd        = EntityAnim()\AnimTab[CurrentAnim]\End
    ACurrent.f    = GetEntityAnimationTime(Entity)
    ;Debug Acurrent
    
    If ACurrent < AStart / EntityAnim()\FPS
      SetEntityAnimationTime(Entity, AStart)
    EndIf
    
    If ACurrent => AEnd / EntityAnim()\FPS
      SetEntityAnimationTime(Entity, AStart)
    EndIf
  
  Next

EndProcedure
  
Procedure AttachEntityAnimation(Entity, File.s)

  tempFile = OpenFile(#PB_Any, File)
  If tempFile
    AddElement(EntityAnim())
      EntityAnim()\Entity = Entity
      EntityAnim()\FPS = Val(Trim(ReadString()))
    Repeat
      cLine.s = Trim(ReadString())
        
        EntityAnim()\AnimTab[EntityAnim()\NbAnim]\Start = Val(StringField(cLine, 1, ","))
        EntityAnim()\AnimTab[EntityAnim()\NbAnim]\End = Val(StringField(cLine, 2, ","))
        EntityAnim()\AnimTab[EntityAnim()\NbAnim]\Name = StringField(cLine, 3, ",")

        EntityAnim()\NbAnim + 1
        
    Until Eof(tempFile)
    CloseFile(tempFile)
    AnimateEntity(Entity, "Default") 
    
    ProcedureReturn #True
  
  EndIf
  
  ProcedureReturn #False

EndProcedure

Procedure.s GetEntityAnimation(Entity)

  ForEach EntityAnim()
  
    If EntityAnim()\Entity = Entity
    
      ProcedureReturn EntityAnim()\AnimTab[EntityAnim()\CurrentAnim]\Name
    
    EndIf
  
  Next
  
  ProcedureReturn ""

EndProcedure

Procedure SetEntityAnimation(Entity, Anim.s)

  ForEach EntityAnim()
  
    If EntityAnim()\Entity = Entity
    
      For t = 0 To EntityAnim()\NbAnim
      
        If EntityAnim()\AnimTab[t]\Name = Anim
        
          EntityAnim()\CurrentAnim = t
          ProcedureReturn #True
        
        EndIf
      
      Next
    
    EndIf
  
  Next
  
  ProcedureReturn #False

EndProcedure
Exemple d'un fichier de description d'animation :
24
0,20,Walk
22,62,Idle

Code : Tout sélectionner

La première ligne indique le taux de frame(s) par seconde des animations, ensuite viennent les descriptions des animations.
Il y a ici 2 animations : Walk et Idle.
Le preimier nombre indique la premiere frame de l'animation et le deuxième, la dernière frame de l'animation.
un exemple d'utilisation :
Un fichier *.rar, contenant les ressources necessaires, est disponible à l'adresse suivante : http://perso.wanadoo.fr/persomrn/download/Gordon.rar

Voila l'exemple :

Code : Tout sélectionner

;Auteur : Cederavic
;Version de PB : 3.93
;Date : 24/05/05
;Description : Permet d'animer des entity gràce à un fichier de description d'animation

InitEngine3D()
InitSprite()
InitKeyboard()
InitMouse()

Structure AnimInf
  Start.l
  End.l
  Name.s
EndStructure

Structure EntityAnim
  Entity.l
  AnimTab.AnimInf[100]
  NbAnim.l
  CurrentAnim.l
  FPS.l
EndStructure

NewList EntityAnim.EntityAnim()

Declare AttachEntityAnimation(Entity, File.s)
Declare SetEntityAnimation(Entity, Anim.s)
Declare.s GetEntityAnimation(Entity)
Declare AnimateEntities()

OpenScreen(1024, 768, 32, "EntityAnimation")

UsePNGImageDecoder()
  
  LoadTexture(0, "Gordon.png")
  
  CreateMaterial(0, TextureID(0))
  
  LoadMesh(0, "Gordon.mesh")
  
  CreateEntity(0, MeshID(0), MaterialID(0))
  AttachEntityAnimation(0, "Gordon_Anim.txt")
  
  CreateCamera(0, 0, 0, 100, 100)
  CameraLocate(0, 0, 50, 100)
  
  Repeat

    ExamineKeyboard()
    ExamineMouse()
  
    If KeyboardPushed(#PB_Key_Left)
      cmovx = -5
    ElseIf KeyboardPushed(#PB_Key_Right)
      cmovx = 5
    Else
      cmovx = 0
    EndIf
    
    If KeyboardPushed(#PB_Key_PageUp)
      cmovy = 5
    ElseIf KeyboardPushed(#PB_Key_PageDown)
      cmovy = -5
    Else
      cmovy = 0
    EndIf
    
    If KeyboardPushed(#PB_Key_Up)
      cmovz = -5
    ElseIf KeyboardPushed(#PB_Key_Down)
      cmovz = 5
    Else
      cmovz = 0
    EndIf
    
    If KeyboardReleased(#PB_Key_Pad1)
      SetEntityAnimation(0, "Idle") 
    ElseIf KeyboardReleased(#PB_Key_Pad2)
      SetEntityAnimation(0, "Walk")
    EndIf
    
    crotx = -MouseDeltaX()
    croty = -MouseDeltaY()
    
    MoveCamera(0, cmovx, cmovy, cmovz)
    RotateCamera(0, crotx, croty, 0)
    
    
    ClearScreen(248, 186, 255)
    
    AnimateEntities()
    RenderWorld()
      
      StartDrawing(ScreenOutput())
        DrawingMode(1)
        FrontColor(248, 186, 255)
        Locate(10, 10)
        DrawText(Str(Engine3DFrameRate(#PB_Engine3D_Current)))
      StopDrawing()
    
    FlipBuffers()
    
  Until KeyboardPushed(#PB_Key_Escape)
  
Procedure AnimateEntities()

  ForEach EntityAnim()
  
    CurrentAnim = EntityAnim()\CurrentAnim
    Entity      = EntityAnim()\Entity
    AStart      = EntityAnim()\AnimTab[CurrentAnim]\Start
    AEnd        = EntityAnim()\AnimTab[CurrentAnim]\End
    ACurrent.f    = GetEntityAnimationTime(Entity)
    ;Debug Acurrent
    
    If ACurrent < AStart / EntityAnim()\FPS
      SetEntityAnimationTime(Entity, AStart)
    EndIf
    
    If ACurrent => AEnd / EntityAnim()\FPS
      SetEntityAnimationTime(Entity, AStart)
    EndIf
  
  Next

EndProcedure
  
Procedure AttachEntityAnimation(Entity, File.s)

  tempFile = OpenFile(#PB_Any, File)
  If tempFile
    AddElement(EntityAnim())
      EntityAnim()\Entity = Entity
      EntityAnim()\FPS = Val(Trim(ReadString()))
    Repeat
      cLine.s = Trim(ReadString())
        
        EntityAnim()\AnimTab[EntityAnim()\NbAnim]\Start = Val(StringField(cLine, 1, ","))
        EntityAnim()\AnimTab[EntityAnim()\NbAnim]\End = Val(StringField(cLine, 2, ","))
        EntityAnim()\AnimTab[EntityAnim()\NbAnim]\Name = StringField(cLine, 3, ",")

        EntityAnim()\NbAnim + 1
        
    Until Eof(tempFile)
    CloseFile(tempFile)
    AnimateEntity(Entity, "Default") 
    
    ProcedureReturn #True
  
  EndIf
  
  ProcedureReturn #False

EndProcedure

Procedure.s GetEntityAnimation(Entity)

  ForEach EntityAnim()
  
    If EntityAnim()\Entity = Entity
    
      ProcedureReturn EntityAnim()\AnimTab[EntityAnim()\CurrentAnim]\Name
    
    EndIf
  
  Next
  
  ProcedureReturn ""

EndProcedure

Procedure SetEntityAnimation(Entity, Anim.s)

  ForEach EntityAnim()
  
    If EntityAnim()\Entity = Entity
    
      For t = 0 To EntityAnim()\NbAnim
      
        If EntityAnim()\AnimTab[t]\Name = Anim
        
          EntityAnim()\CurrentAnim = t
          ProcedureReturn #True
        
        EndIf
      
      Next
    
    EndIf
  
  Next
  
  ProcedureReturn #False

EndProcedure
Et pour finir, une capture de l'export du mesh sous MilkShape :
Image

Voili voilou... :)[/code]
comtois
Messages : 5186
Inscription : mer. 21/janv./2004 17:48
Contact :

Message par comtois »

wow , tu as réussi à utiliser les commandes d"animation ?

J'ai juste lancé la démo , je vais étudier le code plus tard .

En tout cas bravo , bien joué #hello
Avatar de l’utilisateur
cederavic
Messages : 1338
Inscription : lun. 09/févr./2004 23:38
Localisation : Bordeaux

Message par cederavic »

Ben c'est pas très difficle :P
J'espere avoir été assez claire pour l'aide?
comtois
Messages : 5186
Inscription : mer. 21/janv./2004 17:48
Contact :

Message par comtois »

je ne sais pas ,je regarderai comment tu as fait , j'avais fait des tests avec le robot fourni avec purebasic , et ça faisait n'importe quoi :?

pourtant les commandes sont simples , mais bon ... je referai des essais. maintenant que j'ai un modèle qui fonctionne je pourrai comparer les résultats :)
Le Soldat Inconnu
Messages : 4312
Inscription : mer. 28/janv./2004 20:58
Localisation : Clermont ferrand OU Olsztyn
Contact :

Message par Le Soldat Inconnu »

8)
Je ne suis pas à moitié Polonais mais ma moitié est polonaise ... Vous avez suivi ?

[Intel quad core Q9400 2.66mhz, ATI 4870, 4Go Ram, XP (x86) / 7 (x64)]
Avatar de l’utilisateur
cederavic
Messages : 1338
Inscription : lun. 09/févr./2004 23:38
Localisation : Bordeaux

Message par cederavic »

le truc c'est que les commandes PB nous renvoi la temps de l'animation, or le numeros de frame est plus pratique, donc il n'y a qu'a faire : temps * taux
(le taux tu le définit au moment de l'export)
Répondre