#include "cute_c2_ext.h" #include "raylib.h" #include "raymath.h" #include const int screenWidth = 800; const int screenHeight = 450; typedef struct Shape { Vector2 pos; Vector2 center; Vector2 vel; C2_TYPE type; union { c2v boxDim; c2Circle circle; c2Poly poly; } shape; Color colour; } Shape; void DrawShape(const Shape* shape) { switch (shape->type) { case C2_TYPE_AABB: DrawRectangle( shape->center.x - shape->shape.boxDim.x / 2, shape->center.y - shape->shape.boxDim.y / 2, shape->shape.boxDim.x, shape->shape.boxDim.y, shape->colour ); break; case C2_TYPE_CIRCLE: DrawCircle( shape->center.x, shape->center.y, shape->shape.circle.r, shape->colour ); break; case C2_TYPE_POLY: for (int i = 0; i < shape->shape.poly.count; ++i) { int next = (i + 1 + shape->shape.poly.count) % shape->shape.poly.count; DrawLineV( (Vector2){shape->shape.poly.verts[i].x, shape->shape.poly.verts[i].y}, (Vector2){shape->shape.poly.verts[next].x, shape->shape.poly.verts[next].y}, shape->colour ); } break; default: break; } } struct TOIInfo { double toi; c2v contact; c2v normal; }; void draw_toi_info(const struct TOIInfo* info) { if (info->toi != INFINITY) { DrawCircle(info->contact.x, info->contact.y, 4, GREEN); DrawLineEx( (Vector2){info->contact.x, info->contact.y}, (Vector2){ info->contact.x + info->normal.x * 12, info->contact.y + info->normal.y * 12 }, 2, GREEN ); } } int main(void) { InitWindow(screenWidth, screenHeight, "raylib"); SetTargetFPS(60); Shape A = { .pos = {300,150}, .center = {300,150}, .type = C2_TYPE_AABB, .shape.boxDim = {32, 32}, .colour = BLUE, }; Shape B = { .pos = {400,300}, .center = {400,300}, .type = C2_TYPE_AABB, .shape.boxDim = {256, 64}, .colour = RED, }; //Shape C = { // .pos = {200,250}, // .center = {250,266.666}, // .type = C2_TYPE_POLY, // .shape.poly = { // .count = 3, // .verts = { // {0,0}, // {100, 0}, // {50,50}, // }, // }, // .colour = RED, //}; //c2MakePoly(&C.shape.poly); //for (int i = 0; i < C.shape.poly.count; ++i) { // C.shape.poly.verts[i].x += C.pos.x; // C.shape.poly.verts[i].y += C.pos.y; //} while (!WindowShouldClose()) { float frame_time = GetFrameTime(); Vector2 move_dir = {0}; if (IsKeyDown(KEY_LEFT)) { move_dir.x += -1; } if (IsKeyDown(KEY_RIGHT)) { move_dir.x += 1; } if (IsKeyDown(KEY_UP)) { move_dir.y += -1; } if (IsKeyDown(KEY_DOWN)) { move_dir.y += 1; } move_dir = Vector2Normalize(move_dir); A.vel = Vector2Scale(move_dir, 200); c2AABB Abox= { {A.center.x - A.shape.boxDim.x / 2, A.center.y - A.shape.boxDim.y / 2}, {A.center.x + A.shape.boxDim.x / 2, A.center.y + A.shape.boxDim.y / 2}, }; c2AABB Bbox= { {B.center.x - B.shape.boxDim.x / 2, B.center.y - B.shape.boxDim.y / 2}, {B.center.x + B.shape.boxDim.x / 2, B.center.y + B.shape.boxDim.y / 2}, }; struct TOIInfo toi0 = {0}; toi0.toi = AABBToAABBTOI( Abox, (c2v){A.vel.x *frame_time, A.vel.y * frame_time}, Bbox, (c2v){0,0}, &toi0.normal, &toi0.contact ); A.pos = Vector2Add( A.pos , Vector2Scale(A.vel, frame_time) ); A.center = A.pos; BeginDrawing(); ClearBackground(RAYWHITE); DrawShape(&B); if (toi0.toi <= 1 && toi0.toi > 0) { draw_toi_info(&toi0); } DrawShape(&A); EndDrawing(); } return 0; }