]> isoptera.lcsc.edu Git - engine26/.git/commitdiff
Collisions work, but bounce is broken
authorSeth Long <sslong@lcsc.edu>
Tue, 27 Oct 2026 17:38:14 +0000 (10:38 -0700)
committerSeth Long <sslong@lcsc.edu>
Tue, 27 Oct 2026 17:38:14 +0000 (10:38 -0700)
load_level.cpp
main.cpp
misc_objects.h
projectile.h

index 6576018def9246655d82c3a5fe9f1e972636922e..59f3da3295c68376ce49900678c1bcc7380a7631 100644 (file)
@@ -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){
index 18147ae610eca3927aba11a8b232b2516b5d8bea..3bcc6f686dedcedfcd758163677482cc8826fd5e 100644 (file)
--- 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));     
        }
index 15dbd84f171d2f1a7b2bb8ab81e53248d9069272..a3d37ffb65da77d9b2486cc56ca90ce263c1a564 100644 (file)
@@ -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 {
index fb157c684114de19b86b5c8d37f6b59a9b4eb2b6..3746db258f54fab86029c8ceff0c8f1144f03d4a 100644 (file)
@@ -4,6 +4,7 @@
 
 class Projectile : public GameObject {
 public:
+       constexpr static float proj_size = .6;
        vector<vec3> velocities;
        vector<float> 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);
+       };
 };