Begin squish square

master
En Yi 2019-11-23 17:34:09 +08:00
parent a45ef24d5d
commit cd263f0dd5
5 changed files with 102 additions and 29 deletions

View File

@ -1,7 +1,7 @@
#include <raylib.h>
#include <stdio.h>
#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();
struct squishy_square init_squishy_square(Rectangle *rect, Color color);
void update_squishy(struct squishy_square *square);
void draw_squishy(struct squishy_square *square);

11
main.c
View File

@ -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();

View File

@ -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;

View File

@ -1,6 +1,75 @@
#include "header.h"
void draw_squishy(struct squishy_square square){
//DrawTriangleFan();
}
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;i<BEZIER_POINTS;++i)
DrawTriangle(vertices[i], center,vertices[i+1], square->color);
three_point_beizerfan(square->topright, square->right_handle, square->bottomright, vertices);
for(i=0;i<BEZIER_POINTS;++i)
DrawTriangle(vertices[i], center,vertices[i+1], square->color);
three_point_beizerfan(square->bottomright, square->bottom_handle, square->bottomleft, vertices);
for(i=0;i<BEZIER_POINTS;++i)
DrawTriangle(vertices[i], center,vertices[i+1], square->color);
three_point_beizerfan(square->bottomleft, square->left_handle, square->topleft, vertices);
for(i=0;i<BEZIER_POINTS;++i)
DrawTriangle(vertices[i], center,vertices[i+1],square->color);
}
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;
}
}

View File

@ -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<BEZIER_POINTS;++i){
t = i/BEZIER_POINTS;
t_prime = 1-t;
vertices[i].x = t_prime*t_prime*start.x + 2*t_prime*t*mid.x + t*t*end.x;
vertices[i].y = t_prime*t_prime*start.y + 2*t_prime*t*mid.y + t*t*end.y;
}
return vertices;
}