sit_t animator exchange
[carveJwlIkooP6JGAAIwe30JlM.git] / player_dead.c
1 #ifndef PLAYER_DEAD_C
2 #define PLAYER_DEAD_C
3
4 #include "player.h"
5
6 static void player__dead_update(void){
7 player_ragdoll_iter( &localplayer.ragdoll );
8 }
9
10 static void player__dead_post_update(void){
11 struct player_avatar *av = localplayer.playeravatar;
12 struct ragdoll_part *part = &localplayer.ragdoll.parts[ av->id_hip-1 ];
13 struct player_dead *d = &player_dead;
14
15 v3f ext_co;
16 v4f ext_q;
17 rb_extrapolate( &part->obj.rb, ext_co, ext_q );
18
19 v3_lerp( d->co_lpf, ext_co, vg.time_frame_delta*4.0f, d->co_lpf );
20 v3_lerp( d->v_lpf, part->obj.rb.v, vg.time_frame_delta*4.0f, d->v_lpf );
21 v3_lerp( d->w_lpf, part->obj.rb.w, vg.time_frame_delta*4.0f, d->w_lpf );
22
23 v3_copy( d->co_lpf, localplayer.rb.co );
24 v3_zero( localplayer.rb.v );
25 v3_zero( localplayer.rb.w );
26 }
27
28 static void player__dead_animate(void){
29 struct player_dead *d = &player_dead;
30 struct player_dead_animator *animator = &d->animator;
31 struct player_ragdoll *rd = &localplayer.ragdoll;
32 struct player_avatar *av = localplayer.playeravatar;
33 struct skeleton *sk = &av->sk;
34
35 m4x3f transforms[ 32 ];
36
37 /* root transform */
38 q_m3x3( localplayer.rb.q, transforms[0] );
39 v3_copy( localplayer.rb.co, transforms[0][3] );
40
41 v4_copy( localplayer.rb.q, animator->transforms[0].q );
42 v3_copy( localplayer.rb.co, animator->transforms[0].co );
43
44 /* colliders with bones transforms */
45 for( int i=0; i<rd->part_count; i++ ){
46 struct ragdoll_part *part = &rd->parts[i];
47
48 m4x3f mtx;
49
50 v4f q_int;
51 v3f co_int;
52
53 float substep = vg.time_fixed_extrapolate;
54 v3_lerp( part->prev_co, part->obj.rb.co, substep, co_int );
55 q_nlerp( part->prev_q, part->obj.rb.q, substep, q_int );
56 v4_copy( part->obj.rb.q, q_int );
57
58 q_m3x3( q_int, mtx );
59 v3_copy( co_int, mtx[3] );
60
61 m4x3_mul( mtx, part->inv_collider_mtx, transforms[part->bone_id] );
62 }
63
64 /* bones without colliders transforms */
65 for( u32 i=1; i<sk->bone_count; i++ ){
66 struct skeleton_bone *sb = &sk->bones[i];
67
68 if( sb->parent && !sb->collider ){
69 v3f delta;
70 v3_sub( sk->bones[i].co, sk->bones[sb->parent].co, delta );
71
72 m4x3f posemtx;
73 m3x3_identity( posemtx );
74 v3_copy( delta, posemtx[3] );
75
76 /* final matrix */
77 m4x3_mul( transforms[sb->parent], posemtx, transforms[i] );
78 }
79 }
80
81 /* measurements */
82 for( u32 i=1; i<sk->bone_count; i++ ){
83 struct skeleton_bone *sb = &sk->bones[i];
84
85 v3_zero( animator->transforms[i].co );
86 q_identity( animator->transforms[i].q );
87
88 m4x3f parent, inverse, local;
89 m3x3_identity( parent );
90 v3_sub( sk->bones[i].co, sk->bones[sb->parent].co, parent[3] );
91 m4x3_mul( transforms[ sb->parent ], parent, parent );
92 m4x3_invert_affine( parent, inverse );
93
94 v3f _s;
95 m4x3_mul( inverse, transforms[i], local );
96 m4x3_decompose( local, animator->transforms[i].co,
97 animator->transforms[i].q, _s );
98 }
99 }
100
101 static void player__dead_pose( void *_animator, player_pose *pose ){
102 struct player_dead_animator *animator = _animator;
103 struct player_ragdoll *rd = &localplayer.ragdoll;
104 struct player_avatar *av = localplayer.playeravatar;
105 struct skeleton *sk = &av->sk;
106
107 pose->type = k_player_pose_type_fk_2;
108 pose->board.lean = 0.0f;
109
110 v3_copy( animator->transforms[0].co, pose->root_co );
111 v4_copy( animator->transforms[0].q, pose->root_q );
112
113 for( u32 i=1; i<sk->bone_count; i++ ){
114 v3_copy( animator->transforms[i].co, pose->keyframes[i-1].co );
115 v4_copy( animator->transforms[i].q, pose->keyframes[i-1].q );
116 v3_fill( pose->keyframes[i-1].s, 1.0f );
117 }
118 }
119
120 static void player__dead_post_animate(void){
121 localplayer.cam_velocity_influence = 1.0f;
122 }
123
124 static void player__dead_im_gui(void){
125 }
126
127 static void player__dead_transition(void){
128 localplayer.subsystem = k_player_subsystem_dead;
129 copy_avatar_pose_to_ragdoll( localplayer.playeravatar, &localplayer.ragdoll,
130 localplayer.rb.v );
131
132 struct player_avatar *av = localplayer.playeravatar;
133 struct ragdoll_part *part = &localplayer.ragdoll.parts[ av->id_hip-1 ];
134 v3_copy( part->obj.rb.co, player_dead.co_lpf );
135 v3_copy( part->obj.rb.v, player_dead.v_lpf );
136 v3_copy( part->obj.rb.w, player_dead.w_lpf );
137 }
138
139 static void player__dead_animator_exchange( bitpack_ctx *ctx, void *data ){
140 struct player_dead_animator *animator = data;
141
142 for( u32 i=0; i<localplayer.playeravatar->sk.bone_count; i ++ ){
143 bitpack_qv3f( ctx, 24, -1024.0f, 1024.0f, animator->transforms[i].co );
144 bitpack_qquat( ctx, animator->transforms[i].q );
145 }
146 }
147
148 #endif /* PLAYER_DEAD_C */