Skip to content

Commit

Permalink
[ERA-1] ragdoll skinning p3
Browse files Browse the repository at this point in the history
  • Loading branch information
EldarMuradov committed Aug 29, 2024
1 parent 50efbea commit 11142a7
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 88 deletions.
2 changes: 1 addition & 1 deletion modules/core/src/animation/animation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@ namespace era_engine::animation
}

currentGlobalTransforms = globalTransforms;

// TODO
ragdoll->updateKinematic();

if (animation->finished)
Expand Down
186 changes: 99 additions & 87 deletions modules/core/src/px/features/px_ragdoll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ namespace era_engine::physics
len -= len * 0.1f;

if(len - 2.0f * r > 0)
len = len - 2.0f * r;
len = len - r;

float halfHeight = len / 2.0f;

Expand Down Expand Up @@ -274,6 +274,7 @@ namespace era_engine::physics

transforms.resize(joints.size());

ragdoll->initialBodyPos.resize(joints.size());
ragdoll->rigidBodies.resize(joints.size());
ragdoll->childToParentJointDeltaPoses.resize(joints.size());
ragdoll->childToParentJointDeltaRots.resize(joints.size());
Expand Down Expand Up @@ -322,7 +323,7 @@ namespace era_engine::physics
modelWithoutScale,
modelOnlyScale,
model,
r,
8.0f * scale,
rot);

PxRigidDynamic* r_leg = createCapsuleBone(
Expand All @@ -334,32 +335,32 @@ namespace era_engine::physics
modelWithoutScale,
modelOnlyScale,
model,
r,
8.0f * scale,
rot);

PxRigidDynamic* l_calf = createCapsuleBone(
j_calf_l_idx,
j_foot_l_idx,
*ragdoll,
material,
skeleton,
modelWithoutScale,
modelOnlyScale,
model,
r,
rot);

PxRigidDynamic* r_calf = createCapsuleBone(
j_calf_r_idx,
j_foot_r_idx,
*ragdoll,
material,
skeleton,
modelWithoutScale,
modelOnlyScale,
model,
r,
rot);
//PxRigidDynamic* l_calf = createCapsuleBone(
// j_calf_l_idx,
// j_foot_l_idx,
// *ragdoll,
// material,
// skeleton,
// modelWithoutScale,
// modelOnlyScale,
// model,
// 7.0f * scale,
// rot);

//PxRigidDynamic* r_calf = createCapsuleBone(
// j_calf_r_idx,
// j_foot_r_idx,
// *ragdoll,
// material,
// skeleton,
// modelWithoutScale,
// modelOnlyScale,
// model,
// 7.0f * scale,
// rot);

PxRigidDynamic* l_arm = createCapsuleBone(
j_upperarm_l_idx,
Expand All @@ -370,7 +371,7 @@ namespace era_engine::physics
modelWithoutScale,
modelOnlyScale,
model,
r);
8.0f * scale);

PxRigidDynamic* r_arm = createCapsuleBone(
j_upperarm_r_idx,
Expand All @@ -381,31 +382,31 @@ namespace era_engine::physics
modelWithoutScale,
modelOnlyScale,
model,
r);

PxRigidDynamic* l_forearm = createCapsuleBone(
j_lowerarm_l_idx,
j_hand_l_idx,
*ragdoll,
material,
skeleton,
modelWithoutScale,
modelOnlyScale,
model,
r);

PxRigidDynamic* r_forearm = createCapsuleBone(
j_lowerarm_r_idx,
j_hand_r_idx,
*ragdoll,
material,
skeleton,
modelWithoutScale,
modelOnlyScale,
model,
r);

PxRigidDynamic* l_hand = createSphereBone(
8.0f * scale);

//PxRigidDynamic* l_forearm = createCapsuleBone(
// j_lowerarm_l_idx,
// j_hand_l_idx,
// *ragdoll,
// material,
// skeleton,
// modelWithoutScale,
// modelOnlyScale,
// model,
// 7.0f * scale);

//PxRigidDynamic* r_forearm = createCapsuleBone(
// j_lowerarm_r_idx,
// j_hand_r_idx,
// *ragdoll,
// material,
// skeleton,
// modelWithoutScale,
// modelOnlyScale,
// model,
// 7.0f * scale);

/*PxRigidDynamic* l_hand = createSphereBone(
j_middle_01_l_idx,
*ragdoll,
material,
Expand All @@ -421,11 +422,11 @@ namespace era_engine::physics
r,
skeleton,
model,
1.0f);
1.0f);*/

