-
Notifications
You must be signed in to change notification settings - Fork 0
/
player_controller.h
225 lines (192 loc) · 7.84 KB
/
player_controller.h
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
#pragma once
#include "entity.h"
#include "common.h"
#include "camera.h"
#include "motion_matching.h"
#include "resource_manager.h"
#include "blend_stack.h"
namespace Gameplay
{
void ResetPlayer(entity* Player, blend_stack* BlendStack, Resource::resource_manager* Resources,
const mm_controller_data* MMData);
void UpdatePlayer(entity* Player, blend_stack* BlendStack, Memory::stack_allocator* TempAlocator,
const game_input* Input, const camera* Camera, const mm_controller_data* MMData,
const mm_debug_settings* MMDebug, float Speed);
}
//END OF player_conroller.h
#include "player_controller.h"
#include "math.h"
#include "blend_stack.h"
#include "debug_drawing.h"
#include "profile.h"
const vec3 YAxis = { 0, 1, 0 };
const vec3 ZAxis = { 0, 0, 1 };
transform GetAnimRootMotionDelta(Anim::animation* RootMotionAnim,
const Anim::animation_controller* C, bool MirrorRootMotionInX,
float LocalSampleTime, float dt);
void DrawFrameInfo(mm_frame_info AnimGoal, mat4 CoordinateFrame,
mm_info_debug_settings DebugSettings, vec3 BoneColor, vec3 VelocityColor,
vec3 TrajectoryColor, vec3 DirectionColor);
void
Gameplay::ResetPlayer(entity* Player, blend_stack* BlendStack,
Resource::resource_manager* Resources, const mm_controller_data* MMData)
{
assert(Player->AnimController);
Player->AnimController->BlendFunc = NULL;
for(int i = 0; i < Player->AnimController->AnimStateCount; i++)
{
if(Player->AnimController->AnimationIDs[i].Value != 0)
{
bool AnimIsUsedByMMData = false;
for(int j = 0; MMData->FrameInfos.IsValid() && j < MMData->Params.AnimRIDs.Count; j++)
{
if(Player->AnimController->AnimationIDs[i].Value == MMData->Params.AnimRIDs[j].Value)
{
AnimIsUsedByMMData = true;
break;
}
}
if(!AnimIsUsedByMMData)
{
Resources->Animations.RemoveReference(Player->AnimController->AnimationIDs[i]);
}
Player->AnimController->AnimationIDs[i] = {};
Player->AnimController->States[i] = {};
Player->AnimController->Animations[i] = NULL;
}
}
Player->AnimController->AnimStateCount = 0;
ResetBlendStack(BlendStack);
}
void
Gameplay::UpdatePlayer(entity* Player, blend_stack* InOutBlendStack,
Memory::stack_allocator* TempAlocator, const game_input* Input,
const camera* Camera, const mm_controller_data* MMData,
const mm_debug_settings* MMDebug, float Speed)
{
blend_stack& BlendStack = *InOutBlendStack;
TIMED_BLOCK(UpdatePlayer);
assert(MMData);
assert(MMData->FrameInfos.IsValid());
assert(Player->AnimController);
assert(0 < MMData->Params.AnimRIDs.Count);
for(int i = 0; i < MMData->Params.AnimRIDs.Count; i++)
{
assert(MMData->Animations[i]);
}
// Determine the input direction
vec3 Dir = {};
{
vec3 CameraForward = Camera->Forward;
vec3 ViewForward = Math::Normalized(vec3{ CameraForward.X, 0, CameraForward.Z });
vec3 ViewRight = Math::Cross(ViewForward, YAxis);
if(Input->ArrowUp.EndedDown)
{
Dir += ViewForward;
}
if(Input->ArrowDown.EndedDown)
{
Dir -= ViewForward;
}
if(Input->ArrowRight.EndedDown)
{
Dir += ViewRight;
}
if(Input->ArrowLeft.EndedDown)
{
Dir -= ViewRight;
}
if(Math::Length(Dir) > 0.5f)
{
Dir = Math::Normalized(Dir);
}
}
Player->AnimController->BlendFunc = ThirdPersonAnimationBlendFunction;
if(BlendStack.Empty())
{
int InitialAnimIndex = 0;
float StartTime = 0;
bool PlayMirrored = false;
/*PlayAnimation(Player->AnimController, &BlendStack, MMData->Params.AnimRIDs[InitialAnimIndex],
InitialAnimIndex, StartTime, MMData->Params.DynamicParams.BelndInTime,
PlayMirrored);*/
}
else
{
mat4 ModelMatrix = TransformToMat4(Player->Transform);
mm_frame_info AnimGoal = {};
{
mat4 InvModelMatrix = Math::InvMat4(ModelMatrix);
vec3 DesiredModelSpaceVelocity =
Math::MulMat4Vec4(InvModelMatrix,
vec4{ MMData->Params.DynamicParams.TrajectoryTimeHorizon * Dir * Speed,
0 })
.XYZ;
int32_t CurrentAnimIndex = BlendStack.Peek().AnimStateIndex;
AnimGoal = {};
// GetMMGoal(TempAlocator, CurrentAnimIndex, BlendStack.Peek().Mirror, Player->AnimController,
// DesiredModelSpaceVelocity, MMData->Params.FixedParams);
}
DrawFrameInfo(AnimGoal, ModelMatrix, MMDebug->CurrentGoal, { 1, 0, 1 }, { 1, 0, 1 },
{ 0, 0, 1 }, { 1, 0, 0 });
static mm_frame_info LastMatch = {};
DrawFrameInfo(LastMatch, ModelMatrix, MMDebug->MatchedGoal, { 1, 1, 0 }, { 1, 1, 0 },
{ 0, 1, 0 }, { 1, 0, 0 });
{
int32_t NewAnimIndex;
float NewAnimStartTime;
bool NewAnimIsMirrored = false;
mm_frame_info BestMatch = {};
if(!MMData->Params.DynamicParams.MatchMirroredAnimations)
{
MotionMatch(&NewAnimIndex, &NewAnimStartTime, &BestMatch, MMData, AnimGoal);
}
else
{
MotionMatchWithMirrors(&NewAnimIndex, &NewAnimStartTime, &BestMatch, &NewAnimIsMirrored,
MMData, AnimGoal);
}
const Anim::animation* MatchedAnim = MMData->Animations[NewAnimIndex];
// Note: this will always run at least one frame after matching the head of the stack so
// animation pointer should always be present in the AnimController
int ActiveStateIndex = BlendStack.Peek().AnimStateIndex;
float ActiveAnimLocalTime = Anim::GetLocalSampleTime(Player->AnimController, ActiveStateIndex,
Player->AnimController->GlobalTimeSec);
// Figure out if matched frame is sufficiently far away from the current to start a new
// animation
if(Player->AnimController->AnimationIDs[ActiveStateIndex].Value !=
MMData->Params.AnimRIDs[NewAnimIndex].Value ||
(AbsFloat(ActiveAnimLocalTime - NewAnimStartTime) >=
MMData->Params.DynamicParams.MinTimeOffsetThreshold &&
NewAnimIsMirrored == BlendStack.Peek().Mirror) ||
NewAnimIsMirrored != BlendStack.Peek().Mirror)
{
LastMatch = (NewAnimIsMirrored)
? GetMirroredFrameGoal(BestMatch, { -1, 1, 1 }, MMData->Params.FixedParams)
: BestMatch;
/*PlayAnimation(Player->AnimController, &BlendStack, MMData->Params.AnimRIDs[NewAnimIndex],
NewAnimIndex, NewAnimStartTime, MMData->Params.DynamicParams.BelndInTime,
NewAnimIsMirrored);*/
}
}
ThirdPersonBelndFuncStopUnusedAnimations(Player->AnimController, &BlendStack);
// Root motion
if(MMDebug->ApplyRootMotion)
{
blend_in_info AnimBlend = BlendStack.Peek();
//Note(Lukas) this is deprecated and just made to compile
Anim::animation* RootMotionAnim = Player->AnimController->Animations[AnimBlend.AnimStateIndex];
float LocalSampleTime =
Anim::GetLocalSampleTime(RootMotionAnim,
&Player->AnimController->States[AnimBlend.AnimStateIndex],
Player->AnimController->GlobalTimeSec);
/*transform LocalRootDelta =
GetAnimRootMotionDelta(RootMotionAnim, Player->AnimController, AnimBlend.Mirror,
LocalSampleTime, Input->dt);*/
//TODO(Lukas) The translation has to be rotated by the entity matrix for this to work again
transform RootDelta = IdentityTransform(); // LocalRootDelta;
Player->Transform.T += RootDelta.T;
Player->Transform.R = Player->Transform.R * RootDelta.R;
}
}
}