Ragdoll save/load ?

AlexxFree

New member
How to load a ragdoll into the correct state?
I check the work like that.
I start the scene frame by frame.
1) activate the ragdoll and save it

rg_start.jpg

2) simulate a drop a few frames ahead

rg_progress.jpg

3) load the saved state of the ragdoll

rg_restored.jpg

all colliders for some reason in the wrong places


C#:
public override void Save(JObject jObject)

{
    base.Save(jObject);

 
    if (IsActive)
    {
        var jrbs = new JArray();

        for (int i = 0; i < m_Rigidbodies.Length; ++i)
        {
            var rb = m_Rigidbodies[i];

            var jEntity = new JObject();

            jEntity.Add("pos", rb.position.GetAsString());
            jEntity.Add("rot", rb.rotation.GetAsString());
            jEntity.Add("vel", rb.velocity.GetAsString());
            jEntity.Add("avel", rb.angularVelocity.GetAsString());

            jrbs.Add(jEntity);
        }

        jObject.Add("rbs", jrbs);
    }
}


public override void Load(JObject jObject)
{
    base.Load(jObject);

    var prop = jObject.Property("rbs");
    if (prop != null)
    {
        m_Force = Vector3.zero;
        m_Position = Vector3.zero;
        m_CharacterLocomotion.TryStartAbility(this, true, true);

        var jrbs = (JArray)prop.Value;
        
        var index = 0;
        foreach (var rbInfo in jrbs)
        {
            var rb = m_Rigidbodies[index];

            var jEntity = (JObject)rbInfo;
            rb.position = SerializationUtils.Vector3Parse((string)jEntity.GetValue("pos"));
            rb.rotation = SerializationUtils.QuaternionParse((string)jEntity.GetValue("rot"));
            rb.velocity = SerializationUtils.Vector3Parse((string)jEntity.GetValue("vel"));
            rb.angularVelocity = SerializationUtils.Vector3Parse((string)jEntity.GetValue("avel"));
            index++;
        }
    }

}
 
Try removing the Smoothed Bones on the Ultimate Character Locomotion component. My guess is that the KinematicObjectManager is resetting the bone position.
 
Try removing the Smoothed Bones on the Ultimate Character Locomotion component. My guess is that the KinematicObjectManager is resetting the bone position.

To use this functionality, I added a flag and an event to CharacterLocomotion


public bool IsBonesSmooth
{
get { return m_isBonesSmooth; }
set
{
if (m_isBonesSmooth != value)
{
m_isBonesSmooth = value;
OnBonesSmothChangedEvent();
}
}
}

in KinematicCharacter by event
do ClearSmoothedBones();
or SetupSmoothedBones();


use

m_CharacterLocomotion.IsBonesSmooth = false;

var jrbs = (JArray)prop.Value;

var index = 0;
foreach (var rbInfo in jrbs)
{
var rb = m_Rigidbodies[index];

var jEntity = (JObject)rbInfo;
rb.position = SerializationUtils.Vector3Parse((string)jEntity.GetValue("pos"));
rb.rotation = SerializationUtils.QuaternionParse((string)jEntity.GetValue("rot"));
rb.velocity = SerializationUtils.Vector3Parse((string)jEntity.GetValue("vel"));
rb.angularVelocity = SerializationUtils.Vector3Parse((string)jEntity.GetValue("avel"));

// hack for some reason, even if I deleted the bones from kinematic,
// ... ObjectPool.Return(m_SmoothedBones);...
// the first update still spoils the character’s position
var t = m_RigidbodyGameObjects[index].transform;
t.position = rb.position;
t.rotation = rb.rotation;

index++;
}

m_CharacterLocomotion.IsBonesSmooth = true;
 
Top