-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentity_animation_control.cpp
798 lines (723 loc) · 28 KB
/
entity_animation_control.cpp
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
#include "entity_animation_control.h"
#include "debug_drawing.h"
void DrawFrameInfo(mm_frame_info AnimGoal, mat4 CoordinateFrame,
mm_info_debug_settings DebugSettings, vec3 BoneColor, vec3 VelocityColor,
vec3 TrajectoryColor, vec3 DirectionColor);
void DrawTrajectory(mat4 CoordinateFrame, const trajectory* Trajectory, vec3 PastColor,
vec3 PresentColor, vec3 FutureColor);
void
SetDefaultMMControllerFileds(mm_aos_entity_data* MMEntityData)
{
*MMEntityData->MMControllerRID = {};
*MMEntityData->BlendStack = {};
*MMEntityData->EntityIndex = -1;
*MMEntityData->FollowSpline = false;
*MMEntityData->SplineState = { };
{
MMEntityData->SplineState->SplineIndex = -1;
MMEntityData->SplineState->NextWaypointIndex = 0;
MMEntityData->SplineState->Loop = true;
MMEntityData->SplineState->MovingInPositive = true;
}
InitTrajectory(MMEntityData->Trajectory);
*MMEntityData->InputController = {};
{
MMEntityData->InputController->MaxSpeed = 1.0f;
MMEntityData->InputController->PositionBias = 0.08f;
MMEntityData->InputController->DirectionBias = 2;
MMEntityData->InputController->UseStrafing = false;
MMEntityData->InputController->UseSmoothGoal = true;
}
*MMEntityData->Skeleton = NULL;
*MMEntityData->MMController = NULL;
*MMEntityData->LastMatchedGoal = {};
*MMEntityData->MirroredAnimGoal = {};
*MMEntityData->AnimGoal = {};
*MMEntityData->LastMatchedTransform = IdentityTransform();
*MMEntityData->AnimPlayerTime = 0;
}
void
CopyMMEntityData(int32_t DestIndex, int32_t SourceIndex, mm_entity_data* MMEntityData)
{
mm_aos_entity_data Dest = GetAOSMMDataAtIndex(DestIndex, MMEntityData);
mm_aos_entity_data Source = GetAOSMMDataAtIndex(SourceIndex, MMEntityData);
CopyAOSMMEntityData(&Dest, &Source);
}
void
RemoveMMControllerDataAtIndex(entity* Entities, int32_t MMControllerIndex,
Resource::resource_manager* Resources, mm_entity_data* MMEntityData)
{
assert(0 <= MMControllerIndex && MMControllerIndex < MMEntityData->Count);
mm_aos_entity_data RemovedController = GetAOSMMDataAtIndex(MMControllerIndex, MMEntityData);
if(RemovedController.MMControllerRID->Value > 0)
{
Resources->MMControllers.RemoveReference(*RemovedController.MMControllerRID);
}
{
Anim::animation_player* AnimPlayer =
Entities[*RemovedController.EntityIndex].AnimPlayer;
assert(AnimPlayer);
AnimPlayer->BlendFunc = NULL;
for(int i = 0; i < ANIM_PLAYER_MAX_ANIM_COUNT; i++)
{
assert(AnimPlayer->AnimationIDs[i].Value == 0);
AnimPlayer->Animations[i] = NULL;
AnimPlayer->States[i] = {};
}
AnimPlayer->AnimStateCount = 0;
}
mm_aos_entity_data LastController = GetAOSMMDataAtIndex(MMEntityData->Count - 1, MMEntityData);
CopyAOSMMEntityData(&RemovedController, &LastController);
MMEntityData->Count--;
}
int32_t
GetEntityMMDataIndex(int32_t EntityIndex, const mm_entity_data* MMEntityData)
{
int32_t MMDataIndex = -1;
for(int i = 0; i < MMEntityData->Count; i++)
{
if(MMEntityData->EntityIndices[i] == EntityIndex)
{
MMDataIndex = i;
break;
}
}
return MMDataIndex;
}
void
ClearAnimationData(blend_stack* BlendStacks, int32_t* EntityIndices, int32_t Count,
entity* Entities, int32_t DebugEntityCount)
{
for(int i = 0; i < Count; i++)
{
int EntityIndex = EntityIndices[i];
assert(0 <= EntityIndex && EntityIndex < DebugEntityCount);
BlendStacks[i].Clear();
// Clear out anim plaer
Anim::animation_player* AnimPlayer = Entities[EntityIndex].AnimPlayer;
AnimPlayer->BlendFunc = NULL;
for(int a = 0; a < AnimPlayer->AnimStateCount; a++)
{
assert(AnimPlayer->AnimationIDs[a].Value == 0);
AnimPlayer->Animations[a] = NULL;
AnimPlayer->States[a] = {};
}
AnimPlayer->AnimStateCount = 0;
}
}
bool
IsSplineAtIndexValid(int32_t Index, const spline_system* SplineSystem)
{
if(0 <= Index && Index < SplineSystem->Splines.Count)
{
if(SplineSystem->Splines[Index].Waypoints.Count > 0)
{
return true;
}
}
return false;
}
bool
ShouldSwap(int LeftIndex, int RightIndex, const mm_entity_data* MMData,
const spline_system* SplineSystem)
{
if(MMData->MMControllerRIDs[LeftIndex].Value > 0) // Left has controller
{
if(MMData->MMControllerRIDs[RightIndex].Value > 0) // Right has controller
{
if(!MMData->FollowSpline[LeftIndex]) // left is input controlled
{
return false;
}
else // Left is spline controlled
{
if(!MMData->FollowSpline[RightIndex]) // Right is input controlled
{
return true;
}
else // Both are spline controlled
{
if(IsSplineAtIndexValid(MMData->SplineStates[LeftIndex].SplineIndex,
SplineSystem)) // Left has valid spline
{
return false;
}
else // Left has invalid spline
{
if(IsSplineAtIndexValid(MMData->SplineStates[RightIndex].SplineIndex,
SplineSystem)) // Right has valid spline
{
return true;
}
else
{
return false;
}
}
}
}
}
return false;
}
return true;
}
void
SortMMEntityDataByUsage(int32_t* OutInputControlledCount, int32_t* OutTrajectoryControlledStart,
int32_t* OutTrajectoryControlledCount, mm_entity_data* MMEntityData,
const spline_system* Splines)
{
for(int i = 0; i < MMEntityData->Count - 1; i++)
{
int SmallestIndex = i;
for(int j = i + 1; j < MMEntityData->Count; j++)
{
if(ShouldSwap(SmallestIndex, j, MMEntityData, Splines))
{
SmallestIndex = j;
}
}
if(SmallestIndex != i)
{
mm_aos_entity_data A = GetAOSMMDataAtIndex(i, MMEntityData);
mm_aos_entity_data B = GetAOSMMDataAtIndex(SmallestIndex, MMEntityData);
SwapMMEntityData(&A, &B);
}
}
*OutInputControlledCount = 0;
*OutTrajectoryControlledCount = 0;
for(int i = 0; i < MMEntityData->Count; i++)
{
if(MMEntityData->MMControllerRIDs[i].Value > 0)
{
if(MMEntityData->FollowSpline[i])
{
if(IsSplineAtIndexValid(MMEntityData->SplineStates[i].SplineIndex, Splines))
{
(*OutTrajectoryControlledCount)++;
}
}
else
{
(*OutInputControlledCount)++;
}
}
}
*OutTrajectoryControlledStart = *OutInputControlledCount;
}
void
FetchMMControllerDataPointers(Resource::resource_manager* Resources,
mm_controller_data** OutMMControllers, rid* MMControllerRIDs,
int32_t Count)
{
for(int i = 0; i < Count; i++)
{
OutMMControllers[i] = Resources->GetMMController(MMControllerRIDs[i]);
assert(OutMMControllers[i]);
}
}
void
FetchSkeletonPointers(Anim::skeleton** OutSkeletons, const int32_t* EntityIndices,
const entity* Entities, int32_t Count)
{
for(int i = 0; i < Count; i++)
{
OutSkeletons[i] = Entities[EntityIndices[i]].AnimPlayer->Skeleton;
assert(OutSkeletons[i]);
}
}
void
FetchAnimationPointers(Resource::resource_manager* Resources, mm_controller_data** MMControllers,
blend_stack* BlendStacks, int32_t Count)
{
for(int i = 0; i < Count; i++)
{
MMControllers[i]->Animations.HardClear();
for(int j = 0; j < MMControllers[i]->Params.AnimRIDs.Count; j++)
{
Anim::animation* Anim = Resources->GetAnimation(MMControllers[i]->Params.AnimRIDs[j]);
assert(Anim);
MMControllers[i]->Animations.Push(Anim);
}
for(int j = 0; j < BlendStacks[i].Count; j++)
{
BlendStacks[i][j].Animation = MMControllers[i]->Animations[BlendStacks[i][j].IndexInSet];
}
}
}
void
PlayAnimsIfBlendStacksAreEmpty(blend_stack* BSs, float* GlobalTimes,
const mm_controller_data* const* MMControllers, int32_t Count)
{
for(int i = 0; i < Count; i++)
{
if(BSs[i].Empty())
{
const int IndexInSet = 0;
const float LocalStartTime = 0.0f;
const float BlendInTime = 0.0f;
const bool Mirror = false;
GlobalTimes[i] = 0.0f;
PlayAnimation(&BSs[i], MMControllers[i]->Animations[IndexInSet], IndexInSet, LocalStartTime,
GlobalTimes[i], BlendInTime, Mirror);
}
}
}
void
DrawGoalFrameInfos(const mm_frame_info* GoalInfos, const blend_stack* BlendStacks,
const transform* LastMatchTransforms, int32_t Count,
const mm_info_debug_settings* MMInfoDebug, vec3 BoneColor, vec3 TrajectoryColor,
vec3 DirectionColor)
{
for(int i = 0; i < Count; i++)
{
DrawFrameInfo(GoalInfos[i], TransformToMat4(LastMatchTransforms[i]), *MMInfoDebug, BoneColor,
BoneColor, TrajectoryColor, DirectionColor);
}
}
void
DrawGoalFrameInfos(const mm_frame_info* GoalInfos, const int32_t* EntityIndices, int32_t Count,
const entity* Entities, const mm_info_debug_settings* MMInfoDebug,
vec3 BoneColor, vec3 TrajectoryColor, vec3 DirectionColor)
{
for(int i = 0; i < Count; i++)
{
DrawFrameInfo(GoalInfos[i], TransformToMat4(Entities[EntityIndices[i]].Transform), *MMInfoDebug,
BoneColor, BoneColor, TrajectoryColor, DirectionColor);
}
}
void
DrawControlTrajectories(const trajectory* Trajectories, const mm_input_controller* InputControllers,
const int32_t* EntityIndices, int32_t Count, const entity* Entities)
{
for(int i = 0; i < Count; i++)
{
if(InputControllers[i].UseSmoothGoal)
{
DrawTrajectory(TransformToMat4(Entities[EntityIndices[i]].Transform), &Trajectories[i],
{ 0, 1, 0 }, { 0, 0, 1 }, { 1, 1, 0 });
}
}
}
void
GenerateGoalsFromInput(mm_frame_info* OutGoals, mm_frame_info* OutMirroredGoals,
trajectory* Trajectories, Memory::stack_allocator* TempAlloc,
const blend_stack* BlendStacks, const float* GlobalTimes,
const Anim::skeleton* const* Skeletons,
const mm_controller_data* const* MMControllers,
const mm_input_controller* InputControllers, const int32_t* EntityIndices,
int32_t Count, const entity* Entities, const game_input* Input,
const entity_goal_input* InputOverrides, int32_t InputOverrideCount,
vec3 CameraForward, bool AllowWASDControls)
{
// TODO(Lukas) Add joystick option here
vec3 Dir = {};
vec3 ViewForward = Math::Normalized(vec3{ CameraForward.X, 0, CameraForward.Z });
{
vec3 YAxis = { 0, 1, 0 };
vec3 ViewRight = Math::Cross(ViewForward, YAxis);
if(Input->ArrowUp.EndedDown || (Input->w.EndedDown && AllowWASDControls))
{
Dir += ViewForward;
}
if(Input->ArrowDown.EndedDown || (Input->s.EndedDown && AllowWASDControls))
{
Dir -= ViewForward;
}
if(Input->ArrowRight.EndedDown || (Input->d.EndedDown && AllowWASDControls))
{
Dir += ViewRight;
}
if(Input->ArrowLeft.EndedDown || (Input->a.EndedDown && AllowWASDControls))
{
Dir -= ViewRight;
}
if(Math::Length(Dir) > 0.5f)
{
Dir = Math::Normalized(Dir);
}
}
for(int e = 0; e < Count; e++)
{
vec3 InputDir = Dir;
for(int i = 0; i < InputOverrideCount; i++)
{
if(InputOverrides[i].EntityIndex == EntityIndices[e])
{
InputDir = InputOverrides[i].WorldDir;
}
}
quat R = Entities[EntityIndices[e]].Transform.R;
R.V *= -1;
vec3 GoalVelocity =
Math::MulMat3Vec3(Math::QuatToMat3(R), InputControllers[e].MaxSpeed * InputDir);
vec3 GoalFacing =
InputControllers[e].UseStrafing
? Math::MulMat3Vec3(Math::QuatToMat3(R), ViewForward)
: (Math::Length(InputDir) != 0 ? Math::MulMat3Vec3(Math::QuatToMat3(R), InputDir)
: vec3{ 0, 0, 1 });
blend_in_info DominantBlend = BlendStacks[e].Peek();
float LocalAnimTime = GetLocalSampleTime(DominantBlend.Animation, GlobalTimes[e],
DominantBlend.GlobalAnimStartTime);
mat4 InvEntityMatrix = Math::InvMat4(TransformToMat4(Entities[EntityIndices[e]].Transform));
trajectory_update_args TrajectoryArgs = {};
{
TrajectoryArgs.PositionBias = InputControllers[e].PositionBias;
TrajectoryArgs.DirectionBias = InputControllers[e].DirectionBias;
TrajectoryArgs.InvEntityMatrix = InvEntityMatrix;
}
GetMMGoal(&OutGoals[e], &OutMirroredGoals[e], &Trajectories[e], TempAlloc, Skeletons[e],
DominantBlend.Animation, DominantBlend.Mirror, LocalAnimTime, GoalVelocity,
GoalFacing, MMControllers[e]->Params.DynamicParams.TrajectoryTimeHorizon,
MMControllers[e]->Params.FixedParams,
InputControllers[e].UseSmoothGoal ? &TrajectoryArgs : NULL);
}
}
void
AssertSplineIndicesAndClampWaypointIndices(spline_follow_state* SplineStates, int32_t Count,
const movement_spline* Splines, int32_t DebugSplineCount)
{
for(int i = 0; i < Count; i++)
{
int SplineIndex = SplineStates[i].SplineIndex;
assert(0 <= SplineIndex && SplineIndex < DebugSplineCount);
assert(Splines[SplineIndex].Waypoints.Count > 0);
SplineStates[i].NextWaypointIndex = ClampInt32InIn(0, SplineStates[i].NextWaypointIndex,
Splines[SplineIndex].Waypoints.Count - 1);
}
}
void
GenerateGoalsFromSplines(Memory::stack_allocator* TempAlloc, mm_frame_info* OutGoals,
mm_frame_info* OutMirroredGoals, trajectory* Trajectories,
spline_follow_state* SplineStates,
const mm_input_controller* InputControllers,
const mm_controller_data* const* MMControllers,
const blend_stack* BlendStacks, const float* AnimPlayerTimes,
const Anim::skeleton* const* Skeletons, const int32_t* EntityIndices,
int32_t Count, const movement_spline* Splines, int32_t DebugSplineCount,
const entity* Entities)
{
for(int e = 0; e < Count; e++)
{
const float WaypointRadius = 0.8f;
const float Inputdt = 1 / 60.0f;
transform EntityTransform = Entities[EntityIndices[e]].Transform;
quat InvR = EntityTransform.R;
InvR.V *= -1;
spline_follow_state& EntitySplineState = SplineStates[e];
assert(0 <= EntitySplineState.SplineIndex && EntitySplineState.SplineIndex < DebugSplineCount);
const movement_spline* Spline = &Splines[EntitySplineState.SplineIndex];
vec3 WorldDiff =
Spline->Waypoints[EntitySplineState.NextWaypointIndex].Position - EntityTransform.T;
vec3 LocalEntityToWaypoint = Math::MulMat3Vec3(Math::QuatToMat3(InvR), WorldDiff);
vec3 LocalDir = Math::Normalized(LocalEntityToWaypoint);
vec3 GoalVelocity = LocalDir*InputControllers[e].MaxSpeed;
vec3 GoalFacing = LocalDir;
blend_in_info DominantBlend = BlendStacks[e].Peek();
float LocalAnimTime = GetLocalSampleTime(DominantBlend.Animation, AnimPlayerTimes[e],
DominantBlend.GlobalAnimStartTime);
trajectory_update_args TrajectoryArgs = {};
{
TrajectoryArgs.PositionBias = InputControllers[e].PositionBias;
TrajectoryArgs.DirectionBias = InputControllers[e].DirectionBias;
TrajectoryArgs.InvEntityMatrix = Math::InvMat4(TransformToMat4(EntityTransform));
}
GetMMGoal(&OutGoals[e], &OutMirroredGoals[e], &Trajectories[e], TempAlloc, Skeletons[e],
DominantBlend.Animation, DominantBlend.Mirror, LocalAnimTime, GoalVelocity,
GoalFacing, MMControllers[e]->Params.DynamicParams.TrajectoryTimeHorizon,
MMControllers[e]->Params.FixedParams,
InputControllers[e].UseSmoothGoal ? &TrajectoryArgs : NULL);
if(Math::Length(LocalEntityToWaypoint) < WaypointRadius)
{
/*if(EntitySplineState.Loop)
{*/
EntitySplineState.NextWaypointIndex =
(EntitySplineState.NextWaypointIndex + 1) % Spline->Waypoints.Count;
/*}
else
{
EntitySplineState.MovingInPositive = false;
int Forward = (EntitySplineState.MovingInPositive) ? 1 : -1;
int NextWaypointIndex = EntitySplineState.NextWaypointIndex + Forward;
if(NextWaypointIndex < 0 ||
NextWaypointIndex > Spline->Waypoints.Count - 1) // Travel backwards
{
NextWaypointIndex -= 2 * Forward;
}
EntitySplineState.NextWaypointIndex =
ClampInt32InIn(0, NextWaypointIndex, Spline->Waypoints.Count - 1);
}*/
}
}
}
// Only used to visualize the mirrored match, not adequate way to flip for searching mirrors
mm_frame_info
VisualFlipGoalX(const mm_frame_info& Goal)
{
mm_frame_info FlippedGoal = Goal;
for(int i = 0; i < MM_COMPARISON_BONE_COUNT; i++)
{
FlippedGoal.BonePs[i].X *= -1;
FlippedGoal.BoneVs[i].X *= -1;
}
for(int i = 0; i < MM_POINT_COUNT; i++)
{
FlippedGoal.TrajectoryPs[i].X *= -1;
FlippedGoal.TrajectoryAngles[i] *= -1;
}
return FlippedGoal;
}
// Figure out if matched potition is sufficiently far away from all animations which are already
// playing
bool
IsNewAnimSufficientlyFarAway(const Anim::animation* NewAnim, float NewAnimLocalStartTime,
const blend_stack& BlendStack, float AnimPlayerTime,
float TimeOffetThreshold)
{
for(int a = 0; a < BlendStack.Count; a++)
{
blend_in_info ActiveAnimBlend = BlendStack[a];
float ActiveAnimLocalAnimTime = GetLocalSampleTime(ActiveAnimBlend.Animation, AnimPlayerTime,
ActiveAnimBlend.GlobalAnimStartTime);
if(NewAnim == ActiveAnimBlend.Animation &&
AbsFloat(ActiveAnimLocalAnimTime - NewAnimLocalStartTime) < TimeOffetThreshold)
{
return false;
}
}
return true;
}
void
MotionMatchGoals(blend_stack* OutBlendStacks, mm_frame_info* LastMatchedGoals,
transform* OutLastMatchedTransforms, const mm_frame_info* AnimGoals,
const mm_frame_info* MirroredAnimGoals,
const mm_controller_data* const* MMControllers, const float* GlobalTimes,
const int32_t* EntityIndices, int32_t Count, entity* Entities)
{
for(int i = 0; i < Count; i++)
{
int32_t NewAnimIndex;
float NewAnimLocalStartTime;
bool NewMatchIsMirrored = false;
mm_frame_info BestMatch = {};
if(!MMControllers[i]->Params.DynamicParams.MatchMirroredAnimations)
{
MotionMatch(&NewAnimIndex, &NewAnimLocalStartTime, &BestMatch, MMControllers[i],
AnimGoals[i]);
}
else
{
MotionMatchWithMirrors(&NewAnimIndex, &NewAnimLocalStartTime, &BestMatch, &NewMatchIsMirrored,
MMControllers[i], AnimGoals[i], MirroredAnimGoals[i]);
}
const Anim::animation* MatchedAnim = MMControllers[i]->Animations[NewAnimIndex];
if(IsNewAnimSufficientlyFarAway(MatchedAnim, NewAnimLocalStartTime, OutBlendStacks[i],
GlobalTimes[i],
MMControllers[i]->Params.DynamicParams.MinTimeOffsetThreshold))
{
LastMatchedGoals[i] = (NewMatchIsMirrored) ? VisualFlipGoalX(BestMatch) : BestMatch;
PlayAnimation(&OutBlendStacks[i], MMControllers[i]->Animations[NewAnimIndex], NewAnimIndex,
NewAnimLocalStartTime, GlobalTimes[i],
MMControllers[i]->Params.DynamicParams.BlendInTime, NewMatchIsMirrored);
// Store the transform of where the last match occured
OutLastMatchedTransforms[i] = Entities[EntityIndices[i]].Transform;
}
}
}
transform
GetLocalAnimRootMotionDelta(Anim::animation* RootMotionAnim, const Anim::skeleton* Skeleton,
bool MirrorRootMotionInX, float LocalSampleTime, float dt)
{
assert(RootMotionAnim);
const int HipBoneIndex = 0;
transform RootDelta = IdentityTransform();
float NextSampleTime = LocalSampleTime + dt;
transform CurrentHipTransform =
Anim::LinearAnimationBoneSample(RootMotionAnim, HipBoneIndex, LocalSampleTime);
transform NextHipTransform =
Anim::LinearAnimationBoneSample(RootMotionAnim, HipBoneIndex, NextSampleTime);
{
// mat4 Mat4CurrentHip = TransformToMat4(CurrentHipTransform);
mat4 Mat4CurrentHip =
Math::MulMat4(Skeleton->Bones[HipBoneIndex].BindPose, TransformToMat4(CurrentHipTransform));
mat4 Mat4CurrentRoot;
mat4 Mat4InvCurrentRoot;
Anim::GetRootAndInvRootMatrices(&Mat4CurrentRoot, &Mat4InvCurrentRoot, Mat4CurrentHip);
{
// mat4 Mat4NextHip = TransformToMat4(NextHipTransform);
mat4 Mat4NextHip =
Math::MulMat4(Skeleton->Bones[HipBoneIndex].BindPose, TransformToMat4(NextHipTransform));
mat4 Mat4NextRoot;
Anim::GetRootAndInvRootMatrices(&Mat4NextRoot, NULL, Mat4NextHip);
{
mat4 Mat4LocalRootDelta = Math::MulMat4(Mat4InvCurrentRoot, Mat4NextRoot);
quat dR = Math::QuatFromTo(Mat4CurrentRoot.Z, Mat4NextRoot.Z);
if(MirrorRootMotionInX)
{
Mat4LocalRootDelta = Math::MulMat4(Math::Mat4Scale(-1, 1, 1), Mat4LocalRootDelta);
dR = Math::QuatFromTo(Mat4NextRoot.Z, Mat4CurrentRoot.Z);
}
vec3 dT = Mat4LocalRootDelta.T;
RootDelta.T = dT;
RootDelta.R = dR;
return RootDelta;
}
}
}
}
void
ComputeLocalRootMotion(transform* OutLocalDeltaRootMotions, const Anim::skeleton* const* Skeletons,
const blend_stack* BlendStacks, const float* GlobalTimes, int32_t Count,
float dt)
{
for(int i = 0; i < Count; i++)
{
blend_in_info AnimBlend = BlendStacks[i].Peek();
Anim::animation* RootMotionAnim = AnimBlend.Animation;
float LocalSampleTime =
Anim::GetLocalSampleTime(RootMotionAnim, GlobalTimes[i], AnimBlend.GlobalAnimStartTime);
OutLocalDeltaRootMotions[i] =
GetLocalAnimRootMotionDelta(RootMotionAnim, Skeletons[i], AnimBlend.Mirror, LocalSampleTime,
dt);
}
}
void
ApplyRootMotion(entity* InOutEntities, trajectory* Trajectories,
const transform* LocalDeltaRootMotions, int32_t* EntityIndices, int32_t Count)
{
for(int i = 0; i < Count; i++)
{
transform* TargetTransform = &InOutEntities[EntityIndices[i]].Transform;
vec3 dT =
Math::MulMat4Vec4(Math::Mat4Rotate(TargetTransform->R), { LocalDeltaRootMotions[i].T, 0 })
.XYZ;
TargetTransform->R = TargetTransform->R * LocalDeltaRootMotions[i].R;
TargetTransform->T += dT;
vec2 DeltaToNewRoot = vec2{ TargetTransform->T.X, TargetTransform->T.Z } -
Trajectories[i].Transforms[HALF_TRAJECTORY_TRANSFORM_COUNT].T;
for(int p = 0; p < HALF_TRAJECTORY_TRANSFORM_COUNT; p++)
{
Trajectories[i].Transforms[HALF_TRAJECTORY_TRANSFORM_COUNT + p].T += DeltaToNewRoot;
}
Trajectories[i].Transforms[HALF_TRAJECTORY_TRANSFORM_COUNT].R = TargetTransform->R;
}
}
void
AdvanceAnimPlayerTimes(float* InOutAnimPlayerTimes, int32_t Count, float dt)
{
for(int i = 0; i < Count; i++)
{
InOutAnimPlayerTimes[i] += dt;
}
}
void
RemoveBlendedOutAnimsFromBlendStacks(blend_stack* InOutBlendStacks, const float* GlobalPlayTimes,
int32_t Count)
{
for(int i = 0; i < Count; i++)
{
for(int j = InOutBlendStacks[i].Count - 1; j >= 1; j--)
{
blend_in_info TempBlendInfo = InOutBlendStacks[i][j];
if((GlobalPlayTimes[i] - TempBlendInfo.GlobalBlendStartTime) >= TempBlendInfo.BlendDuration)
{
for(int k = 0; k < j; k++)
{
InOutBlendStacks[i].PopBack();
}
break;
}
}
}
}
void
CopyMMAnimDataToAnimationPlayers(entity* OutEntities, const blend_stack* BlendStacks,
const float* GlobalPlayTimes, const int32_t* EntityIndices,
int32_t Count)
{
for(int e = 0; e < Count; e++)
{
Anim::animation_player* C = OutEntities[EntityIndices[e]].AnimPlayer;
C->BlendFunc = BlendStackBlendFunc;
C->GlobalTimeSec = GlobalPlayTimes[e];
for(int i = 0; i < ANIM_PLAYER_MAX_ANIM_COUNT; i++)
{
C->States[i] = {};
}
C->AnimStateCount = BlendStacks[e].Count;
for(int a = 0; a < C->AnimStateCount; a++)
{
const blend_in_info BlendInfo = BlendStacks[e][a];
C->Animations[a] = BlendInfo.Animation;
C->States[a].StartTimeSec = BlendInfo.GlobalAnimStartTime;
C->States[a].Mirror = BlendInfo.Mirror;
C->States[a].Loop = BlendInfo.Loop;
C->States[a].PlaybackRateSec = 1.0f;
}
}
}
void
DrawFrameInfo(mm_frame_info AnimGoal, mat4 CoordinateFrame, mm_info_debug_settings DebugSettings,
vec3 BoneColor, vec3 VelocityColor, vec3 TrajectoryColor, vec3 DirectionColor)
{
const vec3 VerticalOffset{0, 0.005f, 0};
for(int i = 0; i < MM_COMPARISON_BONE_COUNT; i++)
{
vec4 HomogLocalBoneP = { AnimGoal.BonePs[i], 1 };
vec3 WorldBoneP = Math::MulMat4Vec4(CoordinateFrame, HomogLocalBoneP).XYZ;
vec4 HomogLocalBoneV = { AnimGoal.BoneVs[i], 0 };
vec3 WorldBoneV = Math::MulMat4Vec4(CoordinateFrame, HomogLocalBoneV).XYZ;
vec3 WorldVEnd = WorldBoneP + WorldBoneV;
if(DebugSettings.ShowBonePositions)
{
Debug::PushWireframeSphere(WorldBoneP, 0.02f, { BoneColor, 1 });
}
if(DebugSettings.ShowBoneVelocities)
{
Debug::PushLine(WorldBoneP, WorldVEnd, { VelocityColor, 1 });
Debug::PushWireframeSphere(WorldVEnd, 0.01f, { VelocityColor, 1 });
}
}
vec3 PrevWorldTrajectoryPointP = CoordinateFrame.T;
if(DebugSettings.ShowTrajectory)
{
for(int i = 0; i < MM_POINT_COUNT; i++)
{
vec4 HomogTrajectoryPointP = { AnimGoal.TrajectoryPs[i], 1 };
vec3 WorldTrajectoryPointP = Math::MulMat4Vec4(CoordinateFrame, HomogTrajectoryPointP).XYZ;
{
Debug::PushLine(PrevWorldTrajectoryPointP+VerticalOffset, WorldTrajectoryPointP+VerticalOffset, { TrajectoryColor, 1 },
DebugSettings.Overlay);
Debug::PushWireframeSphere(WorldTrajectoryPointP, 0.02f, { TrajectoryColor, 1 });
PrevWorldTrajectoryPointP = WorldTrajectoryPointP;
}
const float GoalDirectionLength = 0.2f;
if(DebugSettings.ShowTrajectoryAngles)
{
vec4 LocalSpaceFacingDirection = { sinf(AnimGoal.TrajectoryAngles[i]), 0,
cosf(AnimGoal.TrajectoryAngles[i]), 0 };
vec3 WorldSpaceFacingDirection =
Math::MulMat4Vec4(CoordinateFrame, LocalSpaceFacingDirection).XYZ;
Debug::PushLine(WorldTrajectoryPointP + VerticalOffset,
WorldTrajectoryPointP + GoalDirectionLength * WorldSpaceFacingDirection +
VerticalOffset,
{ DirectionColor, 1 }, DebugSettings.Overlay);
}
}
}
}
void
DrawTrajectory(mat4 CoordinateFrame, const trajectory* Trajectory, vec3 PastColor,
vec3 PresentColor, vec3 FutureColor)
{
for(int i = 0; i < TRAJECTORY_TRANSFORM_COUNT; i++)
{
vec3 CurrentWorldPos = vec3{ Trajectory->Transforms[i].T.X, 0, Trajectory->Transforms[i].T.Y };
vec3 Forward = { 0, 0, 1 };
vec3 FacingDir = Math::MulMat3Vec3(Math::QuatToMat3(Trajectory->Transforms[i].R), Forward);
vec3 PointColor = (i < HALF_TRAJECTORY_TRANSFORM_COUNT)
? PastColor
: ((i == HALF_TRAJECTORY_TRANSFORM_COUNT) ? PresentColor : FutureColor);
//Debug::PushWireframeSphere(CurrentWorldPos, 0.005f, { PointColor, 0.5 });
Debug::PushLine(CurrentWorldPos, CurrentWorldPos + FacingDir * 0.1f, { 0, 1, 1, 1 });
}
}