/* ================ idIK_Reach::Evaluate ================ */ void idIK_Reach::Evaluate( void ) { int i; idVec3 modelOrigin, shoulderOrigin, elbowOrigin, handOrigin, shoulderDir, elbowDir; idMat3 modelAxis, axis; idMat3 shoulderAxis[MAX_ARMS], elbowAxis[MAX_ARMS]; trace_t trace; modelOrigin = self->GetRenderEntity()->origin; modelAxis = self->GetRenderEntity()->axis; // solve IK for( i = 0; i < numArms; i++ ) { // get the position of the shoulder in world space animator->GetJointTransform( shoulderJoints[i], gameLocal.time, shoulderOrigin, axis ); shoulderOrigin = modelOrigin + shoulderOrigin * modelAxis; shoulderDir = shoulderForward[i] * axis * modelAxis; // get the position of the hand in world space animator->GetJointTransform( handJoints[i], gameLocal.time, handOrigin, axis ); handOrigin = modelOrigin + handOrigin * modelAxis; // get first collision going from shoulder to hand gameLocal.clip.TracePoint( trace, shoulderOrigin, handOrigin, CONTENTS_SOLID, self ); handOrigin = trace.endpos; // get the IK bend direction animator->GetJointTransform( elbowJoints[i], gameLocal.time, elbowOrigin, axis ); elbowDir = elbowForward[i] * axis * modelAxis; // solve IK and calculate elbow position SolveTwoBones( shoulderOrigin, handOrigin, elbowDir, upperArmLength[i], lowerArmLength[i], elbowOrigin ); if( ik_debug.GetBool() ) { gameRenderWorld->DebugLine( colorCyan, shoulderOrigin, elbowOrigin ); gameRenderWorld->DebugLine( colorRed, elbowOrigin, handOrigin ); gameRenderWorld->DebugLine( colorYellow, elbowOrigin, elbowOrigin + elbowDir ); gameRenderWorld->DebugLine( colorGreen, elbowOrigin, elbowOrigin + shoulderDir ); } // get the axis for the shoulder joint GetBoneAxis( shoulderOrigin, elbowOrigin, shoulderDir, axis ); shoulderAxis[i] = upperArmToShoulderJoint[i] * ( axis * modelAxis.Transpose() ); // get the axis for the elbow joint GetBoneAxis( elbowOrigin, handOrigin, elbowDir, axis ); elbowAxis[i] = lowerArmToElbowJoint[i] * ( axis * modelAxis.Transpose() ); } for( i = 0; i < numArms; i++ ) { animator->SetJointAxis( shoulderJoints[i], JOINTMOD_WORLD_OVERRIDE, shoulderAxis[i] ); animator->SetJointAxis( elbowJoints[i], JOINTMOD_WORLD_OVERRIDE, elbowAxis[i] ); } ik_activate = true; }
/* ================ idIK_Walk::Evaluate ================ */ void idIK_Walk::Evaluate() { int i, newPivotFoot = -1; float modelHeight, jointHeight, lowestHeight, floorHeights[MAX_LEGS]; float shift, smallestShift, newHeight, step, newPivotYaw, height, largestAnkleHeight; idVec3 modelOrigin, normal, hipDir, kneeDir, start, end, jointOrigins[MAX_LEGS]; idVec3 footOrigin, ankleOrigin, kneeOrigin, hipOrigin, waistOrigin; idMat3 modelAxis, waistAxis, axis; idMat3 hipAxis[MAX_LEGS], kneeAxis[MAX_LEGS], ankleAxis[MAX_LEGS]; trace_t results; if ( !self || !gameLocal.isNewFrame ) { return; } // if no IK enabled on any legs if ( !enabledLegs ) { return; } normal = - self->GetPhysics()->GetGravityNormal(); modelOrigin = self->GetPhysics()->GetOrigin(); modelAxis = self->GetRenderEntity()->axis; modelHeight = modelOrigin * normal; modelOrigin += modelOffset * modelAxis; // create frame without joint mods animator->CreateFrame( gameLocal.time, false ); // get the joint positions for the feet lowestHeight = idMath::INFINITY; for ( i = 0; i < numLegs; i++ ) { animator->GetJointTransform( footJoints[i], gameLocal.time, footOrigin, axis ); jointOrigins[i] = modelOrigin + footOrigin * modelAxis; jointHeight = jointOrigins[i] * normal; if ( jointHeight < lowestHeight ) { lowestHeight = jointHeight; newPivotFoot = i; } } if ( usePivot ) { newPivotYaw = modelAxis[0].ToYaw(); // change pivot foot if ( newPivotFoot != pivotFoot || idMath::Fabs( idMath::AngleNormalize180( newPivotYaw - pivotYaw ) ) > 30.0f ) { pivotFoot = newPivotFoot; pivotYaw = newPivotYaw; animator->GetJointTransform( footJoints[pivotFoot], gameLocal.time, footOrigin, axis ); pivotPos = modelOrigin + footOrigin * modelAxis; } // keep pivot foot in place jointOrigins[pivotFoot] = pivotPos; } // get the floor heights for the feet for ( i = 0; i < numLegs; i++ ) { if ( !( enabledLegs & ( 1 << i ) ) ) { continue; } start = jointOrigins[i] + normal * footUpTrace; end = jointOrigins[i] - normal * footDownTrace; gameLocal.clip.Translation( results, start, end, footModel, mat3_identity, CONTENTS_SOLID|CONTENTS_IKCLIP, self ); floorHeights[i] = results.endpos * normal; if ( ik_debug.GetBool() && footModel ) { idFixedWinding w; for ( int j = 0; j < footModel->GetTraceModel()->numVerts; j++ ) { w += footModel->GetTraceModel()->verts[j]; } gameRenderWorld->DebugWinding( colorRed, w, results.endpos, results.endAxis ); } } const idPhysics *phys = self->GetPhysics(); // test whether or not the character standing on the ground bool onGround = phys->HasGroundContacts(); // test whether or not the character is standing on a plat bool onPlat = false; for ( i = 0; i < phys->GetNumContacts(); i++ ) { idEntity *ent = gameLocal.entities[ phys->GetContact( i ).entityNum ]; if ( ent != NULL && ent->IsType( idPlat::Type ) ) { onPlat = true; break; } } // adjust heights of the ankles smallestShift = idMath::INFINITY; largestAnkleHeight = -idMath::INFINITY; for ( i = 0; i < numLegs; i++ ) { if ( onGround && ( enabledLegs & ( 1 << i ) ) ) { shift = floorHeights[i] - modelHeight + footShift; } else { shift = 0.0f; } if ( shift < smallestShift ) { smallestShift = shift; } animator->GetJointTransform( ankleJoints[i], gameLocal.time, ankleOrigin, ankleAxis[i] ); jointOrigins[i] = modelOrigin + ankleOrigin * modelAxis; height = jointOrigins[i] * normal; if ( oldHeightsValid && !onPlat ) { step = height + shift - oldAnkleHeights[i]; shift -= smoothing * step; } newHeight = height + shift; if ( newHeight > largestAnkleHeight ) { largestAnkleHeight = newHeight; } oldAnkleHeights[i] = newHeight; jointOrigins[i] += shift * normal; } animator->GetJointTransform( waistJoint, gameLocal.time, waistOrigin, waistAxis ); waistOrigin = modelOrigin + waistOrigin * modelAxis; // adjust position of the waist waistOffset = ( smallestShift + waistShift ) * normal; // if the waist should be at least a certain distance above the floor if ( minWaistFloorDist > 0.0f && waistOffset * normal < 0.0f ) { start = waistOrigin; end = waistOrigin + waistOffset - normal * minWaistFloorDist; gameLocal.clip.Translation( results, start, end, footModel, modelAxis, CONTENTS_SOLID|CONTENTS_IKCLIP, self ); height = ( waistOrigin + waistOffset - results.endpos ) * normal; if ( height < minWaistFloorDist ) { waistOffset += ( minWaistFloorDist - height ) * normal; } } // if the waist should be at least a certain distance above the ankles if ( minWaistAnkleDist > 0.0f ) { height = ( waistOrigin + waistOffset ) * normal; if ( height - largestAnkleHeight < minWaistAnkleDist ) { waistOffset += ( minWaistAnkleDist - ( height - largestAnkleHeight ) ) * normal; } } if ( oldHeightsValid ) { // smoothly adjust height of waist newHeight = ( waistOrigin + waistOffset ) * normal; step = newHeight - oldWaistHeight; waistOffset -= waistSmoothing * step * normal; } // save height of waist for smoothing oldWaistHeight = ( waistOrigin + waistOffset ) * normal; if ( !oldHeightsValid ) { oldHeightsValid = true; return; } // solve IK for ( i = 0; i < numLegs; i++ ) { // get the position of the hip in world space animator->GetJointTransform( hipJoints[i], gameLocal.time, hipOrigin, axis ); hipOrigin = modelOrigin + waistOffset + hipOrigin * modelAxis; hipDir = hipForward[i] * axis * modelAxis; // get the IK bend direction animator->GetJointTransform( kneeJoints[i], gameLocal.time, kneeOrigin, axis ); kneeDir = kneeForward[i] * axis * modelAxis; // solve IK and calculate knee position SolveTwoBones( hipOrigin, jointOrigins[i], kneeDir, upperLegLength[i], lowerLegLength[i], kneeOrigin ); if ( ik_debug.GetBool() ) { gameRenderWorld->DebugLine( colorCyan, hipOrigin, kneeOrigin ); gameRenderWorld->DebugLine( colorRed, kneeOrigin, jointOrigins[i] ); gameRenderWorld->DebugLine( colorYellow, kneeOrigin, kneeOrigin + hipDir ); gameRenderWorld->DebugLine( colorGreen, kneeOrigin, kneeOrigin + kneeDir ); } // get the axis for the hip joint GetBoneAxis( hipOrigin, kneeOrigin, hipDir, axis ); hipAxis[i] = upperLegToHipJoint[i] * ( axis * modelAxis.Transpose() ); // get the axis for the knee joint GetBoneAxis( kneeOrigin, jointOrigins[i], kneeDir, axis ); kneeAxis[i] = lowerLegToKneeJoint[i] * ( axis * modelAxis.Transpose() ); } // set the joint mods animator->SetJointAxis( waistJoint, JOINTMOD_WORLD_OVERRIDE, waistAxis ); animator->SetJointPos( waistJoint, JOINTMOD_WORLD_OVERRIDE, ( waistOrigin + waistOffset - modelOrigin ) * modelAxis.Transpose() ); for ( i = 0; i < numLegs; i++ ) { animator->SetJointAxis( hipJoints[i], JOINTMOD_WORLD_OVERRIDE, hipAxis[i] ); animator->SetJointAxis( kneeJoints[i], JOINTMOD_WORLD_OVERRIDE, kneeAxis[i] ); animator->SetJointAxis( ankleJoints[i], JOINTMOD_WORLD_OVERRIDE, ankleAxis[i] ); } ik_activate = true; }
/* ================ sdIK_Walker::Evaluate ================ */ bool sdIK_Walker::Evaluate( void ) { trace_t results; // clear joint mods ClearJointMods(); animator->CreateFrame( gameLocal.time, true ); const renderEntity_t* renderEnt = self->GetRenderEntity(); idVec3 normal = -self->GetPhysics()->GetGravityNormal(); const idVec3& modelOrigin = renderEnt->origin; const idMat3& modelAxis = renderEnt->axis; float modelHeight = modelOrigin * normal; isStable = true; // get the joint positions for the feet for ( int i = 0; i < legs.Num(); i++ ) { leg_t& leg = legs[ i ]; idVec3 footOrigin; animator->GetJointTransform( leg.footJoint, gameLocal.time, footOrigin ); leg.current.jointWorldOrigin = modelOrigin + footOrigin * modelAxis; idVec3 start = leg.current.jointWorldOrigin + ( normal * footUpTrace ); idVec3 end = leg.current.jointWorldOrigin - ( normal * footDownTrace ); // gameLocal.clip.Translation( results, start, end, footModel, modelAxis, CONTENTS_SOLID, self ); if ( !gameLocal.clip.TracePoint( CLIP_DEBUG_PARMS results, start, end, MASK_VEHICLESOLID | CONTENTS_MONSTER, self ) ) { isStable = false; } leg.current.floorHeight = results.endpos * normal; idMat3 newAxes; if ( results.fraction != 1.f ) { idVec3 normal = results.c.normal * modelAxis.Transpose(); idVec3 vec3_forward( 1.f, 0.f, 0.f ); newAxes[ 0 ] = vec3_forward - ( normal * ( vec3_forward * normal ) ); newAxes[ 0 ].Normalize(); newAxes[ 2 ] = normal; newAxes[ 1 ] = newAxes[ 2 ].Cross( newAxes[ 0 ] ); } else { newAxes.Identity(); } idQuat newQuat; newQuat.Slerp( leg.lastAnkleAxes.ToQuat(), newAxes.ToQuat(), 0.1f ); leg.lastAnkleAxes = newQuat.ToMat3(); leg.lastAnkleAxes.FixDenormals(); if ( ik_debug.GetBool() && footModel ) { idFixedWinding w; for ( int j = 0; j < footModel->GetTraceModel()->numVerts; j++ ) { w += footModel->GetTraceModel()->verts[j]; } gameRenderWorld->DebugWinding( colorRed, w, results.endpos, results.endAxis ); } } // adjust heights of the ankles float smallestShift = idMath::INFINITY; float largestAnkleHeight = -idMath::INFINITY; for ( int i = 0; i < legs.Num(); i++ ) { leg_t& leg = legs[ i ]; idVec3 ankleOrigin; idVec3 footOrigin; animator->GetJointTransform( leg.ankleJoint, gameLocal.time, ankleOrigin, leg.current.ankleAxis ); animator->GetJointTransform( leg.footJoint, gameLocal.time, footOrigin ); float shift = leg.current.floorHeight - modelHeight + footShift; if ( shift < smallestShift ) { smallestShift = shift; } leg.current.jointWorldOrigin = modelOrigin + ankleOrigin * modelAxis; float height = leg.current.jointWorldOrigin * normal; if ( oldHeightsValid ) { float step = height + shift - leg.oldAnkleHeight; if ( step < 0 ) { shift -= smoothing * step; } else { shift -= downsmoothing * step; } } float newHeight = height + shift; if ( newHeight > largestAnkleHeight ) { largestAnkleHeight = newHeight; } leg.oldAnkleHeight = newHeight; leg.current.jointWorldOrigin += shift * normal; } idVec3 waistOrigin; animator->GetJointTransform( waistJoint, gameLocal.time, waistOrigin ); waistOrigin = modelOrigin + waistOrigin * modelAxis; // adjust position of the waist waistOffset = ( smallestShift + waistShift ) * normal; // if the waist should be at least a certain distance above the floor if ( minWaistFloorDist > 0.0f && waistOffset * normal < 0.0f ) { idVec3 start = waistOrigin; idVec3 end = waistOrigin + waistOffset - normal * minWaistFloorDist; gameLocal.clip.Translation( CLIP_DEBUG_PARMS results, start, end, footModel, modelAxis, CONTENTS_SOLID, self ); float height = ( waistOrigin + waistOffset - results.endpos ) * normal; if ( height < minWaistFloorDist ) { waistOffset += ( minWaistFloorDist - height ) * normal; } } // if the waist should be at least a certain distance above the ankles if ( minWaistAnkleDist > 0.0f ) { float height = ( waistOrigin + waistOffset ) * normal; if ( height - largestAnkleHeight < minWaistAnkleDist ) { waistOffset += ( minWaistAnkleDist - ( height - largestAnkleHeight ) ) * normal; } } if ( oldHeightsValid ) { // smoothly adjust height of waist float newHeight = ( waistOrigin + waistOffset ) * normal; float step = newHeight - oldWaistHeight; waistOffset -= waistSmoothing * step * normal; } // save height of waist for smoothing oldWaistHeight = ( waistOrigin + waistOffset ) * normal; if ( !oldHeightsValid ) { oldHeightsValid = true; return false; } // solve IK for ( int i = 0; i < legs.Num(); i++ ) { leg_t& leg = legs[ i ]; idVec3 hipOrigin; idMat3 axis; // get the position of the hip in world space animator->GetJointTransform( leg.hipJoint, gameLocal.time, hipOrigin, axis ); hipOrigin = modelOrigin + waistOffset + hipOrigin * modelAxis; // DebugAxis( hipOrigin, axis * modelAxis ); idVec3 hipDir = leg.hipForward * axis * modelAxis; idVec3 midOrigin; // get the IK bend direction animator->GetJointTransform( leg.midJoint, gameLocal.time, midOrigin, axis ); midOrigin = modelOrigin + waistOffset + midOrigin * modelAxis; // DebugAxis( midOrigin, axis * modelAxis ); idVec3 midDir = leg.midForward * axis * modelAxis; idVec3 midSideDir = leg.midSide * axis * modelAxis; idVec3 kneeOrigin; // get the IK bend direction animator->GetJointTransform( leg.kneeJoint, gameLocal.time, kneeOrigin, axis ); kneeOrigin = modelOrigin + waistOffset + kneeOrigin * modelAxis; // DebugAxis( kneeOrigin, axis * modelAxis ); idVec3 kneeDir = leg.kneeForward * axis * modelAxis; idVec3 ankleOrigin; animator->GetJointTransform( leg.ankleJoint, gameLocal.time, ankleOrigin, axis ); ankleOrigin = modelOrigin + waistOffset + ankleOrigin * modelAxis; float len1 = leg.upperLegLength; float minLen2 = fabs( leg.midLegLength - leg.lowerLegLength ); float maxLen2 = leg.midLegLength + leg.lowerLegLength; float wantLen = ( hipOrigin - leg.current.jointWorldOrigin ).Length(); float constrainedMaxLen2, constrainedMinLen2; float lenTotal = 0; if( minLen2 < fabs( wantLen - len1 ) ) { minLen2 = fabs( wantLen - len1 ); } float minTotal = FindSmallest( len1, minLen2, maxLen2, constrainedMaxLen2, constrainedMinLen2 ); float maxTotal = len1 + maxLen2; if ( maxTotal < wantLen ) { lenTotal = maxTotal; } else if ( minTotal > wantLen ) { lenTotal = constrainedMaxLen2; } else { float scale = compressionInterpolate.GetCurrentValue( MS2SEC( gameLocal.time ) ); lenTotal = Lerp( minLen2, maxLen2, scale ); } // solve IK and calculate upper knee position SolveTwoBones( hipOrigin, leg.current.jointWorldOrigin, midDir, leg.upperLegLength, lenTotal, midOrigin ); float d1 = ( hipOrigin - midOrigin ).Length(); idVec3 kneeDirTest = kneeDir; kneeDirTest.z += invertedLegDirOffset; kneeDirTest.NormalizeFast(); // solve IK and calculate lower knee position, using -kneeDir, as lower leg is inverted SolveTwoBones( midOrigin, leg.current.jointWorldOrigin, -kneeDirTest, leg.midLegLength, leg.lowerLegLength, kneeOrigin ); float d2 = ( midOrigin - kneeOrigin ).Length(); if ( ik_debug.GetBool() ) { gameRenderWorld->DebugLine( colorCyan, hipOrigin, midOrigin ); gameRenderWorld->DebugLine( colorRed, midOrigin, kneeOrigin ); gameRenderWorld->DebugLine( colorBlue, kneeOrigin, leg.current.jointWorldOrigin ); gameRenderWorld->DebugLine( colorYellow, midOrigin, midOrigin + ( midSideDir * 32 ) ); gameRenderWorld->DebugLine( colorGreen, hipOrigin, hipOrigin + ( hipDir * 32 ) ); gameRenderWorld->DebugLine( colorGreen, midOrigin, midOrigin + ( midDir * 32 ) ); gameRenderWorld->DebugLine( colorGreen, kneeOrigin, kneeOrigin + ( -kneeDirTest * 32 ) ); } // get the axis for the hip joint GetBoneAxis( hipOrigin, midOrigin, hipDir, axis ); leg.current.hipAxis = leg.upperLegToHipJoint * ( axis * modelAxis.Transpose() ); // get the axis for the knee joint GetBoneAxis( midOrigin, kneeOrigin, midSideDir, axis ); leg.current.midAxis = leg.midToUpperLegJoint * ( axis * modelAxis.Transpose() ); // get the axis for the knee joint GetBoneAxis( kneeOrigin, leg.current.jointWorldOrigin, kneeDir, axis ); leg.current.kneeAxis = leg.lowerLegToKneeJoint * ( axis * modelAxis.Transpose() ); } // set the joint mods animator->SetJointPos( waistJoint, JOINTMOD_WORLD_OVERRIDE, ( waistOrigin + waistOffset - modelOrigin ) * modelAxis.Transpose() ); for ( int i = 0; i < legs.Num(); i++ ) { leg_t& leg = legs[ i ]; animator->SetJointAxis( leg.hipJoint, JOINTMOD_WORLD_OVERRIDE, leg.current.hipAxis ); animator->SetJointAxis( leg.midJoint, JOINTMOD_WORLD_OVERRIDE, leg.current.midAxis ); animator->SetJointAxis( leg.kneeJoint, JOINTMOD_WORLD_OVERRIDE, leg.current.kneeAxis ); animator->SetJointAxis( leg.ankleJoint, JOINTMOD_WORLD_OVERRIDE, leg.current.ankleAxis * leg.lastAnkleAxes ); } return true; }