# [SOLVED] Dual Quaternion Skinning failing on blend

This topic is 2012 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi,

I've implemented dual quaternion skinning and for some reason it just doesn't work. I'm hoping somebody will have ideas on further avenues for trying to fix this. To start with, I'm calculating all my bone transforms with quaternion rotations and position vectors. I've verified this works by converting the final result to a matrix and doing basic linear skinning.

My quat/vec to dual quaternion conversion function is:

[CODE]
void iUQTtoUDQ(math::quat* dual, const math::quat& q, const math::vec3& p)
{
// Straight copy of rotation
dual[0].x = q.x;
dual[0].y = q.y;
dual[0].z = q.z;
dual[0].w = q.w;

// Multiply rotation by pure quaternion position and scale by 0.5 (dual scalar)

dual[1].x = 0.5f * ( p.x * q.w + p.y * q.z - p.z * q.y );
dual[1].y = 0.5f * (-p.x * q.z + p.y * q.w + p.z * q.x );
dual[1].z = 0.5f * ( p.x * q.y - p.y * q.x + p.z * q.w );
dual[1].w = 0.5f * (-p.x * q.x - p.y * q.y - p.z * q.z );
}
[/CODE]

This matches my quaternion multiplication function with w set to zero for converting a vector into a pure quaternion.

This is my skinning function, with debug code left in-tact:

[CODE]
float3 SkinPositionDualQuat(float4 pos, uint4 bone_indices, float4 bone_weights)
{
float2x4 dq0 = GetBoneDualQuat(bone_indices.x);
float2x4 dq1 = GetBoneDualQuat(bone_indices.y);
float2x4 dq2 = GetBoneDualQuat(bone_indices.z);
float2x4 dq3 = GetBoneDualQuat(bone_indices.w);

// DEBUG: Here I'm rescaling weights to test weighting by 1,2,3 and 4 bones at a time
// Weights are sorted, largest influence first

//bone_weights.y = 0;
bone_weights.z = 0;
bone_weights.w = 0;
float t = dot(bone_weights, 1);
bone_weights /= t;

// Antipodality checks

if (dot(dq0[0], dq1[0]) < 0.0) bone_weights.y *= -1.0;
if (dot(dq0[0], dq2[0]) < 0.0) bone_weights.z *= -1.0;
if (dot(dq0[0], dq3[0]) < 0.0) bone_weights.w *= -1.0;

// Weight dual quaternions
float2x4 result =
bone_weights.x * dq0 +
bone_weights.y * dq1 +
bone_weights.z * dq2 +
bone_weights.w * dq3;

// Normalise the result and transform

float normDQ = length(result[0]);
result /= normDQ;
return DQTransformPoint( result[0], result[1], pos.xyz );
}
[/CODE]

Now for the interesting bit of code:

[CODE]
float3 DQTransformPoint( float4 realDQ, float4 dualDQ, float3 pt )
{
return pt + 2 * cross(realDQ.xyz, cross(realDQ.xyz, pt) - realDQ.w * pt)
+ 2 * (realDQ.w * dualDQ.xyz - dualDQ.w * realDQ.xyz + cross(realDQ.xyz, dualDQ.xyz));
}
[/CODE]

This matches the original source from Kavan, except that I've swapped the last sign on the first line (-realDQ.w*pt used to be +realDW.w*pt). If I don't do this, it doesn't work.

So to the problem: If I weight by one bone only, this all works perfectly. However, when my character bends their arm up to their shoulder, the entire arm skews like crazy.

I can't believe that the linear interpolation of dual quats above is the source of the problem (unless dual quats are a modern version of the emperors new clothes). I'm guessing it's something to do with the fact that I've changed DQTransformPoint to work in my engine but haven't changed the encoding. Something is amiss!

Can anybody think of anywhere I can go with this?

Thanks!

##### Share on other sites
So I've gone back to basics and reduced my input and output to un-optimised representations of the equations in this brilliant paper: [url="http://wscg.zcu.cz/wscg2012/cd-rom/short/A29-full.pdf"]http://wscg.zcu.cz/w...rt/A29-full.pdf[/url] (A beginner's guide to dual quaternions).

The DQ generation code is simply:

[CODE]
void iUQTtoUDQ(math::quat* dual, const math::quat& q, const math::vec3& p)
{
// Qr = r
dual[0] = q;
// Qd = 0.5(0,t)r
math::quat qp = { p.x, p.y, p.z, 0 };
dual[1] = qScale(qMultiply(qp, q), 0.5f);
}

[/CODE]

And the shader transform code is:

[CODE]
float3 Transform(float4 q, float3 p)
{
return Multiply(Conjugate(q), Multiply(float4(p, 1), q)).xyz;
}
float3 DQTransformPoint(float4 Qr, float4 Qd, float3 pt)
{
// r = Qr
// t = 2QdQr*
float3 p = Transform(Qr, pt);
float4 t = 2 * Multiply(Qd, Conjugate(Qr));
return p + t.xyz;
}

[/CODE]

This produced the same artefacts. By swapping the Multiply parameters used to generate [b]dual[1][/b] and also for generating [b]t [/b]everything is now working as expected. My multiplies concatenate right-to-left and hence didn't match any of the available source code out there for me to compare with. I guess that will teach me to test my stuff with a basic idle animation.

Now to optimise...

##### Share on other sites
Oops! Typo: [b]float4(p,1)[/b] should be [b]float4(p,0)[/b]

##### Share on other sites
For anybody who might have similar problems in the future, this is where I started on the optimisations:

[CODE]
float4 MultiplyPure(float3 a, float4 b)
{
// lhs is a pure quaternion, rhs is a quaternion, so a.w=0
return float4(b.w * a + cross(a, b.xyz), -dot(a, b.xyz));
}
float3 MultiplyConjugate3(float4 a, float4 b)
{
// perform conjugate on lhs before multiplying, discard result.w
return a.w * b.xyz - b.w * a.xyz + cross(-a.xyz, b.xyz);
}
float3 DQTransformPoint(float4 Qr, float4 Qd, float3 pt)
{
// Vector transform pt by rotation Qr (pvp')
float3 p = MultiplyConjugate3(Qr, MultiplyPure(pt, Qr));
// t = 2QdQr*
float3 t = 2 * MultiplyConjugate3(Qr, Qd);
return p + t;
}
[/CODE]

which reduces to:

[CODE]
float3 DQTransformPoint(float4 Qr, float4 Qd, float3 pt)
{
float3 p = pt + 2 * cross(-Qr.xyz, Qr.w * pt + cross(pt, Qr.xyz));
float3 t = 2 * (Qr.w * Qd.xyz - Qd.w * Qr.xyz + cross(-Qr.xyz, Qd.xyz));
return p + t;
}
[/CODE]

This is a subtle re-arrangement of the source paper's results which could have been achieved through guesswork. It was more fun this way [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]