From: Seth Long Date: Thu, 5 Nov 2026 18:36:05 +0000 (-0800) Subject: After class on mov5 X-Git-Url: https://isoptera.lcsc.edu/gitweb/?a=commitdiff_plain;h=39959b23ea51a0c73138b5e65cd49c0a8dff2f6b;p=engine26%2F.git After class on mov5 --- diff --git a/cat.h b/cat.h index 3260f7d..795c88b 100644 --- a/cat.h +++ b/cat.h @@ -14,9 +14,9 @@ public: } void movement(float time) { for(size_t i = 0; i < models.size(); i++) { - vec3 mov = vec3(0, 0, -.01); + vec3 mov = vec3(0, 0, -.01 * time); if(!direction) - mov = vec3(0, 0, .01); + mov = vec3(0, 0, .01 * time); if(switchtime < 0) { direction = !direction; switchtime = 1000; diff --git a/explodingtarget.h b/explodingtarget.h index efa727d..ad8a566 100644 --- a/explodingtarget.h +++ b/explodingtarget.h @@ -4,7 +4,7 @@ class ExplodingTarget : public FloorPanel { public: float get_random(){ - return ((random() % 2000000) - 1000000.0f) / 500000.0f; + return ((rand() % 2000000) - 1000000.0f) / 500000.0f; } void report_collision(GameObject *other, size_t other_instance, size_t our_instance) override { diff --git a/load_level.cpp b/load_level.cpp index c2c5f8f..d88e354 100644 --- a/load_level.cpp +++ b/load_level.cpp @@ -6,8 +6,8 @@ void load_level(){ FloorPanel *floor = (FloorPanel*)add_object(new FloorPanel, "The Floor", true); floor->texture_file = "textures/stones.png"; - for(float i = -45; i < 45; i+= 5) - for(float j = -45; j < 45; j+= 5) + for(float i = -90; i < 90; i+= 5) + for(float j = -90; j < 90; j+= 5) for(float k = -2; k <= -2; k += 1) floor->add_location_size(vec3(i, k, j), vec3(5, 1, 5)); diff --git a/main.cpp b/main.cpp index 621fbfb..bd5c12c 100644 --- a/main.cpp +++ b/main.cpp @@ -45,21 +45,30 @@ void collision_check(){ for(size_t mo_inst = 0; mo_inst < mo->models.size(); mo_inst++){ for(auto so : solid_objects){ for(size_t so_inst = 0; so_inst < so->models.size(); so_inst++){ - if(mo_inst == so_inst && mo == so) - continue; - vec3 moinst_center = mo->get_center(mo_inst); - vec3 soinst_center = so->get_center(so_inst); - if(mo->fast_check_distance(mo_inst) + so->fast_check_distance(so_inst) - < distance(moinst_center, soinst_center)) - continue; - 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); + /* Check and see if mo_inst is still valid */ + /* Add a mutex on the moving object so it can't be moved while we're checking */ + /* What if we just make sure the instance is valid inside the object? */ + try { + if(mo_inst == so_inst && mo == so) + continue; + vec3 moinst_center = mo->get_center(mo_inst); + vec3 soinst_center = so->get_center(so_inst); + if(mo->fast_check_distance(mo_inst) + so->fast_check_distance(so_inst) + < distance(moinst_center, soinst_center)) + continue; + 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); + } + } catch (const runtime_error& e){ + printf("Caught collision exception, will ignore and continue\n"); + return; } + } } } @@ -222,10 +231,11 @@ void movement_function() { double end_time = glfwGetTime(); movement_count++; - 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)); + } } diff --git a/projectile.h b/projectile.h index 3efb817..907c662 100644 --- a/projectile.h +++ b/projectile.h @@ -15,7 +15,8 @@ public: models.push_back(translate(mat4(1.0), start)); models[models.size() - 1] = scale(models[models.size() - 1], vec3(proj_size, proj_size, proj_size)); velocities.push_back(vel * 0.1f); - times_to_live.push_back(20000); + times_to_live.push_back(2000); + specials.push_back(0); } void add_location_size(){ /* Defaults to "launched by player" */ @@ -33,6 +34,7 @@ public: models.erase(models.begin() + i); velocities.erase(velocities.begin() + i); times_to_live.erase(times_to_live.begin() + i); + specials.erase(specials.begin() + i); } } } @@ -40,22 +42,30 @@ public: float fast_check_distance(size_t instance) {return proj_size;} vec3 closest_point(vec3 other, size_t instance) { + if(instance >= models.size()) + throw runtime_error("Closest point doesn't exist on nonexistent instance"); vec3 cmpos = get_center(instance); if(distance(cmpos, other) < proj_size) // Contains sqrt return other; return cmpos - (proj_size * normalize(cmpos - other)); } bool inside_instance(vec3 other, size_t instance) { + if(instance >= models.size()) + throw runtime_error("Nothing can be inside a nonexistent instance"); vec3 cmpos = get_center(instance); if(distance(cmpos, other) < proj_size) // Contains sqrt return true; return false; } vec3 get_center(size_t instance) { + if(instance >= models.size()) + throw runtime_error("Attempt to get center of nonexistent instance"); 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) { + if(our_instance >= models.size()) + throw runtime_error("Attempt to report collision with nonexistent instance"); vec3 our_center = get_center(our_instance); vec3 other_cp = other->closest_point(our_center, other_instance); /* Now figure out what shape the other object is! */ diff --git a/water_tank.h b/water_tank.h index 6e29fff..910c410 100644 --- a/water_tank.h +++ b/water_tank.h @@ -17,6 +17,7 @@ public: positions.push_back({0.0f, 1.0f, location, size, 0}); models.push_back(translate(mat4(1.0), location)); models[models.size() - 1] = scale(models[models.size() - 1], size); + specials.push_back(0); } void movement(float time); float get_topbottom(size_t instance, bool) override;