-
Notifications
You must be signed in to change notification settings - Fork 105
/
controller.cpp
2490 lines (2098 loc) · 88.5 KB
/
controller.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
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
extern "C"
{
#include "raylib.h"
#include "raymath.h"
#define RAYGUI_IMPLEMENTATION
#include "raygui.h"
}
#if defined(PLATFORM_WEB)
#include <emscripten/emscripten.h>
#endif
#include "common.h"
#include "vec.h"
#include "quat.h"
#include "spring.h"
#include "array.h"
#include "character.h"
#include "database.h"
#include "nnet.h"
#include "lmm.h"
#include <initializer_list>
#include <functional>
//--------------------------------------
static inline Vector3 to_Vector3(vec3 v)
{
return (Vector3){ v.x, v.y, v.z };
}
//--------------------------------------
// Perform linear blend skinning and copy
// result into mesh data. Update and upload
// deformed vertex positions and normals to GPU
void deform_character_mesh(
Mesh& mesh,
const character& c,
const slice1d<vec3> bone_anim_positions,
const slice1d<quat> bone_anim_rotations,
const slice1d<int> bone_parents)
{
linear_blend_skinning_positions(
slice1d<vec3>(mesh.vertexCount, (vec3*)mesh.vertices),
c.positions,
c.bone_weights,
c.bone_indices,
c.bone_rest_positions,
c.bone_rest_rotations,
bone_anim_positions,
bone_anim_rotations);
linear_blend_skinning_normals(
slice1d<vec3>(mesh.vertexCount, (vec3*)mesh.normals),
c.normals,
c.bone_weights,
c.bone_indices,
c.bone_rest_rotations,
bone_anim_rotations);
UpdateMeshBuffer(mesh, 0, mesh.vertices, mesh.vertexCount * 3 * sizeof(float), 0);
UpdateMeshBuffer(mesh, 2, mesh.normals, mesh.vertexCount * 3 * sizeof(float), 0);
}
Mesh make_character_mesh(character& c)
{
Mesh mesh = { 0 };
mesh.vertexCount = c.positions.size;
mesh.triangleCount = c.triangles.size / 3;
mesh.vertices = (float*)MemAlloc(c.positions.size * 3 * sizeof(float));
mesh.texcoords = (float*)MemAlloc(c.texcoords.size * 2 * sizeof(float));
mesh.normals = (float*)MemAlloc(c.normals.size * 3 * sizeof(float));
mesh.indices = (unsigned short*)MemAlloc(c.triangles.size * sizeof(unsigned short));
memcpy(mesh.vertices, c.positions.data, c.positions.size * 3 * sizeof(float));
memcpy(mesh.texcoords, c.texcoords.data, c.texcoords.size * 2 * sizeof(float));
memcpy(mesh.normals, c.normals.data, c.normals.size * 3 * sizeof(float));
memcpy(mesh.indices, c.triangles.data, c.triangles.size * sizeof(unsigned short));
UploadMesh(&mesh, true);
return mesh;
}
//--------------------------------------
// Basic functionality to get gamepad input including deadzone and
// squaring of the stick location to increase sensitivity. To make
// all the other code that uses this easier, we assume stick is
// oriented on floor (i.e. y-axis is zero)
enum
{
GAMEPAD_PLAYER = 0,
};
enum
{
GAMEPAD_STICK_LEFT,
GAMEPAD_STICK_RIGHT,
};
vec3 gamepad_get_stick(int stick, const float deadzone = 0.2f)
{
float gamepadx = GetGamepadAxisMovement(GAMEPAD_PLAYER, stick == GAMEPAD_STICK_LEFT ? GAMEPAD_AXIS_LEFT_X : GAMEPAD_AXIS_RIGHT_X);
float gamepady = GetGamepadAxisMovement(GAMEPAD_PLAYER, stick == GAMEPAD_STICK_LEFT ? GAMEPAD_AXIS_LEFT_Y : GAMEPAD_AXIS_RIGHT_Y);
float gamepadmag = sqrtf(gamepadx*gamepadx + gamepady*gamepady);
if (gamepadmag > deadzone)
{
float gamepaddirx = gamepadx / gamepadmag;
float gamepaddiry = gamepady / gamepadmag;
float gamepadclippedmag = gamepadmag > 1.0f ? 1.0f : gamepadmag*gamepadmag;
gamepadx = gamepaddirx * gamepadclippedmag;
gamepady = gamepaddiry * gamepadclippedmag;
}
else
{
gamepadx = 0.0f;
gamepady = 0.0f;
}
return vec3(gamepadx, 0.0f, gamepady);
}
//--------------------------------------
float orbit_camera_update_azimuth(
const float azimuth,
const vec3 gamepadstick_right,
const bool desired_strafe,
const float dt)
{
vec3 gamepadaxis = desired_strafe ? vec3() : gamepadstick_right;
return azimuth + 2.0f * dt * -gamepadaxis.x;
}
float orbit_camera_update_altitude(
const float altitude,
const vec3 gamepadstick_right,
const bool desired_strafe,
const float dt)
{
vec3 gamepadaxis = desired_strafe ? vec3() : gamepadstick_right;
return clampf(altitude + 2.0f * dt * gamepadaxis.z, 0.0, 0.4f * PIf);
}
float orbit_camera_update_distance(
const float distance,
const float dt)
{
float gamepadzoom =
IsGamepadButtonDown(GAMEPAD_PLAYER, GAMEPAD_BUTTON_LEFT_TRIGGER_1) ? +1.0f :
IsGamepadButtonDown(GAMEPAD_PLAYER, GAMEPAD_BUTTON_RIGHT_TRIGGER_1) ? -1.0f : 0.0f;
return clampf(distance + 10.0f * dt * gamepadzoom, 0.1f, 100.0f);
}
// Updates the camera using the orbit cam controls
void orbit_camera_update(
Camera3D& cam,
float& camera_azimuth,
float& camera_altitude,
float& camera_distance,
const vec3 target,
const vec3 gamepadstick_right,
const bool desired_strafe,
const float dt)
{
camera_azimuth = orbit_camera_update_azimuth(camera_azimuth, gamepadstick_right, desired_strafe, dt);
camera_altitude = orbit_camera_update_altitude(camera_altitude, gamepadstick_right, desired_strafe, dt);
camera_distance = orbit_camera_update_distance(camera_distance, dt);
quat rotation_azimuth = quat_from_angle_axis(camera_azimuth, vec3(0, 1, 0));
vec3 position = quat_mul_vec3(rotation_azimuth, vec3(0, 0, camera_distance));
vec3 axis = normalize(cross(position, vec3(0, 1, 0)));
quat rotation_altitude = quat_from_angle_axis(camera_altitude, axis);
vec3 eye = target + quat_mul_vec3(rotation_altitude, position);
cam.target = (Vector3){ target.x, target.y, target.z };
cam.position = (Vector3){ eye.x, eye.y, eye.z };
}
//--------------------------------------
bool desired_strafe_update()
{
return IsGamepadButtonDown(GAMEPAD_PLAYER, GAMEPAD_BUTTON_LEFT_TRIGGER_2) > 0.5f;
}
void desired_gait_update(
float& desired_gait,
float& desired_gait_velocity,
const float dt,
const float gait_change_halflife = 0.1f)
{
simple_spring_damper_exact(
desired_gait,
desired_gait_velocity,
IsGamepadButtonDown(GAMEPAD_PLAYER, GAMEPAD_BUTTON_RIGHT_FACE_DOWN) ? 1.0f : 0.0f,
gait_change_halflife,
dt);
}
vec3 desired_velocity_update(
const vec3 gamepadstick_left,
const float camera_azimuth,
const quat simulation_rotation,
const float fwrd_speed,
const float side_speed,
const float back_speed)
{
// Find stick position in world space by rotating using camera azimuth
vec3 global_stick_direction = quat_mul_vec3(
quat_from_angle_axis(camera_azimuth, vec3(0, 1, 0)), gamepadstick_left);
// Find stick position local to current facing direction
vec3 local_stick_direction = quat_inv_mul_vec3(
simulation_rotation, global_stick_direction);
// Scale stick by forward, sideways and backwards speeds
vec3 local_desired_velocity = local_stick_direction.z > 0.0 ?
vec3(side_speed, 0.0f, fwrd_speed) * local_stick_direction :
vec3(side_speed, 0.0f, back_speed) * local_stick_direction;
// Re-orientate into the world space
return quat_mul_vec3(simulation_rotation, local_desired_velocity);
}
quat desired_rotation_update(
const quat desired_rotation,
const vec3 gamepadstick_left,
const vec3 gamepadstick_right,
const float camera_azimuth,
const bool desired_strafe,
const vec3 desired_velocity)
{
quat desired_rotation_curr = desired_rotation;
// If strafe is active then desired direction is coming from right
// stick as long as that stick is being used, otherwise we assume
// forward facing
if (desired_strafe)
{
vec3 desired_direction = quat_mul_vec3(quat_from_angle_axis(camera_azimuth, vec3(0, 1, 0)), vec3(0, 0, -1));
if (length(gamepadstick_right) > 0.01f)
{
desired_direction = quat_mul_vec3(quat_from_angle_axis(camera_azimuth, vec3(0, 1, 0)), normalize(gamepadstick_right));
}
return quat_from_angle_axis(atan2f(desired_direction.x, desired_direction.z), vec3(0, 1, 0));
}
// If strafe is not active the desired direction comes from the left
// stick as long as that stick is being used
else if (length(gamepadstick_left) > 0.01f)
{
vec3 desired_direction = normalize(desired_velocity);
return quat_from_angle_axis(atan2f(desired_direction.x, desired_direction.z), vec3(0, 1, 0));
}
// Otherwise desired direction remains the same
else
{
return desired_rotation_curr;
}
}
//--------------------------------------
// Moving the root is a little bit difficult when we have the
// inertializer set up in the way we do. Essentially we need
// to also make sure to adjust all of the locations where
// we are transforming the data to and from as well as the
// offsets being blended out
void inertialize_root_adjust(
vec3& offset_position,
vec3& transition_src_position,
quat& transition_src_rotation,
vec3& transition_dst_position,
quat& transition_dst_rotation,
vec3& position,
quat& rotation,
const vec3 input_position,
const quat input_rotation)
{
// Find the position difference and add it to the state and transition location
vec3 position_difference = input_position - position;
position = position_difference + position;
transition_dst_position = position_difference + transition_dst_position;
// Find the point at which we want to now transition from in the src data
transition_src_position = transition_src_position + quat_mul_vec3(transition_src_rotation,
quat_inv_mul_vec3(transition_dst_rotation, position - offset_position - transition_dst_position));
transition_dst_position = position;
offset_position = vec3();
// Find the rotation difference. We need to normalize here or some error can accumulate
// over time during adjustment.
quat rotation_difference = quat_normalize(quat_mul_inv(input_rotation, rotation));
// Apply the rotation difference to the current rotation and transition location
rotation = quat_mul(rotation_difference, rotation);
transition_dst_rotation = quat_mul(rotation_difference, transition_dst_rotation);
}
void inertialize_pose_reset(
slice1d<vec3> bone_offset_positions,
slice1d<vec3> bone_offset_velocities,
slice1d<quat> bone_offset_rotations,
slice1d<vec3> bone_offset_angular_velocities,
vec3& transition_src_position,
quat& transition_src_rotation,
vec3& transition_dst_position,
quat& transition_dst_rotation,
const vec3 root_position,
const quat root_rotation)
{
bone_offset_positions.zero();
bone_offset_velocities.zero();
bone_offset_rotations.set(quat());
bone_offset_angular_velocities.zero();
transition_src_position = root_position;
transition_src_rotation = root_rotation;
transition_dst_position = vec3();
transition_dst_rotation = quat();
}
// This function transitions the inertializer for
// the full character. It takes as input the current
// offsets, as well as the root transition locations,
// current root state, and the full pose information
// for the pose being transitioned from (src) as well
// as the pose being transitioned to (dst) in their
// own animation spaces.
void inertialize_pose_transition(
slice1d<vec3> bone_offset_positions,
slice1d<vec3> bone_offset_velocities,
slice1d<quat> bone_offset_rotations,
slice1d<vec3> bone_offset_angular_velocities,
vec3& transition_src_position,
quat& transition_src_rotation,
vec3& transition_dst_position,
quat& transition_dst_rotation,
const vec3 root_position,
const vec3 root_velocity,
const quat root_rotation,
const vec3 root_angular_velocity,
const slice1d<vec3> bone_src_positions,
const slice1d<vec3> bone_src_velocities,
const slice1d<quat> bone_src_rotations,
const slice1d<vec3> bone_src_angular_velocities,
const slice1d<vec3> bone_dst_positions,
const slice1d<vec3> bone_dst_velocities,
const slice1d<quat> bone_dst_rotations,
const slice1d<vec3> bone_dst_angular_velocities)
{
// First we record the root position and rotation
// in the animation data for the source and destination
// animation
transition_dst_position = root_position;
transition_dst_rotation = root_rotation;
transition_src_position = bone_dst_positions(0);
transition_src_rotation = bone_dst_rotations(0);
// We then find the velocities so we can transition the
// root inertiaizers
vec3 world_space_dst_velocity = quat_mul_vec3(transition_dst_rotation,
quat_inv_mul_vec3(transition_src_rotation, bone_dst_velocities(0)));
vec3 world_space_dst_angular_velocity = quat_mul_vec3(transition_dst_rotation,
quat_inv_mul_vec3(transition_src_rotation, bone_dst_angular_velocities(0)));
// Transition inertializers recording the offsets for
// the root joint
inertialize_transition(
bone_offset_positions(0),
bone_offset_velocities(0),
root_position,
root_velocity,
root_position,
world_space_dst_velocity);
inertialize_transition(
bone_offset_rotations(0),
bone_offset_angular_velocities(0),
root_rotation,
root_angular_velocity,
root_rotation,
world_space_dst_angular_velocity);
// Transition all the inertializers for each other bone
for (int i = 1; i < bone_offset_positions.size; i++)
{
inertialize_transition(
bone_offset_positions(i),
bone_offset_velocities(i),
bone_src_positions(i),
bone_src_velocities(i),
bone_dst_positions(i),
bone_dst_velocities(i));
inertialize_transition(
bone_offset_rotations(i),
bone_offset_angular_velocities(i),
bone_src_rotations(i),
bone_src_angular_velocities(i),
bone_dst_rotations(i),
bone_dst_angular_velocities(i));
}
}
// This function updates the inertializer states. Here
// it outputs the smoothed animation (input plus offset)
// as well as updating the offsets themselves. It takes
// as input the current playing animation as well as the
// root transition locations, a halflife, and a dt
void inertialize_pose_update(
slice1d<vec3> bone_positions,
slice1d<vec3> bone_velocities,
slice1d<quat> bone_rotations,
slice1d<vec3> bone_angular_velocities,
slice1d<vec3> bone_offset_positions,
slice1d<vec3> bone_offset_velocities,
slice1d<quat> bone_offset_rotations,
slice1d<vec3> bone_offset_angular_velocities,
const slice1d<vec3> bone_input_positions,
const slice1d<vec3> bone_input_velocities,
const slice1d<quat> bone_input_rotations,
const slice1d<vec3> bone_input_angular_velocities,
const vec3 transition_src_position,
const quat transition_src_rotation,
const vec3 transition_dst_position,
const quat transition_dst_rotation,
const float halflife,
const float dt)
{
// First we find the next root position, velocity, rotation
// and rotational velocity in the world space by transforming
// the input animation from it's animation space into the
// space of the currently playing animation.
vec3 world_space_position = quat_mul_vec3(transition_dst_rotation,
quat_inv_mul_vec3(transition_src_rotation,
bone_input_positions(0) - transition_src_position)) + transition_dst_position;
vec3 world_space_velocity = quat_mul_vec3(transition_dst_rotation,
quat_inv_mul_vec3(transition_src_rotation, bone_input_velocities(0)));
// Normalize here because quat inv mul can sometimes produce
// unstable returns when the two rotations are very close.
quat world_space_rotation = quat_normalize(quat_mul(transition_dst_rotation,
quat_inv_mul(transition_src_rotation, bone_input_rotations(0))));
vec3 world_space_angular_velocity = quat_mul_vec3(transition_dst_rotation,
quat_inv_mul_vec3(transition_src_rotation, bone_input_angular_velocities(0)));
// Then we update these two inertializers with these new world space inputs
inertialize_update(
bone_positions(0),
bone_velocities(0),
bone_offset_positions(0),
bone_offset_velocities(0),
world_space_position,
world_space_velocity,
halflife,
dt);
inertialize_update(
bone_rotations(0),
bone_angular_velocities(0),
bone_offset_rotations(0),
bone_offset_angular_velocities(0),
world_space_rotation,
world_space_angular_velocity,
halflife,
dt);
// Then we update the inertializers for the rest of the bones
for (int i = 1; i < bone_positions.size; i++)
{
inertialize_update(
bone_positions(i),
bone_velocities(i),
bone_offset_positions(i),
bone_offset_velocities(i),
bone_input_positions(i),
bone_input_velocities(i),
halflife,
dt);
inertialize_update(
bone_rotations(i),
bone_angular_velocities(i),
bone_offset_rotations(i),
bone_offset_angular_velocities(i),
bone_input_rotations(i),
bone_input_angular_velocities(i),
halflife,
dt);
}
}
//--------------------------------------
// Copy a part of a feature vector from the
// matching database into the query feature vector
void query_copy_denormalized_feature(
slice1d<float> query,
int& offset,
const int size,
const slice1d<float> features,
const slice1d<float> features_offset,
const slice1d<float> features_scale)
{
for (int i = 0; i < size; i++)
{
query(offset + i) = features(offset + i) * features_scale(offset + i) + features_offset(offset + i);
}
offset += size;
}
// Compute the query feature vector for the current
// trajectory controlled by the gamepad.
void query_compute_trajectory_position_feature(
slice1d<float> query,
int& offset,
const vec3 root_position,
const quat root_rotation,
const slice1d<vec3> trajectory_positions)
{
vec3 traj0 = quat_inv_mul_vec3(root_rotation, trajectory_positions(1) - root_position);
vec3 traj1 = quat_inv_mul_vec3(root_rotation, trajectory_positions(2) - root_position);
vec3 traj2 = quat_inv_mul_vec3(root_rotation, trajectory_positions(3) - root_position);
query(offset + 0) = traj0.x;
query(offset + 1) = traj0.z;
query(offset + 2) = traj1.x;
query(offset + 3) = traj1.z;
query(offset + 4) = traj2.x;
query(offset + 5) = traj2.z;
offset += 6;
}
// Same but for the trajectory direction
void query_compute_trajectory_direction_feature(
slice1d<float> query,
int& offset,
const quat root_rotation,
const slice1d<quat> trajectory_rotations)
{
vec3 traj0 = quat_inv_mul_vec3(root_rotation, quat_mul_vec3(trajectory_rotations(1), vec3(0, 0, 1)));
vec3 traj1 = quat_inv_mul_vec3(root_rotation, quat_mul_vec3(trajectory_rotations(2), vec3(0, 0, 1)));
vec3 traj2 = quat_inv_mul_vec3(root_rotation, quat_mul_vec3(trajectory_rotations(3), vec3(0, 0, 1)));
query(offset + 0) = traj0.x;
query(offset + 1) = traj0.z;
query(offset + 2) = traj1.x;
query(offset + 3) = traj1.z;
query(offset + 4) = traj2.x;
query(offset + 5) = traj2.z;
offset += 6;
}
//--------------------------------------
// Collide against the obscales which are
// essentially bounding boxes of a given size
vec3 simulation_collide_obstacles(
const vec3 prev_pos,
const vec3 next_pos,
const slice1d<vec3> obstacles_positions,
const slice1d<vec3> obstacles_scales,
const float radius = 0.6f)
{
vec3 dx = next_pos - prev_pos;
vec3 proj_pos = prev_pos;
// Substep because I'm too lazy to implement CCD
int substeps = 1 + (int)(length(dx) * 5.0f);
for (int j = 0; j < substeps; j++)
{
proj_pos = proj_pos + dx / substeps;
for (int i = 0; i < obstacles_positions.size; i++)
{
// Find nearest point inside obscale and push out
vec3 nearest = clamp(proj_pos,
obstacles_positions(i) - 0.5f * obstacles_scales(i),
obstacles_positions(i) + 0.5f * obstacles_scales(i));
if (length(nearest - proj_pos) < radius)
{
proj_pos = radius * normalize(proj_pos - nearest) + nearest;
}
}
}
return proj_pos;
}
// Taken from https://theorangeduck.com/page/spring-roll-call#controllers
void simulation_positions_update(
vec3& position,
vec3& velocity,
vec3& acceleration,
const vec3 desired_velocity,
const float halflife,
const float dt,
const slice1d<vec3> obstacles_positions,
const slice1d<vec3> obstacles_scales)
{
float y = halflife_to_damping(halflife) / 2.0f;
vec3 j0 = velocity - desired_velocity;
vec3 j1 = acceleration + j0*y;
float eydt = fast_negexpf(y*dt);
vec3 position_prev = position;
position = eydt*(((-j1)/(y*y)) + ((-j0 - j1*dt)/y)) +
(j1/(y*y)) + j0/y + desired_velocity * dt + position_prev;
velocity = eydt*(j0 + j1*dt) + desired_velocity;
acceleration = eydt*(acceleration - j1*y*dt);
position = simulation_collide_obstacles(
position_prev,
position,
obstacles_positions,
obstacles_scales);
}
void simulation_rotations_update(
quat& rotation,
vec3& angular_velocity,
const quat desired_rotation,
const float halflife,
const float dt)
{
simple_spring_damper_exact(
rotation,
angular_velocity,
desired_rotation,
halflife, dt);
}
// Predict what the desired velocity will be in the
// future. Here we need to use the future trajectory
// rotation as well as predicted future camera
// position to find an accurate desired velocity in
// the world space
void trajectory_desired_velocities_predict(
slice1d<vec3> desired_velocities,
const slice1d<quat> trajectory_rotations,
const vec3 desired_velocity,
const float camera_azimuth,
const vec3 gamepadstick_left,
const vec3 gamepadstick_right,
const bool desired_strafe,
const float fwrd_speed,
const float side_speed,
const float back_speed,
const float dt)
{
desired_velocities(0) = desired_velocity;
for (int i = 1; i < desired_velocities.size; i++)
{
desired_velocities(i) = desired_velocity_update(
gamepadstick_left,
orbit_camera_update_azimuth(
camera_azimuth, gamepadstick_right, desired_strafe, i * dt),
trajectory_rotations(i),
fwrd_speed,
side_speed,
back_speed);
}
}
void trajectory_positions_predict(
slice1d<vec3> positions,
slice1d<vec3> velocities,
slice1d<vec3> accelerations,
const vec3 position,
const vec3 velocity,
const vec3 acceleration,
const slice1d<vec3> desired_velocities,
const float halflife,
const float dt,
const slice1d<vec3> obstacles_positions,
const slice1d<vec3> obstacles_scales)
{
positions(0) = position;
velocities(0) = velocity;
accelerations(0) = acceleration;
for (int i = 1; i < positions.size; i++)
{
positions(i) = positions(i-1);
velocities(i) = velocities(i-1);
accelerations(i) = accelerations(i-1);
simulation_positions_update(
positions(i),
velocities(i),
accelerations(i),
desired_velocities(i),
halflife,
dt,
obstacles_positions,
obstacles_scales);
}
}
// Predict desired rotations given the estimated future
// camera rotation and other parameters
void trajectory_desired_rotations_predict(
slice1d<quat> desired_rotations,
const slice1d<vec3> desired_velocities,
const quat desired_rotation,
const float camera_azimuth,
const vec3 gamepadstick_left,
const vec3 gamepadstick_right,
const bool desired_strafe,
const float dt)
{
desired_rotations(0) = desired_rotation;
for (int i = 1; i < desired_rotations.size; i++)
{
desired_rotations(i) = desired_rotation_update(
desired_rotations(i-1),
gamepadstick_left,
gamepadstick_right,
orbit_camera_update_azimuth(
camera_azimuth, gamepadstick_right, desired_strafe, i * dt),
desired_strafe,
desired_velocities(i));
}
}
void trajectory_rotations_predict(
slice1d<quat> rotations,
slice1d<vec3> angular_velocities,
const quat rotation,
const vec3 angular_velocity,
const slice1d<quat> desired_rotations,
const float halflife,
const float dt)
{
rotations.set(rotation);
angular_velocities.set(angular_velocity);
for (int i = 1; i < rotations.size; i++)
{
simulation_rotations_update(
rotations(i),
angular_velocities(i),
desired_rotations(i),
halflife,
i * dt);
}
}
//--------------------------------------
void contact_reset(
bool& contact_state,
bool& contact_lock,
vec3& contact_position,
vec3& contact_velocity,
vec3& contact_point,
vec3& contact_target,
vec3& contact_offset_position,
vec3& contact_offset_velocity,
const vec3 input_contact_position,
const vec3 input_contact_velocity,
const bool input_contact_state)
{
contact_state = false;
contact_lock = false;
contact_position = input_contact_position;
contact_velocity = input_contact_velocity;
contact_point = input_contact_position;
contact_target = input_contact_position;
contact_offset_position = vec3();
contact_offset_velocity = vec3();
}
void contact_update(
bool& contact_state,
bool& contact_lock,
vec3& contact_position,
vec3& contact_velocity,
vec3& contact_point,
vec3& contact_target,
vec3& contact_offset_position,
vec3& contact_offset_velocity,
const vec3 input_contact_position,
const bool input_contact_state,
const float unlock_radius,
const float foot_height,
const float halflife,
const float dt,
const float eps=1e-8)
{
// First compute the input contact position velocity via finite difference
vec3 input_contact_velocity =
(input_contact_position - contact_target) / (dt + eps);
contact_target = input_contact_position;
// Update the inertializer to tick forward in time
inertialize_update(
contact_position,
contact_velocity,
contact_offset_position,
contact_offset_velocity,
// If locked we feed the contact point and zero velocity,
// otherwise we feed the input from the animation
contact_lock ? contact_point : input_contact_position,
contact_lock ? vec3() : input_contact_velocity,
halflife,
dt);
// If the contact point is too far from the current input position
// then we need to unlock the contact
bool unlock_contact = contact_lock && (
length(contact_point - input_contact_position) > unlock_radius);
// If the contact was previously inactive but is now active we
// need to transition to the locked contact state
if (!contact_state && input_contact_state)
{
// Contact point is given by the current position of
// the foot projected onto the ground plus foot height
contact_lock = true;
contact_point = contact_position;
contact_point.y = foot_height;
inertialize_transition(
contact_offset_position,
contact_offset_velocity,
input_contact_position,
input_contact_velocity,
contact_point,
vec3());
}
// Otherwise if we need to unlock or we were previously in
// contact but are no longer we transition to just taking
// the input position as-is
else if ((contact_lock && contact_state && !input_contact_state)
|| unlock_contact)
{
contact_lock = false;
inertialize_transition(
contact_offset_position,
contact_offset_velocity,
contact_point,
vec3(),
input_contact_position,
input_contact_velocity);
}
// Update contact state
contact_state = input_contact_state;
}
//--------------------------------------
// Rotate a joint to look toward some
// given target position
void ik_look_at(
quat& bone_rotation,
const quat global_parent_rotation,
const quat global_rotation,
const vec3 global_position,
const vec3 child_position,
const vec3 target_position,
const float eps = 1e-5f)
{
vec3 curr_dir = normalize(child_position - global_position);
vec3 targ_dir = normalize(target_position - global_position);
if (fabs(1.0f - dot(curr_dir, targ_dir) > eps))
{
bone_rotation = quat_inv_mul(global_parent_rotation,
quat_mul(quat_between(curr_dir, targ_dir), global_rotation));
}
}
// Basic two-joint IK in the style of https://theorangeduck.com/page/simple-two-joint
// Here I add a basic "forward vector" which acts like a kind of pole-vetor
// to control the bending direction
void ik_two_bone(
quat& bone_root_lr,
quat& bone_mid_lr,
const vec3 bone_root,
const vec3 bone_mid,
const vec3 bone_end,
const vec3 target,
const vec3 fwd,
const quat bone_root_gr,
const quat bone_mid_gr,
const quat bone_par_gr,
const float max_length_buffer) {
float max_extension =
length(bone_root - bone_mid) +
length(bone_mid - bone_end) -
max_length_buffer;
vec3 target_clamp = target;
if (length(target - bone_root) > max_extension)
{
target_clamp = bone_root + max_extension * normalize(target - bone_root);
}
vec3 axis_dwn = normalize(bone_end - bone_root);
vec3 axis_rot = normalize(cross(axis_dwn, fwd));
vec3 a = bone_root;
vec3 b = bone_mid;
vec3 c = bone_end;
vec3 t = target_clamp;
float lab = length(b - a);
float lcb = length(b - c);
float lat = length(t - a);
float ac_ab_0 = acosf(clampf(dot(normalize(c - a), normalize(b - a)), -1.0f, 1.0f));
float ba_bc_0 = acosf(clampf(dot(normalize(a - b), normalize(c - b)), -1.0f, 1.0f));
float ac_ab_1 = acosf(clampf((lab * lab + lat * lat - lcb * lcb) / (2.0f * lab * lat), -1.0f, 1.0f));
float ba_bc_1 = acosf(clampf((lab * lab + lcb * lcb - lat * lat) / (2.0f * lab * lcb), -1.0f, 1.0f));
quat r0 = quat_from_angle_axis(ac_ab_1 - ac_ab_0, axis_rot);
quat r1 = quat_from_angle_axis(ba_bc_1 - ba_bc_0, axis_rot);
vec3 c_a = normalize(bone_end - bone_root);
vec3 t_a = normalize(target_clamp - bone_root);
quat r2 = quat_from_angle_axis(
acosf(clampf(dot(c_a, t_a), -1.0f, 1.0f)),
normalize(cross(c_a, t_a)));
bone_root_lr = quat_inv_mul(bone_par_gr, quat_mul(r2, quat_mul(r0, bone_root_gr)));
bone_mid_lr = quat_inv_mul(bone_root_gr, quat_mul(r1, bone_mid_gr));
}
//--------------------------------------
void draw_axis(const vec3 pos, const quat rot, const float scale = 1.0f)
{
vec3 axis0 = pos + quat_mul_vec3(rot, scale * vec3(1.0f, 0.0f, 0.0f));
vec3 axis1 = pos + quat_mul_vec3(rot, scale * vec3(0.0f, 1.0f, 0.0f));
vec3 axis2 = pos + quat_mul_vec3(rot, scale * vec3(0.0f, 0.0f, 1.0f));
DrawLine3D(to_Vector3(pos), to_Vector3(axis0), RED);
DrawLine3D(to_Vector3(pos), to_Vector3(axis1), GREEN);
DrawLine3D(to_Vector3(pos), to_Vector3(axis2), BLUE);
}
void draw_features(const slice1d<float> features, const vec3 pos, const quat rot, const Color color)
{
vec3 lfoot_pos = quat_mul_vec3(rot, vec3(features( 0), features( 1), features( 2))) + pos;
vec3 rfoot_pos = quat_mul_vec3(rot, vec3(features( 3), features( 4), features( 5))) + pos;
vec3 lfoot_vel = quat_mul_vec3(rot, vec3(features( 6), features( 7), features( 8)));
vec3 rfoot_vel = quat_mul_vec3(rot, vec3(features( 9), features(10), features(11)));
//vec3 hip_vel = quat_mul_vec3(rot, vec3(features(12), features(13), features(14)));
vec3 traj0_pos = quat_mul_vec3(rot, vec3(features(15), 0.0f, features(16))) + pos;
vec3 traj1_pos = quat_mul_vec3(rot, vec3(features(17), 0.0f, features(18))) + pos;
vec3 traj2_pos = quat_mul_vec3(rot, vec3(features(19), 0.0f, features(20))) + pos;
vec3 traj0_dir = quat_mul_vec3(rot, vec3(features(21), 0.0f, features(22)));
vec3 traj1_dir = quat_mul_vec3(rot, vec3(features(23), 0.0f, features(24)));
vec3 traj2_dir = quat_mul_vec3(rot, vec3(features(25), 0.0f, features(26)));
DrawSphereWires(to_Vector3(lfoot_pos), 0.05f, 4, 10, color);
DrawSphereWires(to_Vector3(rfoot_pos), 0.05f, 4, 10, color);
DrawSphereWires(to_Vector3(traj0_pos), 0.05f, 4, 10, color);
DrawSphereWires(to_Vector3(traj1_pos), 0.05f, 4, 10, color);
DrawSphereWires(to_Vector3(traj2_pos), 0.05f, 4, 10, color);
DrawLine3D(to_Vector3(lfoot_pos), to_Vector3(lfoot_pos + 0.1f * lfoot_vel), color);
DrawLine3D(to_Vector3(rfoot_pos), to_Vector3(rfoot_pos + 0.1f * rfoot_vel), color);
DrawLine3D(to_Vector3(traj0_pos), to_Vector3(traj0_pos + 0.25f * traj0_dir), color);
DrawLine3D(to_Vector3(traj1_pos), to_Vector3(traj1_pos + 0.25f * traj1_dir), color);
DrawLine3D(to_Vector3(traj2_pos), to_Vector3(traj2_pos + 0.25f * traj2_dir), color);
}