Jump to content

[SOLVED] Loading and Playing Animations


photo

Recommended Posts

Hi guys,

I was looking into "character system" for how to load animations and play them. I came across some code which actually confused me.

 

This is from actor.h from one of the samples :

 

string animations[] = (
 STATE_WALK_IDLE   : "idle.sanim",
 STATE_WALK_JUMP   : "jump.sanim",
 STATE_WALK_FORWARD  : "walk_fwd.sanim",
 STATE_WALK_BACKWARD  : "walk_bwd.sanim",
 STATE_WALK_MOVE_LEFT : "walk_lt.sanim",
 STATE_WALK_MOVE_RIGHT : "walk_rt.sanim",
 STATE_WALK_TURN_LEFT : "walk_lt.sanim",
 STATE_WALK_TURN_RIGHT : "walk_rt.sanim",
 STATE_RUN_FORWARD  : "run_fwd.sanim",
 STATE_RUN_BACKWARD  : "walk_bwd.sanim",
 STATE_RUN_MOVE_LEFT  : "run_lt.sanim",
 STATE_RUN_MOVE_RIGHT : "run_rt.sanim",
);
PlayerActorSkinned(PlayerActor a) {

 actor = a;

 mesh = add_editor(new ObjectMeshSkinned("samples/players/meshes/actor_00.smesh"));
 for(int i = 0; i < mesh.getNumSurfaces(); i++) {
  mesh.setMaterial(get_material(i),i);
  mesh.setProperty("surface_base","*");
  mesh.setCollision(0,i);
 }
 mesh.setNumLayers(7);

 foreach(string animation; animations) {
  animation = "samples/players/meshes/actor_00/" + animation;
  mesh.setAnimation(animation);
 }
}

void set_layer(int layer,float weight) {
 mesh.setLayer(layer);
 mesh.setWeight(weight);
}

void set_frame(int state,float time) {
 if(mesh.getWeight() > EPSILON) {
  mesh.setAnimation(animations[state]);
  mesh.setFrame(time * fps[state]);
 }
}

 

My confusion :

-Why setAnimation function take different arguments once with path and once without path.

-Will it always load animation from external file, that's what description of the function says. Isn't it slow bcoz second setAnimation is being called from "::update" ?

 

 

And one more thing can we TWO set animations in one LAYER so blend them or each animation must be in separate layer ?

 

BTW, character system is really helpful.

 

Thanks

 

Thanks

Link to comment
  • 2 weeks later...

-Why setAnimation function take different arguments once with path and once without path.

There's a bit of a trick here. setAnimation() should be given a proper path - when loading the animation for the first time. But once the animation has been set, the next time you load the same animation, it internally compares two strings (the first provided one and the current one) and if they match at least partially, uses the already loaded animation.

 

 

-Will it always load animation from external file, that's what description of the function says. Isn't it slow bcoz second setAnimation is being called from "::update" ?

As it uses animation cache to set the already loaded animation, it can be called every update() without any penalty, if necessary.

 

 

And one more thing can we TWO set animations in one LAYER so blend them or each animation must be in separate layer ?

Each separate animation uses its own layer. Layers can be blended together with different weights to create combined animation.

Link to comment
  • 3 weeks later...

Hi manguste,

One more thing, How can we get length of the particular animation in seconds ?

 

This is what I did but it's not working. I don't know whether this is correct or not. :(

int SetAnimation(Node node, int iLayerIndex, string & strAnimFilePath, float fWeight)
{
 ObjectMeshSkinned objMeshSkinned = node_cast(node);
 objMeshSkinned.setLayer(iLayerIndex);
 objMeshSkinned.setWeight(fWeight);
 objMeshSkinned.setAnimation(strAnimFilePath);
 return 1;
}

float GetAnimationTime(Node node, int iLayerIndex)
{
 ObjectMeshSkinned objMeshSkinned = node_cast(node);
 objMeshSkinned.setLayer(iLayerIndex);
 return objMeshSkinned.getTime();
}

Link to comment

Hi manguste,

 

Thanks for quick replay and resolving confusion, but to my big surprise getTime() returns 0.0f for every animations even though it's working fine in editor.

Link to comment
  • 2 weeks later...
×
×
  • Create New...