-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathActorIKConstraint.cs
445 lines (392 loc) · 11.3 KB
/
ActorIKConstraint.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
using System;
using System.IO;
using System.Collections.Generic;
using Nima.Math2D;
namespace Nima
{
public class InfluencedBone
{
public ushort m_BoneIdx;
public ActorBone m_Bone;
}
public class ActorIKConstraint : ActorTargetedConstraint
{
const float PI2 = (float)Math.PI*2;
const float PI = (float)Math.PI;
private class BoneChain
{
public int m_Index;
public ActorBone m_Bone;
public float m_Angle;
public bool m_Included;
public TransformComponents m_TransformComponents;
public Mat2D m_ParentWorldInverse;
}
private bool m_InvertDirection;
private InfluencedBone[] m_InfluencedBones;
private BoneChain[] m_FKChain;
private IList<BoneChain> m_BoneData;
public ActorIKConstraint()
{
}
// Support old system.
public ActorIKConstraint(ActorIKTarget target)
{
InfluencedBone[] bones = target.InfluencedBones;
m_Actor = target.Actor;
m_InfluencedBones = target.InfluencedBones;
m_TargetIdx = target.Idx;
m_ParentIdx = bones[bones.Length-1].m_BoneIdx;
m_InvertDirection = target.InvertDirection;
m_Strength = target.m_Strength;
m_IsEnabled = true;
}
public bool InvertDirection
{
get
{
return m_InvertDirection;
}
set
{
if(value == m_InvertDirection)
{
return;
}
m_InvertDirection = value;
MarkDirty();
}
}
public override void ResolveComponentIndices(ActorComponent[] components)
{
base.ResolveComponentIndices(components);
if(m_Parent != null)
{
// This works because nodes are exported in hierarchy order, so we are assured constraints get added in order as we resolve indices.
m_Parent.AddConstraint(this);
}
if(m_InfluencedBones != null)
{
foreach(InfluencedBone influenced in m_InfluencedBones)
{
influenced.m_Bone = components[influenced.m_BoneIdx] as ActorBone;
}
}
}
public override void CompleteResolve()
{
base.CompleteResolve();
if(m_InfluencedBones == null || m_InfluencedBones.Length == 0)
{
return;
}
// Initialize solver.
ActorBone start = m_InfluencedBones[0].m_Bone;
ActorBone end = m_InfluencedBones[m_InfluencedBones.Length-1].m_Bone;
int count = 0;
while(end != null && end != start.Parent)
{
count++;
end = end.Parent as ActorBone;
}
bool allIn = count < 3;
end = m_InfluencedBones[m_InfluencedBones.Length-1].m_Bone;
m_FKChain = new BoneChain[count];
int idx = count-1;
while(end != null && end != start.Parent)
{
BoneChain bc = new BoneChain();
bc.m_Bone = end;
bc.m_Angle = 0.0f;
bc.m_Included = allIn;
bc.m_TransformComponents = new TransformComponents();
bc.m_ParentWorldInverse = new Mat2D();
bc.m_Index = idx;
m_FKChain[idx--] = bc;
end = end.Parent as ActorBone;
}
// Make sure bones are good.
m_BoneData = new List<BoneChain>();
foreach(InfluencedBone bone in m_InfluencedBones)
{
BoneChain item = Array.Find(m_FKChain, chainItem => chainItem.m_Bone == bone.m_Bone);
if(item == null)
{
Console.WriteLine("Bone not in chain: " + bone.m_Bone.Name);
continue;
}
m_BoneData.Add(item);
}
if(!allIn)
{
// Influenced bones are in the IK chain.
for(int i = 0; i < m_BoneData.Count-1; i++)
{
BoneChain item = m_BoneData[i];
item.m_Included = true;
m_FKChain[item.m_Index+1].m_Included = true;
}
}
// Finally mark dependencies.
foreach(InfluencedBone bone in m_InfluencedBones)
{
// Don't mark dependency on parent as ActorComponent already does this.
if(bone.m_Bone == m_Parent)
{
continue;
}
m_Actor.AddDependency(this, bone.m_Bone);
}
if(m_Target != null)
{
m_Actor.AddDependency(this, m_Target);
}
// All the first level children of the influenced bones should depend on the final bone.
BoneChain tip = m_FKChain[m_FKChain.Length-1];
foreach(BoneChain fk in m_FKChain)
{
if(fk == tip)
{
continue;
}
ActorBone bone = fk.m_Bone;
foreach(ActorNode node in bone.Children)
{
BoneChain item = Array.Find(m_FKChain, chainItem => chainItem.m_Bone == node);
if(item != null)
{
// node is in the FK chain.
continue;
}
m_Actor.AddDependency(node, tip.m_Bone);
}
}
}
public static ActorIKConstraint Read(Actor actor, BinaryReader reader, ActorIKConstraint component = null)
{
if(component == null)
{
component = new ActorIKConstraint();
}
ActorTargetedConstraint.Read(actor, reader, component);
component.m_InvertDirection = reader.ReadByte() == 1;
int numInfluencedBones = (int)reader.ReadByte();
if(numInfluencedBones > 0)
{
component.m_InfluencedBones = new InfluencedBone[numInfluencedBones];
for(int i = 0; i < numInfluencedBones; i++)
{
InfluencedBone ib = new InfluencedBone();
ib.m_BoneIdx = reader.ReadUInt16();
component.m_InfluencedBones[i] = ib;
}
}
return component;
}
public override void Constrain(ActorNode node)
{
ActorNode target = m_Target as ActorNode;
if(target == null)
{
return;
}
Vec2D worldTargetTranslation = new Vec2D();
target.GetWorldTranslation(worldTargetTranslation);
if(m_InfluencedBones.Length == 0)
{
return;
}
// Decompose the chain.
foreach(BoneChain item in m_FKChain)
{
ActorBone bone = item.m_Bone;
Mat2D parentWorld = bone.Parent.WorldTransform;
Mat2D.Invert(item.m_ParentWorldInverse, parentWorld);
Mat2D.Multiply(bone.Transform, item.m_ParentWorldInverse, bone.WorldTransform);
Mat2D.Decompose(bone.Transform, item.m_TransformComponents);
}
int count = m_BoneData.Count;
if(count == 1)
{
Solve1(m_BoneData[0], worldTargetTranslation);
}
else if(count == 2)
{
Solve2(m_BoneData[0], m_BoneData[1], worldTargetTranslation);
}
else
{
BoneChain tip = m_BoneData[count-1];
for(int i = 0; i < count-1; i++)
{
BoneChain item = m_BoneData[i];
Solve2(item, tip, worldTargetTranslation);
for(int j = item.m_Index+1; j < m_FKChain.Length-1; j++)
{
BoneChain fk = m_FKChain[j];
Mat2D.Invert(fk.m_ParentWorldInverse, fk.m_Bone.Parent.WorldTransform);
}
}
}
// At the end, mix the FK angle with the IK angle by strength
if(m_Strength != 1.0)
{
foreach(BoneChain fk in m_FKChain)
{
if(!fk.m_Included)
{
ActorBone bone = fk.m_Bone;
Mat2D.Multiply(bone.WorldTransform, bone.Parent.WorldTransform, bone.Transform);
continue;
}
float fromAngle = fk.m_TransformComponents.Rotation%PI2;
float toAngle = fk.m_Angle%PI2;
float diff = toAngle - fromAngle;
if(diff > PI)
{
diff -= PI2;
}
else if(diff < -PI)
{
diff += PI2;
}
float angle = fromAngle + diff * m_Strength;
ConstrainRotation(fk, angle);
}
}
}
void ConstrainRotation(BoneChain fk, float rotation)
{
ActorBone bone = fk.m_Bone;
Mat2D parentWorld = bone.Parent.WorldTransform;
Mat2D transform = bone.Transform;
TransformComponents c = fk.m_TransformComponents;
if(rotation == 0.0f)
{
Mat2D.Identity(transform);
}
else
{
Mat2D.FromRotation(transform, rotation);
}
// Translate
transform[4] = c.X;
transform[5] = c.Y;
// Scale
float scaleX = c.ScaleX;
float scaleY = c.ScaleY;
transform[0] *= scaleX;
transform[1] *= scaleX;
transform[2] *= scaleY;
transform[3] *= scaleY;
// Skew
float skew = c.Skew;
if(skew != 0.0)
{
transform[2] = transform[0] * skew + transform[2];
transform[3] = transform[1] * skew + transform[3];
}
Mat2D.Multiply(bone.WorldTransform, parentWorld, transform);
}
void Solve1(BoneChain fk1, Vec2D worldTargetTranslation)
{
Mat2D iworld = fk1.m_ParentWorldInverse;
var pA = new Vec2D();
fk1.m_Bone.GetWorldTranslation(pA);
var pBT = new Vec2D(worldTargetTranslation);
// To target in worldspace
Vec2D toTarget = Vec2D.Subtract(new Vec2D(), pBT, pA);
// Note this is directional, hence not transformMat2d
Vec2D toTargetLocal = Vec2D.TransformMat2(new Vec2D(), toTarget, iworld);
float r = (float)Math.Atan2(toTargetLocal[1], toTargetLocal[0]);
ConstrainRotation(fk1, r);
fk1.m_Angle = r;
}
void Solve2(BoneChain fk1, BoneChain fk2, Vec2D worldTargetTranslation)
{
ActorBone b1 = fk1.m_Bone;
ActorBone b2 = fk2.m_Bone;
BoneChain firstChild = m_FKChain[fk1.m_Index+1];
Mat2D iworld = fk1.m_ParentWorldInverse;
Vec2D pA = b1.GetWorldTranslation(new Vec2D());
Vec2D pC = firstChild.m_Bone.GetWorldTranslation(new Vec2D());
Vec2D pB = b2.GetTipWorldTranslation(new Vec2D());;
Vec2D pBT = new Vec2D(worldTargetTranslation);
pA = Vec2D.TransformMat2D(pA, pA, iworld);
pC = Vec2D.TransformMat2D(pC, pC, iworld);
pB = Vec2D.TransformMat2D(pB, pB, iworld);
pBT = Vec2D.TransformMat2D(pBT, pBT, iworld);
// http://mathworld.wolfram.com/LawofCosines.html
Vec2D av = Vec2D.Subtract(new Vec2D(), pB, pC);
float a = Vec2D.Length(av);
Vec2D bv = Vec2D.Subtract(new Vec2D(), pC, pA);
float b = Vec2D.Length(bv);
Vec2D cv = Vec2D.Subtract(new Vec2D(), pBT, pA);
float c = Vec2D.Length(cv);
float A = (float)Math.Acos(Math.Max(-1,Math.Min(1,(-a*a+b*b+c*c)/(2*b*c))));
float C = (float)Math.Acos(Math.Max(-1, Math.Min(1,(a*a+b*b-c*c)/(2*a*b))));
float r1, r2;
if(b2.Parent != b1)
{
BoneChain secondChild = m_FKChain[fk1.m_Index+2];
Mat2D secondChildWorldInverse = secondChild.m_ParentWorldInverse;
pC = firstChild.m_Bone.GetWorldTranslation(new Vec2D());
pB = b2.GetTipWorldTranslation(new Vec2D());
Vec2D avec = Vec2D.Subtract(new Vec2D(), pB, pC);
Vec2D avLocal = Vec2D.TransformMat2(new Vec2D(), avec, secondChildWorldInverse);
float angleCorrection = (float)-Math.Atan2(avLocal[1], avLocal[0]);
if(m_InvertDirection)
{
r1 = (float)Math.Atan2(cv[1],cv[0]) - A;
r2 = -C+PI + angleCorrection;
}
else
{
r1 = A + (float)Math.Atan2(cv[1],cv[0]);
r2 = C-PI + angleCorrection;
}
}
else if(m_InvertDirection)
{
r1 = (float)Math.Atan2(cv[1],cv[0]) - A;
r2 = -C+PI;
}
else
{
r1 = A + (float)Math.Atan2(cv[1],cv[0]);
r2 = C-PI;
}
ConstrainRotation(fk1, r1);
ConstrainRotation(firstChild, r2);
if(firstChild != fk2)
{
ActorBone bone = fk2.m_Bone;
Mat2D.Multiply(bone.WorldTransform, bone.Parent.WorldTransform, bone.Transform);
}
// Simple storage, need this for interpolation.
fk1.m_Angle = r1;
firstChild.m_Angle = r2;
}
public override ActorComponent MakeInstance(Actor resetActor)
{
ActorIKConstraint instance = new ActorIKConstraint();
instance.Copy(this, resetActor);
return instance;
}
public void Copy(ActorIKConstraint node, Actor resetActor)
{
base.Copy(node, resetActor);
m_InvertDirection = node.m_InvertDirection;
if(node.m_InfluencedBones != null)
{
m_InfluencedBones = new InfluencedBone[node.m_InfluencedBones.Length];
for(int i = 0; i < m_InfluencedBones.Length; i++)
{
InfluencedBone ib = new InfluencedBone();
ib.m_BoneIdx = node.m_InfluencedBones[i].m_BoneIdx;
m_InfluencedBones[i] = ib;
}
}
}
}
}