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){
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);
}
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));
}
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));
}
}
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 {
class Projectile : public GameObject {
public:
+ constexpr static float proj_size = .6;
vector<vec3> velocities;
vector<float> times_to_live;
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);
}
}
/* 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;
}
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);
+ };
};