jadav.rakesh Posted September 7, 2011 Posted September 7, 2011 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
manguste Posted September 15, 2011 Posted September 15, 2011 -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.
jadav.rakesh Posted October 7, 2011 Author Posted October 7, 2011 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(); }
manguste Posted October 7, 2011 Posted October 7, 2011 getTime() returns the animation time measured not in seconds, but in animation frames. getSpeed(), its turn, is measured in frames per second. So, in order to get animation time in seconds, you have to multiply getTime() by getSpeed().
jadav.rakesh Posted October 8, 2011 Author Posted October 8, 2011 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.
jadav.rakesh Posted October 8, 2011 Author Posted October 8, 2011 Hi manguste, Should I have to call setTime() before getTime() ?
manguste Posted October 18, 2011 Posted October 18, 2011 Updated info: setTime() is designed to be used only for play/stop options in the editor. In all other cases use setFrame() and getFrameTime(). For example, see how animation is set in samples/animation/animation_00.
Recommended Posts