rot = mat4::identity;

PxRigidDynamic* l_foot = createCapsuleBone(
/*PxRigidDynamic* l_foot = createCapsuleBone(
j_foot_l_idx,
j_ball_l_idx,
*ragdoll,
Expand All @@ -447,7 +448,7 @@ namespace era_engine::physics
modelOnlyScale,
model,
r,
rot);
rot);*/

for (int i = 1; i < skeleton->joints.size(); i++)
{
Expand All @@ -464,6 +465,7 @@ namespace era_engine::physics

trs jointTrs = mat4ToTRS(skeleton->joints[i].bindTransform);

ragdoll->initialBodyPos[i] = bodyGlobalTransform.position;
ragdoll->relativeJointPoses[i] = (invBodyPosTransformMat * vec4(jointTrs.position, 1.0f)).xyz;
ragdoll->originalBodyRotations[i] = normalize(bodyGlobalTransform.rotation);

Expand All @@ -476,7 +478,7 @@ namespace era_engine::physics

ragdoll->childToParentJointDeltaPoses[i] = bodyGlobalTransform.position / scale - jointTrs.position;

ragdoll->childToParentJointDeltaRots[i] = normalize(bodyGlobalTransform.rotation * conjugate(jointTrs.rotation));
ragdoll->childToParentJointDeltaRots[i] = normalize(conjugate(jointTrs.rotation) * bodyGlobalTransform.rotation);

finishBody(body, 1.0f, 1.0f);
}
Expand All @@ -491,23 +493,23 @@ namespace era_engine::physics

// Left Leg
scene->addActor(*l_leg);
scene->addActor(*l_calf);
scene->addActor(*l_foot);
//scene->addActor(*l_calf);
//scene->addActor(*l_foot);

// Right Leg
scene->addActor(*r_leg);
scene->addActor(*r_calf);
scene->addActor(*r_foot);
//scene->addActor(*r_calf);
//scene->addActor(*r_foot);

// Left Arm
scene->addActor(*l_arm);
scene->addActor(*l_forearm);
scene->addActor(*l_hand);
//scene->addActor(*l_forearm);
//scene->addActor(*l_hand);

// Right Arm
scene->addActor(*r_arm);
scene->addActor(*r_forearm);
scene->addActor(*r_hand);
//scene->addActor(*r_forearm);
//scene->addActor(*r_hand);

//// ---------------------------------------------------------------------------------------------------------------
//// Create joints
Expand All @@ -521,35 +523,35 @@ namespace era_engine::physics
createD6Joint(pelvis, r_leg, skeleton, j_thigh_r_idx, model);

// Thighs to Calf
createD6Joint(l_leg, l_calf, skeleton, j_calf_l_idx, model);
createD6Joint(r_leg, r_calf, skeleton, j_calf_r_idx, model);
//createRevoluteJoint(l_leg, l_calf, skeleton, j_calf_l_idx, mat4::identity, model);
//createRevoluteJoint(r_leg, r_calf, skeleton, j_calf_r_idx, mat4::identity, model);
//createD6Joint(l_leg, l_calf, skeleton, j_calf_l_idx, model);
//createD6Joint(r_leg, r_calf, skeleton, j_calf_r_idx, model);
////createRevoluteJoint(l_leg, l_calf, skeleton, j_calf_l_idx, mat4::identity, model);
////createRevoluteJoint(r_leg, r_calf, skeleton, j_calf_r_idx, mat4::identity, model);

// Calf to Foot
createD6Joint(l_calf, l_foot, skeleton, j_foot_l_idx, model);
createD6Joint(r_calf, r_foot, skeleton, j_foot_r_idx, model);
////createD6Joint(l_calf, l_foot, skeleton, j_foot_l_idx, model);
////createD6Joint(r_calf, r_foot, skeleton, j_foot_r_idx, model);

// Chest to Upperarm
createD6Joint(pelvis, l_arm, skeleton, j_upperarm_l_idx, model);
createD6Joint(pelvis, r_arm, skeleton, j_upperarm_r_idx, model);

//mat4 arm_rot = mat4::identity;
//arm_rot = trsToMat4(trs{vec3(0.0f), quat(vec3(0.0f, 0.0f, 1.0f), -PxPi / 2.0f) });
////mat4 arm_rot = mat4::identity;
////arm_rot = trsToMat4(trs{vec3(0.0f), quat(vec3(0.0f, 0.0f, 1.0f), -PxPi / 2.0f) });

// Upperarm to Lowerman
//createRevoluteJoint(l_arm, l_forearm, skeleton, j_lowerarm_l_idx, arm_rot, model);
createD6Joint(l_arm, l_forearm, skeleton, j_lowerarm_l_idx, model);
////createRevoluteJoint(l_arm, l_forearm, skeleton, j_lowerarm_l_idx, arm_rot, model);
//createD6Joint(l_arm, l_forearm, skeleton, j_lowerarm_l_idx, model);

//arm_rot = mat4::identity;
//arm_rot = trsToMat4(trs{ vec3(0.0f), quat(vec3(0.0f, 0.0f, 1.0f), PxPi / 2.0f) });
createD6Joint(r_arm, r_forearm, skeleton, j_lowerarm_r_idx, model);
////arm_rot = mat4::identity;
////arm_rot = trsToMat4(trs{ vec3(0.0f), quat(vec3(0.0f, 0.0f, 1.0f), PxPi / 2.0f) });
//createD6Joint(r_arm, r_forearm, skeleton, j_lowerarm_r_idx, model);

//createRevoluteJoint(r_arm, r_forearm, skeleton, j_lowerarm_r_idx, arm_rot, model);
////createRevoluteJoint(r_arm, r_forearm, skeleton, j_lowerarm_r_idx, arm_rot, model);

// Lowerman to Hand
createD6Joint(l_forearm, l_hand, skeleton, j_hand_l_idx, model);
createD6Joint(r_forearm, r_hand, skeleton, j_hand_r_idx, model);
//createD6Joint(l_forearm, l_hand, skeleton, j_hand_l_idx, model);
//createD6Joint(r_forearm, r_hand, skeleton, j_hand_r_idx, model);

ragdoll->setKinematic(false);

Expand Down Expand Up @@ -587,7 +589,6 @@ namespace era_engine::physics

if (ragdoll->rigidBodies.size() > 0)
{

std::vector<skeleton_joint>& joints = skeleton->joints;

transforms[0] = mat4ToTRS(joints[0].bindTransform);
Expand All @@ -599,17 +600,28 @@ namespace era_engine::physics

PxTransform pxPose = body->getGlobalPose();
trs bodyGlobalTransform = trs{ createVec3(pxPose.p), createQuat(pxPose.q) };
trs jointTrs = mat4ToTRS(skeleton->joints[i].bindTransform);

vec4 globalJointPos = trsToMat4(bodyGlobalTransform) * vec4(ragdoll->relativeJointPoses[i], 1.0f);
quat deltaRotation = bodyGlobalTransform.rotation * conjugate(ragdoll->originalBodyRotations[i]);
mat4 translationRot;

if(chosenIdx == i)
translationRot = trsToMat4(trs{ bodyGlobalTransform.position / scale - ragdoll->childToParentJointDeltaPoses[i], jointTrs.rotation * conjugate(deltaRotation) });
else
{
auto& parent = joints[chosenIdx];
trs parentPos = mat4ToTRS(parent.bindTransform);

trs jointTrs = mat4ToTRS(skeleton->joints[i].bindTransform);
vec3 localPos = jointTrs.position - parentPos.position;

localPos = deltaRotation * localPos;

quat deltaRotation = normalize(conjugate(ragdoll->originalBodyRotations[i]) * bodyGlobalTransform.rotation);
vec3 pos = localPos + parentPos.position + (bodyGlobalTransform.position / scale - ragdoll->initialBodyPos[i] / scale);

mat4 translation = trsToMat4(trs{ bodyGlobalTransform.position / scale - ragdoll->childToParentJointDeltaPoses[i], quat::identity});
mat4 rotation = modelRotation * trsToMat4(trs{ vec3(0.0f), jointTrs.rotation * deltaRotation });
translationRot = trsToMat4(trs{ pos, deltaRotation * jointTrs.rotation });
}

transforms[i] = mat4ToTRS(modelWithoutScale * translation * rotation);
transforms[i] = mat4ToTRS(invert(modelWithoutScale) * translationRot);
}
}

Expand Down
2 changes: 2 additions & 0 deletions modules/core/src/px/features/px_ragdoll.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ namespace era_engine::physics
std::vector<vec3> bodyPosRelativeToJoint;
std::vector<quat> originalJointRotations;

std::vector<vec3> initialBodyPos;

bool kinematic = false;

PxRigidDynamic* findRecentBody(uint32_t idx, ref<animation::animation_skeleton> skeleton, uint32_t& chosenIdx);
Expand Down

0 comments on commit 11142a7

Please sign in to comment.