]> isoptera.lcsc.edu Git - engine26/.git/commitdiff
After class on mov5
authorSeth Long <sslong@lcsc.edu>
Thu, 5 Nov 2026 18:36:05 +0000 (10:36 -0800)
committerSeth Long <sslong@lcsc.edu>
Thu, 5 Nov 2026 18:36:05 +0000 (10:36 -0800)
cat.h
explodingtarget.h
load_level.cpp
main.cpp
projectile.h
water_tank.h

diff --git a/cat.h b/cat.h
index 3260f7d3430cd6f1fbc77806bf46c5107274ab4a..795c88b4ff84dd7af21a15a068be37a3918c1413 100644 (file)
--- 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;
index efa727d1fea5c3d25859e96fc4481d5152343796..ad8a566d2e50a1ad84f483c6720f51f078edb9b1 100644 (file)
@@ -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 {
index c2c5f8f3550048f845289397381311fc1ca0039e..d88e354e30320033ff3f57c5b90b4324a881543f 100644 (file)
@@ -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));
 
index 621fbfbec9d08a5599d09a58af4aabfb515218e9..bd5c12c49bb5f5cabdb6ea120749ae1df40ce9e3 100644 (file)
--- 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));       
+
        }
 }
 
index 3efb817c3ee6f79f327b981239e11f5dfc8afbea..907c6620bcf465d8588c36c75ffa082fd5853bb7 100644 (file)
@@ -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! */
index 6e29fff82aadd1fa2c8eb67c2fce29143877e196..910c4109ced072b83f6f131af9bc4b90e94bcec3 100644 (file)
@@ -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;