From cd263f0dd5613c151d46c7460e4fe77984c2dc6b Mon Sep 17 00:00:00 2001 From: En Yi Date: Sat, 23 Nov 2019 17:34:09 +0800 Subject: [PATCH] Begin squish square --- include/header.h | 28 +++++++++++------- main.c | 11 ++++++- obj/kinematics.c | 2 +- obj/squishy.c | 77 +++++++++++++++++++++++++++++++++++++++++++++--- utilities/math.c | 13 -------- 5 files changed, 102 insertions(+), 29 deletions(-) diff --git a/include/header.h b/include/header.h index f0fa012..2c5458e 100644 --- a/include/header.h +++ b/include/header.h @@ -1,7 +1,7 @@ #include #include -#define BEZIER_POINTS 25 +#define BEZIER_POINTS 10 struct kinematic_obj { @@ -12,13 +12,20 @@ struct kinematic_obj struct squishy_square { - Vector2 pos; - double width; - double height; - double top_handle; - double bott_handle; - double left_handle; - double right_handle; + Rectangle *rect; + Color color; + Vector2 topleft; + Vector2 topright; + Vector2 bottomleft; + Vector2 bottomright; + Vector2 top_handle; + Vector2 bottom_handle; + Vector2 left_handle; + Vector2 right_handle; + double top_offset; + double bottom_offset; + double left_offset; + double right_offset; }; // Placeholder collision checking structure. Use linked list for now @@ -41,9 +48,10 @@ bool place_meeting(struct kinematic_obj *obj, Vector2 dir); long mag(Vector2 vec); int sign(float val); Vector2 dir(Vector2 vec); -Vector2* three_point_beizer(Vector2 start, Vector2 mid, Vector2 end); // Linked list, linked_list.c // Squishy Square functions, squishy.c -void draw_squishy(); \ No newline at end of file +struct squishy_square init_squishy_square(Rectangle *rect, Color color); +void update_squishy(struct squishy_square *square); +void draw_squishy(struct squishy_square *square); diff --git a/main.c b/main.c index 0976f20..2369c34 100644 --- a/main.c +++ b/main.c @@ -43,6 +43,7 @@ int main() struct kinematic_obj tile = init_kinematic_obj(900, 100); set_position(&player, 400, 300); set_position(&tile, -50, 380); + struct squishy_square sqr = init_squishy_square(&player.rect, RED); // TODO: get a linked list implementation struct obj_node tile_node = {.obj=&tile, .next=NULL}; @@ -71,6 +72,12 @@ int main() if (IsKeyDown(KEY_SPACE) && place_meeting(&player, (Vector2){0,1})) accel.y -= JUMP_ACCEL; move(&player, accel); + + update_squishy(&sqr); + Vector2 center = (Vector2){ + .x = (sqr.topleft.x + sqr.topright.x)/2, + .y = (sqr.topleft.y + sqr.bottomleft.y)/2 + }; //---------------------------------------------------------------------------------- // Draw @@ -85,7 +92,9 @@ int main() DrawRectangleRec(current->obj->rect, current->obj->color); current = current->next; } - DrawFPS(100, 100); + DrawTriangle((Vector2){0,0}, + (Vector2){screenWidth, screenHeight}, (Vector2){50,0}, BLACK); + draw_squishy(&sqr); EndMode2D(); EndDrawing(); diff --git a/obj/kinematics.c b/obj/kinematics.c index e73f0c9..5af13a7 100644 --- a/obj/kinematics.c +++ b/obj/kinematics.c @@ -17,7 +17,7 @@ void set_position(struct kinematic_obj *obj, int x, int y){ void move(struct kinematic_obj *obj, Vector2 acceleration){ // Use Euler method for moving - float delta = 1.0/(float)GetFPS(); + double delta = 1.0/60.0; obj->velocity.x += acceleration.x * delta; obj->velocity.y += acceleration.y * delta; obj->rect.x += obj->velocity.x * delta; diff --git a/obj/squishy.c b/obj/squishy.c index 91c477d..d8e6bf9 100644 --- a/obj/squishy.c +++ b/obj/squishy.c @@ -1,6 +1,75 @@ #include "header.h" -void draw_squishy(struct squishy_square square){ - - //DrawTriangleFan(); -} \ No newline at end of file +void three_point_beizerfan(Vector2 start, Vector2 mid, Vector2 end, Vector2* arr); + +struct squishy_square init_squishy_square(Rectangle *rect, Color color){ + struct squishy_square sqr = { + .rect = rect, + .color = color, + .topleft = (Vector2){0,0}, + .topright= (Vector2){0,0}, + .bottomleft = (Vector2){0,0}, + .bottomright = (Vector2){0,0}, + .top_handle = (Vector2){0,0}, + .bottom_handle = (Vector2){0,0}, + .left_handle = (Vector2){0,0}, + .right_handle = (Vector2){0,0}, + .top_offset = 0.0, + .bottom_offset = 0.0, + .left_offset = 0.0, + .right_offset = 0.0 + }; + return sqr; +} + +void update_squishy(struct squishy_square *square){ + square->topleft.x = square->rect->x; + square->topleft.y = square->rect->y; + square->topright.x = square->rect->x + square->rect->width; + square->topright.y = square->rect->y; + square->bottomleft.x = square->rect->x; + square->bottomleft.y = square->rect->y + square->rect->height; + square->bottomright.x = square->rect->x + square->rect->width; + square->bottomright.y = square->rect->y + square->rect->height; + + square->top_handle.x = square->rect->x + square->rect->width / 2; + square->top_handle.y = square->rect->y; + square->bottom_handle.x = square->rect->x + square->rect->width / 2; + square->bottom_handle.y = square->rect->y + square->rect->height; + square->left_handle.x = square->rect->x; + square->left_handle.y = square->rect->y + square->rect->height / 2; + square->right_handle.x = square->rect->x + square->rect->width; + square->right_handle.y = square->rect->y+ square->rect->height / 2; +} + +void draw_squishy(struct squishy_square *square){ + Vector2 center = (Vector2){ + .x = (square->topleft.x + square->topright.x)/2, + .y = (square->topleft.y + square->bottomleft.y)/2 + }; + Vector2 vertices[BEZIER_POINTS+1]; + int i; + three_point_beizerfan(square->topleft, square->top_handle, square->topright, vertices); + for(i=0;icolor); + three_point_beizerfan(square->topright, square->right_handle, square->bottomright, vertices); + for(i=0;icolor); + three_point_beizerfan(square->bottomright, square->bottom_handle, square->bottomleft, vertices); + for(i=0;icolor); + three_point_beizerfan(square->bottomleft, square->left_handle, square->topleft, vertices); + for(i=0;icolor); +} + +void three_point_beizerfan(Vector2 start, Vector2 mid, Vector2 end, Vector2* arr){ + double t; + double t_prime; + for (int i=0;i<=BEZIER_POINTS;++i){ + t = i*1.0/BEZIER_POINTS; + t_prime = 1-t; + arr[i].x = t_prime*t_prime*start.x + 2*t_prime*t*mid.x + t*t*end.x; + arr[i].y = t_prime*t_prime*start.y + 2*t_prime*t*mid.y + t*t*end.y; + } +} diff --git a/utilities/math.c b/utilities/math.c index cea806b..cf49076 100644 --- a/utilities/math.c +++ b/utilities/math.c @@ -12,17 +12,4 @@ Vector2 dir(Vector2 vec){ int sign(float val){ return (val > 0)?1:-1; -} - -Vector2* three_point_beizer(Vector2 start, Vector2 mid, Vector2 end){ - Vector2 vertices[BEZIER_POINTS]; - double t; - double t_prime; - for (int i=0;i