Add lerp to changing length

master
BeardedBread 2021-09-16 10:01:39 +08:00
parent 3d1a4e7cd8
commit 8d94b7ea7b
3 changed files with 15 additions and 7 deletions

View File

@ -12,7 +12,8 @@ Body* init_body(int N, float link_length, float root_x, float root_y){
body->link_length = link_length; body->link_length = link_length;
body->links_lengths = (float *)calloc(N - 1, sizeof(float)); body->links_lengths = (float *)calloc(N - 1, sizeof(float));
body->total_length = link_length * (N - 1); body->total_length = link_length * (N - 1);
body->current_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->target = (Vector2){root_x, body->total_length};
body->final_target = (Vector2){root_x, body->total_length}; body->final_target = (Vector2){root_x, body->total_length};
body->angle_limit = PI; body->angle_limit = PI;
@ -44,8 +45,12 @@ void set_body_target(Body* body, Vector2 new_target){
body->final_target = new_target; body->final_target = new_target;
} }
void set_current_length(Body* body, float new_length){ void set_length_target(Body* body, float new_length){
body->current_length = Clamp(new_length, 0, body->total_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); body->N = (int)(body->current_length / body->link_length);
for (int i=0; i< body->N; ++i) for (int i=0; i< body->N; ++i)
@ -91,6 +96,7 @@ Vector2 _limit_new_pos(Vector2 a, Vector2 b, Vector2 c, float length, float angl
} }
void update_body(Body *body){ void update_body(Body *body){
_update_body_length(body);
body->target = Vector2Lerp(body->target, body->final_target, 0.2); body->target = Vector2Lerp(body->target, body->final_target, 0.2);
// Check distance // Check distance

View File

@ -14,6 +14,7 @@ typedef struct _Body {
float link_length; float link_length;
float* links_lengths; float* links_lengths;
float current_length; float current_length;
float target_length;
Joint* joints; Joint* joints;
} Body; } Body;
@ -21,7 +22,8 @@ typedef struct _Body {
Body* init_body(int N, float link_length, float root_x, float root_y); Body* init_body(int N, float link_length, float root_x, float root_y);
void set_body_root(Body* body, Vector2 new_pos); void set_body_root(Body* body, Vector2 new_pos);
void set_body_target(Body* body, Vector2 new_target); void set_body_target(Body* body, Vector2 new_target);
void set_current_length(Body* body, float new_length); void set_length_target(Body* body, float new_length);
void _update_body_length(Body* body);
void update_body(Body* body); void update_body(Body* body);
void draw_body(Body* body); void draw_body(Body* body);
void free_body(Body* body); void free_body(Body* body);

6
main.c
View File

@ -6,16 +6,16 @@ int main(int argc, char** argv) {
const unsigned int scrnHeight = 600; const unsigned int scrnHeight = 600;
InitWindow(scrnWidth, scrnHeight, "FABRIK"); InitWindow(scrnWidth, scrnHeight, "FABRIK");
SetTargetFPS(30); SetTargetFPS(60);
Body* body = init_body(15, 20, 400, 500); Body* body = init_body(15, 20, 400, 500);
bool follow_mode = false; bool follow_mode = false;
while(!WindowShouldClose()){ while(!WindowShouldClose()){
if (IsKeyReleased(KEY_O)) if (IsKeyReleased(KEY_O))
set_current_length(body, body->current_length - 10); set_length_target(body, body->current_length - 30);
if (IsKeyReleased(KEY_P)) if (IsKeyReleased(KEY_P))
set_current_length(body, body->current_length + 10); set_length_target(body, body->current_length + 30);
if (IsKeyReleased(KEY_F)) if (IsKeyReleased(KEY_F))
follow_mode = !follow_mode; follow_mode = !follow_mode;