From 18369132cc79a6a1721a25ec0d730d239dcb4d91 Mon Sep 17 00:00:00 2001 From: Seth Long Date: Tue, 27 Oct 2026 10:38:14 -0700 Subject: [PATCH] Collisions work, but bounce is broken --- load_level.cpp | 2 +- main.cpp | 9 +++++++-- misc_objects.h | 25 +++++++++++++++++++++++++ projectile.h | 30 ++++++++++++++++++++++-------- 4 files changed, 55 insertions(+), 11 deletions(-) diff --git a/load_level.cpp b/load_level.cpp index 6576018..59f3da3 100644 --- a/load_level.cpp +++ b/load_level.cpp @@ -42,7 +42,7 @@ void load_level(){ Projectile *proj = (Projectile*)add_object(new Projectile, "Projectiles", false); proj->object_file = "objects/sphere.obj"; - proj->texture_file = "textures/brick.jpg"; + proj->texture_file = "textures/projectile.jpg"; } GameObject* add_object(GameObject *new_object, string name, bool solid){ diff --git a/main.cpp b/main.cpp index 18147ae..3bcc6f6 100644 --- a/main.cpp +++ b/main.cpp @@ -52,8 +52,11 @@ void collision_check(){ if(mo->fast_check_distance(mo_inst) + so->fast_check_distance(so_inst) < distance(moinst_center, soinst_center)) continue; - if(distance(mo->closest_point(soinst_center, mo_inst), so->closest_point(moinst_center, so_inst)) < 0.3){ + vec3 mo_cp = mo->closest_point(soinst_center, mo_inst); + vec3 so_cp = so->closest_point(moinst_center, so_inst); + if(mo->inside_instance(so_cp, mo_inst) || so->inside_instance(mo_cp, so_inst)){ // We have a collison! +// printf("Collision Found: (%f %f %f), (%f %f %f)\n", moinst_center.x, moinst_center.y, moinst_center.z, soinst_center.x, soinst_center.y, soinst_center.z); mo->report_collision(so, so_inst, mo_inst); so->report_collision(mo, mo_inst, so_inst); } @@ -181,7 +184,7 @@ void movement_function() { double end_time = glfwGetTime(); movement_count++; - double sleep_seconds = 0.01 - (end_time - start_time); + double sleep_seconds = 0.001 - (end_time - start_time); unsigned long sleep_microseconds = sleep_seconds * 1000000.0; this_thread::sleep_for(chrono::microseconds(sleep_microseconds)); } @@ -195,12 +198,14 @@ void stats_function() { g.movement_time = 1.0 / (double)movement_count; movement_count = 0; printf("FPS: %u cctime = %lf movement_time = %lf gmtime = %lf\n", g.last_fps, g.cctime, g.movement_time, g.gmtime); + /* for(auto proj : game_objects) { if(proj->name == "Projectiles") { printf("Projectile count: %lu\n", ((Projectile*)proj)->models.size()); break; } } + */ this_thread::sleep_for(chrono::milliseconds(1000)); } diff --git a/misc_objects.h b/misc_objects.h index 15dbd84..a3d37ff 100644 --- a/misc_objects.h +++ b/misc_objects.h @@ -22,6 +22,31 @@ public: } virtual void trigger_action(size_t instance, int action) override; ssize_t is_inside(vec3, float) override; + + /* Required for collision detection */ + float fast_check_distance(size_t instance) override { + return 20; + } + vec3 closest_point(vec3 other, size_t instance) override { + vec3 uloc = get_center(instance); + vec3 size = vec3(models[instance][0][0], models[instance][1][1], models[instance][2][2]); + vec3 cp; + cp.x = clamp(other.x, uloc.x - (size.x/2), uloc.x + (size.x/2)); + cp.y = clamp(other.y, uloc.y - (size.y/2), uloc.y + (size.y/2)); + cp.z = clamp(other.z, uloc.z - (size.z/2), uloc.z + (size.z/2)); + return cp; + } + vec3 get_center(size_t instance) override { + return vec3(models[instance][3][0], models[instance][3][1], models[instance][3][2]); + } + void report_collision(GameObject *other, size_t other_instance, size_t our_instance) override { +// models.erase(models.begin() + our_instance); + }; + bool inside_instance(vec3 other, size_t instance) override { + vec3 uloc = get_center(instance); + vec3 size = vec3(models[instance][0][0], models[instance][1][1], models[instance][2][2]); + return abs(other.x - uloc.x) <= size.x/2 && abs(other.y - uloc.y) <= size.y/2 && abs(other.z - uloc.z) <= size.z/2; + } }; class VanishBlock : public FloorPanel { diff --git a/projectile.h b/projectile.h index fb157c6..3746db2 100644 --- a/projectile.h +++ b/projectile.h @@ -4,6 +4,7 @@ class Projectile : public GameObject { public: + constexpr static float proj_size = .6; vector velocities; vector times_to_live; @@ -13,19 +14,19 @@ public: void add_location_size(){ /* Defaults to "launched by player" */ models.push_back(translate(mat4(1.0), g.player_position)); + models[models.size() - 1] = scale(models[models.size() - 1], vec3(proj_size, proj_size, proj_size)); float gdist = cos(g.elevation); float vx = gdist * cos(g.heading); float vz = gdist * sin(g.heading); float vy = sin(g.elevation); /* The number we multiply the vector by on the next line is the speed */ - velocities.push_back(vec3(vx, vy, vz) * 0.8f); - times_to_live.push_back(2000); + velocities.push_back(vec3(vx, vy, vz) * 0.01f); + times_to_live.push_back(200000); } void movement(float time) { for(size_t i = 0; i < models.size(); i++) { models[i] = translate(models[i], velocities[i]); times_to_live[i] -= time; - printf("times to live[i] = %f\n", times_to_live[i]); if(times_to_live[i] < 0){ models.erase(models.begin() + i); velocities.erase(velocities.begin() + i); @@ -34,16 +35,17 @@ public: } } /* Required for collision detection */ - float fast_check_distance(size_t instance) {return 1;} + float fast_check_distance(size_t instance) {return 0.2;} + vec3 closest_point(vec3 other, size_t instance) { vec3 cmpos = get_center(instance); - if(distance(cmpos, other) < 1) // Contains sqrt + if(distance(cmpos, other) < proj_size) // Contains sqrt return other; - return cmpos - normalize(cmpos - other); + return cmpos - (proj_size * normalize(cmpos - other)); } bool inside_instance(vec3 other, size_t instance) { vec3 cmpos = get_center(instance); - if(distance(cmpos, other) < 1) // Contains sqrt + if(distance(cmpos, other) < proj_size) // Contains sqrt return true; return false; } @@ -51,5 +53,17 @@ public: mat4 m = models[instance]; // The optimizer can deal with it return vec3(m[3][0], m[3][1], m[3][2]); } - void report_collision(GameObject *other, size_t other_instance, size_t our_instance) {}; + void report_collision(GameObject *other, size_t other_instance, size_t our_instance) { + vec3 our_center = get_center(our_instance); + vec3 other_cp = other->closest_point(our_center, other_instance); + if(other_cp == our_center) + printf("our_center is the closest point!\n"); + /* Now figure out what shape the other object is! */ + if(other_cp.x == our_center.x) + velocities[our_instance] *= vec3(-1, 1, 1); + if(other_cp.y == our_center.y) + velocities[our_instance] *= vec3(1, -1, 1); + if(other_cp.z == our_center.z) + velocities[our_instance] *= vec3(1, 1, -1); + }; }; -- 2.47.3