Skip to content

Commit 9866908

Browse files
committed
Required changes for Inverse Kinematics
Added a * operator compute from to -> adds safeties around shortestArc conjugate -> reverses the xyz of the quaternion IK Solver commit Added: IKChain struct to tsshape commands to tsshapeconstruct to create and setup ikchains ik solvers -> ccd and fabrik, these are in their own file tsIKSolver TODO: there needs to be some tooling added to the shape editor for this
1 parent 42e8687 commit 9866908

10 files changed

Lines changed: 1048 additions & 1 deletion

File tree

Engine/source/math/mQuat.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,3 +342,40 @@ QuatF & QuatF::shortestArc( const VectorF &a, const VectorF &b )
342342
return *this;
343343
}
344344

345+
QuatF& QuatF::computeRotationFromTo(const VectorF& from, const VectorF& to)
346+
{
347+
VectorF f = from;
348+
VectorF t = to;
349+
350+
f.normalizeSafe();
351+
t.normalizeSafe();
352+
353+
if (f.isZero() || t.isZero())
354+
{
355+
return identity();
356+
}
357+
358+
F32 dot = mClampF(mDot(f, t), -1.0f, 1.0f);
359+
360+
// Parallel = no rotation.
361+
if (dot > 0.9999f)
362+
{
363+
return identity();
364+
}
365+
366+
// Opposite = pick perpendicular.
367+
if (dot < -0.9999f)
368+
{
369+
VectorF axis;
370+
if (mFabs(f.x) < mFabs(f.z))
371+
axis.set(0, -f.z, f.y);
372+
else
373+
axis.set(-f.y, f.x, 0);
374+
375+
axis.normalizeSafe();
376+
return set(axis, M_PI_F); // 180 degrees
377+
}
378+
379+
return shortestArc(f, t);
380+
}
381+

Engine/source/math/mQuat.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,12 @@ class QuatF
7070
QuatF& operator /=( F32 a );
7171

7272
QuatF operator-( const QuatF &c ) const;
73+
QuatF operator*(const QuatF& rhs) const;
7374
QuatF operator*( F32 a ) const;
7475

7576
QuatF& square();
7677
QuatF& neg();
78+
QuatF& conjugate();
7779
F32 dot( const QuatF &q ) const;
7880

7981
MatrixF* setMatrix( MatrixF * mat ) const;
@@ -91,6 +93,7 @@ class QuatF
9193

9294
// Vectors passed in must be normalized
9395
QuatF& shortestArc( const VectorF &normalizedA, const VectorF &normalizedB );
96+
QuatF& computeRotationFromTo(const VectorF& from, const VectorF& to);
9497
};
9598

9699
// a couple simple utility methods
@@ -208,6 +211,18 @@ inline QuatF QuatF::operator -( const QuatF &c ) const
208211
w - c.w );
209212
}
210213

214+
inline QuatF QuatF::operator*(const QuatF& rhs) const
215+
{
216+
QuatF out;
217+
218+
out.w = w * rhs.w - x * rhs.x - y * rhs.y - z * rhs.z;
219+
out.x = w * rhs.x + x * rhs.w + y * rhs.z - z * rhs.y;
220+
out.y = w * rhs.y - x * rhs.z + y * rhs.w + z * rhs.x;
221+
out.z = w * rhs.z + x * rhs.y - y * rhs.x + z * rhs.w;
222+
223+
return out;
224+
}
225+
211226
inline QuatF QuatF::operator *( F32 a ) const
212227
{
213228
return QuatF( x * a,
@@ -225,6 +240,14 @@ inline QuatF& QuatF::neg()
225240
return *this;
226241
}
227242

243+
inline QuatF& QuatF::conjugate()
244+
{
245+
x = -x;
246+
y = -y;
247+
z = -z;
248+
return *this;
249+
}
250+
228251
inline F32 QuatF::dot( const QuatF &q ) const
229252
{
230253
return mClampF(w*q.w + x*q.x + y*q.y + z*q.z, -1.0f, 1.0f);

Engine/source/ts/tsAnimate.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,8 +866,21 @@ void TSShapeInstance::animate(S32 dl)
866866

867867
// animate nodes?
868868
if (dirtyFlags & TransformDirty)
869+
{
869870
animateNodes(ss);
870871

872+
//---------------------------------------
873+
// TODO: Implement different ik methods
874+
// add limits to ik chain nodes
875+
// cache bone lengths.
876+
//---------------------------------------
877+
for (U32 i = 0; i < mShape->ikChains.size(); i++)
878+
{
879+
if (mShape->ikChains[i].enabled)
880+
solveCCD(mShape->ikChains[i]);
881+
}
882+
}
883+
871884
// animate objects?
872885
if (dirtyFlags & VisDirty)
873886
animateVisibility(ss);

0 commit comments

Comments
 (0)