XNA Creators Club Online
Page 1 of 1 (6 items)
Sort Posts: Previous Next

[Solved] Animation Trouble - Multiple instances of the same model always have same animation

Last post 05-23-2008 8:12 PM by Xavier. 5 replies.
  • 02-25-2008 7:24 PM

    [Solved] Animation Trouble - Multiple instances of the same model always have same animation

    Hello,

    I'm working on a simple game right now. I have some animated character models created with XSI that I am using. The problem I am having is that, if i have two character models using the same model, the animation players refer to the same object. It seems perhaps that the problem is that you have to call:

    l_Animations.ResolveBones(Model),

     which loops through the model and records references to all the ModelBones, which would be a shallow copy instead of a deep copy (because the content manager always returns the same model). This seems counterintuitive, as it would be very common for games to need the same model animated different ways, so its definitely possible the problem is in my code.

    Also, I don't have the same problem with the SkinnedModel sample on XNA, this seems to work fine... but we would prefer to use .xsi if possible

    Has anyone encountered a similar problem and maybe found a workaround?

    Thanks!

    EDIT: I found a workaround - I moved the code that handled the updating of the animation (Playback() etc) into the Draw function, before the bone transforms are copied. This fixed the problem

     

  • 05-10-2008 10:16 PM In reply to

    Re: [Solved] Animation Trouble - Multiple instances of the same model always have same animation

    any chance i can see the code you used? or something like it? i have the same problem as you described and more or less thats the only problem that is stopping me from releasing my game. i tried putting the playback() thing in the draw method but the other instances seem to still follow the suit, if you can show me what you did im sure i can figuer it out.
  • 05-12-2008 2:23 PM In reply to

    Re: [Solved] Animation Trouble - Multiple instances of the same model always have same animation

    Sure thing - my solution was to basically move the animation logic from the Update() function to the Draw() function. I haven't thought about this in a while but I believe the problem was that the model instance is shared, thus, when you set the bone transforms, you set it for that shared instance. So if you update the animation player and set the transforms for the shared model, only the last update persists, because every update is updating the shared model.

    I fixed this by moving the animation updating logic to the code that is called in Draw(), right before the bone transforms are set. This works because you are forcing the animations you need onto the shared instance, right before you used the shared instance. Hopefully that makes sense! Here's an excerpt of the code I used it in, this is the "rendering module" of my entity class:

    AnimationUpdate simply does the logic of running the animation player, and is called in Draw(), althought it used to be in Update()

    Good luck, hope that helps... I can post the whole file too if you'd like

        void AnimationUpdate()
    {
    if (AnimationIndex == -1)
    return;

    Animations[AnimationIndex].PlayBack(TimeSpan.FromSeconds((lastTime - animStart) / animSpeed
    ), 1.0f);


    if (Animations[AnimationIndex].Loop == true && nextAnimation < lastTime)
    {
    Animations[AnimationIndex].Reset();
    nextAnimation = lastTime + currentAnimLength;
    animStart = lastTime;
    }
    }

    public void Render()
    {
    if (refreshWorld)
    world = Matrix.CreateScale(scale) * Matrix.CreateFromYawPitchRoll(rotation.X, rotation.Y, rotation.Z)
    * Matrix.CreateTranslation(translation);

    AnimationUpdate();

    XSIAnimationData l_Animations = model.Tag as XSIAnimationData;

    model.CopyAbsoluteBoneTransformsTo(transforms);
    l_Animations.ComputeBoneTransforms(transforms);
    bones = l_Animations.BoneTransforms;

    bool isSkinned = (bones.GetLength(0) > 0);




    for(int i = 0; i < model.Meshes.Count; i++)
    {
    foreach (Effect effect in model.MeshesIdea.Effects)
    {
    EffectTechnique tech;
    if (isSkinned)
    tech = effect.Techniques["Skinned"];
    else
    tech = effect.Techniques["Static"];

    BatchRender.BatchRender(effect, tech, i, this);
    }
    }



    }
  • 05-12-2008 2:51 PM In reply to

    Re: [Solved] Animation Trouble - Multiple instances of the same model always have same animation

    i guess that kinda makes sense, can you send variables values so its easier to determine exactly whats happening? Also, i assume that much like myself, you are trying to animate alot of models at once. how many instances can you use befor any real lag starts to set in? In the game im trying to make, at most there could be around 1000 instances on screen at once which i assume will cause some serious lag issues.
  • 05-16-2008 12:06 PM In reply to

    Re: [Solved] Animation Trouble - Multiple instances of the same model always have same animation

    Hey Hex,

    I put the whole code for my RenderEntityModule class on pastebin here: http://pastebin.com/f18e1976e

    I apologize if it is confusing, i didn't get a chance to really clean it up... but if you have any questions i'll be happy to do my best to answer them =)

    As for my game, I didn't really plan on having a lot of animated models at once - 32 probably at max on screen at a time. I would imagine with a 1000 animated models you would get some definite lag issues, depending on the vertex count and of the models and numerous other issues of course

    Are all your models animated? If not, I would suggest looking at the Instanced Model sample, as hardware instancing or xbox 360 instancing would likely be faster than drawing each model individually, because of the overhead associated with the draw calls. If they are all animated... well... instancing can't do too much for you then, for now (unless you have the rare case of every instance having the same animation)! But perhaps some sort of culling, like frustrum culling might be helpful.

     

  • 05-23-2008 8:12 PM In reply to

    Re: [Solved] Animation Trouble - Multiple instances of the same model always have same animation

    Hey guys, I have a problem somehow similar to yours, I have have an FBX file and imported it to two different Model objects but XNA makes the two objects have the same refrence, so when I change the diffuse color of an effect in one of the model objects, the other model object is affected too, so I just can't render the same model twice with different colors, the only solution is to make a copy to the FBX file but this is not a good idea because this will be a multiplayer game so I wont be able to make a copy of the file for each player !

    Another solution that I discovered is to change the color of the model's effect just before rendering it to the color corresponding to that player but this will be very laggy I think, so do you know anyway I can prevent Content.load<Model> from giving the same refrence for both Model objects or a way to copy the memory refrence ?

Page 1 of 1 (6 items) Previous Next