-
Notifications
You must be signed in to change notification settings - Fork 1
/
joints.c
180 lines (156 loc) · 5.45 KB
/
joints.c
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
#include "joints.h"
#include "raymath.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
Body* init_body(int N, float link_length, float root_x, float root_y){
Body *body = (Body *)malloc(sizeof(Body));
body->N = N;
body->total_length = 0;
body->link_length = link_length;
body->links_lengths = (float *)calloc(N - 1, sizeof(float));
body->total_length = link_length * (N - 1);
body->current_length = body->total_length;
body->target_length = link_length * (N - 1);
body->target = (Vector2){root_x, body->total_length};
body->final_target = (Vector2){root_x, body->total_length};
body->angle_limit = PI;
body->root_pos = (Vector2){root_x, root_y};
body->joints = (Joint *)calloc(N, sizeof(Joint));
body->joints[0].pos = body->root_pos;
for (int i = 1; i<N;++i){
body->links_lengths[i-1] = link_length;
body->joints[i].pos = (Vector2){root_x, root_y - link_length * i};
}
#ifdef DEBUG
for (int i = 0; i<N;++i){
printf("x %.2f, y %.2f\n", body->joints[i].pos.x, body->joints[i].pos.y);
}
#endif
return body;
}
void set_body_root(Body* body, Vector2 new_pos){
Vector2 offset = Vector2Subtract(new_pos, body->root_pos);
body->root_pos = new_pos;
set_body_target(body, Vector2Add(body->final_target, offset));
}
void set_body_target(Body* body, Vector2 new_target){
body->final_target = new_target;
}
void set_length_target(Body* body, float new_length){
body->target_length = Clamp(new_length, 0, body->total_length);
}
void _update_body_length(Body* body){
body->current_length = Lerp(body->current_length, body->target_length, 0.2);
body->N = (int)(body->current_length / body->link_length);
for (int i=0; i< body->N; ++i)
body->links_lengths[i] = body->link_length;
float remainder = body->current_length - body->N * body->link_length;
if (remainder >= 1) {
body->links_lengths[body->N] = remainder;
body->N += 1;
}
body->N += 1;
}
Vector2 _get_new_pos(Vector2 p1, Vector2 p2, float length){
float dist = Vector2Distance(p1, p2);
// Ensure it's not zero. Due to integer math, this is possible
// Just set it to 1 and let it settle
dist = (dist == 0) ? 1 : dist;
float lambda = length / dist;
return Vector2Add(
Vector2Scale(p2,1-lambda),
Vector2Scale(p1, lambda)
);
}
Vector2 _limit_new_pos(Vector2 a, Vector2 b, Vector2 c, float length, float angle){
Vector2 d_hat = Vector2Normalize(Vector2Subtract(c, b));
Vector2 e_hat = Vector2Normalize(Vector2Subtract(b, a));
Vector2 f_hat = (Vector2){-e_hat.y, e_hat.x};
if (Vector2DotProduct(d_hat, e_hat) > cos(angle)){
return _get_new_pos(c, b, length);
}
float lambda = length * cos(angle);
float mu = length * sin(angle);
if (Vector2DotProduct(d_hat, f_hat) < 0)
mu *= -1;
Vector2 v = Vector2Add(Vector2Scale(e_hat, lambda), Vector2Scale(f_hat, mu));
return Vector2Add(b, v);
}
void update_body(Body *body){
_update_body_length(body);
body->target = Vector2Lerp(body->target, body->final_target, 0.2);
// Check distance
float dist = Vector2Distance(body->joints[0].pos, body->target);
if (dist >= body->current_length){
for(int i = 0; i< body->N-1; ++i){
body->joints[i+1].pos = _get_new_pos(
body->target,
body->joints[i].pos,
body->links_lengths[i]
);
}
return;
}
// Perform FABRIK
double error = 1;
int iterations = 0;
Vector2 prev_pos;
while (error > 1e-2 && iterations < 100){
++iterations;
prev_pos = body->joints[body->N-1].pos;
// Backward Pass
body->joints[body->N-1].pos = body->target;
body->joints[body->N-2].pos = _get_new_pos(
body->joints[body->N-2].pos,
body->joints[body->N-1].pos,
body->links_lengths[body->N-2]
);
for(int i = body->N - 3; i >= 0; --i){
// Do regular calc for the one after the end
body->joints[i].pos = _limit_new_pos(
body->joints[i+2].pos,
body->joints[i+1].pos,
body->joints[i].pos,
body->links_lengths[i],
body->angle_limit
);
}
// Forward Pass
body->joints[0].pos = body->root_pos;
body->joints[1].pos = _get_new_pos(
body->joints[1].pos,
body->joints[0].pos,
body->links_lengths[1]
);
for(int i = 2; i < body->N; ++i){
body->joints[i].pos = _limit_new_pos(
body->joints[i-2].pos,
body->joints[i-1].pos,
body->joints[i].pos,
body->links_lengths[i-1],
body->angle_limit
);
}
error = Vector2Distance(prev_pos, body->joints[body->N-1].pos);
}
}
void draw_body(Body* body){
DrawCircleV(body->joints[0].pos, 5, BLACK);
DrawLineEx(body->joints[0].pos, body->joints[1].pos, body->N, BLACK);
for(int i = 1; i < body->N-1; ++i){
DrawLineEx(body->joints[i].pos, body->joints[i+1].pos, body->N - i, BLACK);
#ifdef DEBUG
DrawCircleV(body->joints[i].pos, 5, BLUE);
#else
DrawCircleV(body->joints[i].pos,(int)((body->N - i) / 2), BLACK);
#endif
}
#ifdef DEBUG
DrawCircleV(body->target, 3, RED);
#endif
}
void free_body(Body *body){
free(body->joints);
free(body->links_lengths);
}