diff --git a/engine/geometry/geometry.c b/engine/geometry/geometry.c index 14ec987..e6fb6df 100644 --- a/engine/geometry/geometry.c +++ b/engine/geometry/geometry.c @@ -88,7 +88,7 @@ double PolyToPolyTOI(const c2Poly* pA, const c2x* ax_ptr, c2v vA, const c2Poly* // Change to check if difference is within some threshold // This is fine because the value should fall between 0 and 1 if(v_t - t < -1e-6){ - if(v_t<0) return INFINITY; //point collided in the past, therefore this is a degenerate case + //if(v_t<0) return INFINITY; //point collided in the past, therefore this is a degenerate case t = v_t; n = c2Neg(B.norms[j]); @@ -106,7 +106,7 @@ double PolyToPolyTOI(const c2Poly* pA, const c2x* ax_ptr, c2v vA, const c2Poly* if(leading_verts_b[i]){ double v_t = PointToSegmentTOI(B.verts[i], vB2A, A.verts[j], A.verts[(j+1)%A.count]); if(v_t - t < -1e-6){ - if(v_t<0) return INFINITY; //point collided in the past, therefore this is a degenerate case + //if(v_t<0) return INFINITY; //point collided in the past, therefore this is a degenerate case t = v_t; n = A.norms[j]; @@ -154,7 +154,7 @@ double PolyToCircleTOI(const c2Poly* pA, const c2x* ax_ptr, c2v vA, c2Circle cB, double v_t = PointToCircleTOI(A.verts[i], vA2B, cB.p, cB.r); if(v_t <= t){ - if(v_t<0) return INFINITY; //point collided in the past, therefore this is a degenerate case + //if(v_t<0) return INFINITY; //point collided in the past, therefore this is a degenerate case t = v_t; n = c2Sub( @@ -172,7 +172,7 @@ double PolyToCircleTOI(const c2Poly* pA, const c2x* ax_ptr, c2v vA, c2Circle cB, c2v offset = c2Mulvs(A.norms[i], cB.r); double v_t = PointToSegmentTOI(cB.p, vB2A, c2Add(A.verts[i], offset), c2Add(A.verts[(i+1)%A.count], offset)); if(v_t <= t){ - if(v_t<0) return INFINITY; //point collided in the past, therefore this is a degenerate case + //if(v_t<0) return INFINITY; //point collided in the past, therefore this is a degenerate case t = v_t; n = A.norms[i]; diff --git a/engine/tests/geometry/samples/CMakeLists.txt b/engine/tests/geometry/samples/CMakeLists.txt index 0459c92..023bd8a 100644 --- a/engine/tests/geometry/samples/CMakeLists.txt +++ b/engine/tests/geometry/samples/CMakeLists.txt @@ -4,5 +4,15 @@ target_link_libraries(collisionSample PRIVATE ) set_target_properties(collisionSample PROPERTIES - RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/manual) + RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/manual +) + +add_executable(collisionSample2 collision_sample2.c) +target_link_libraries(collisionSample2 PRIVATE + engine +) +set_target_properties(collisionSample2 + PROPERTIES + RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/manual +) diff --git a/engine/tests/geometry/samples/collision_sample2.c b/engine/tests/geometry/samples/collision_sample2.c new file mode 100644 index 0000000..c21a326 --- /dev/null +++ b/engine/tests/geometry/samples/collision_sample2.c @@ -0,0 +1,177 @@ +#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; +}