]> git.localhorst.tv Git - blobs.git/blobdiff - src/world/world.cpp
aggression
[blobs.git] / src / world / world.cpp
index 49380ac8afdbee816cc6fc6658b2e2b6f78161f4..1758c5e431ea4ebfb862dde58b4290408b5dd0e5 100644 (file)
@@ -26,9 +26,6 @@
 #include <glm/gtx/io.hpp>
 #include <glm/gtx/transform.hpp>
 
-using blobs::G;
-using blobs::PI_2p0;
-
 using std::sin;
 using std::cos;
 using std::pow;
@@ -104,7 +101,7 @@ double Body::GravitationalParameter() const noexcept {
 
 double Body::OrbitalPeriod() const noexcept {
        if (parent) {
-               return PI_2p0 * sqrt(pow(orbit.SemiMajorAxis(), 3) / (G * (parent->Mass() + Mass())));
+               return PI * 2.0 * sqrt(pow(orbit.SemiMajorAxis(), 3) / (G * (parent->Mass() + Mass())));
        } else {
                return 0.0;
        }
@@ -114,7 +111,15 @@ double Body::RotationalPeriod() const noexcept {
        if (std::abs(angular) < std::numeric_limits<double>::epsilon()) {
                return std::numeric_limits<double>::infinity();
        } else {
-               return PI_2p0 * Inertia() / angular;
+               return PI * 2.0 * Inertia() / angular;
+       }
+}
+
+double Body::SphereOfInfluence() const noexcept {
+       if (HasParent()) {
+               return orbit.SemiMajorAxis() * std::pow(Mass() / Parent().Mass(), 2.0 / 5.0);
+       } else {
+               return std::numeric_limits<double>::infinity();
        }
 }
 
@@ -165,11 +170,11 @@ void Body::Tick(double dt) {
 void Body::Cache() noexcept {
        if (parent) {
                orbital =
-                       orbit.Matrix(PI_2p0 * (GetSimulation().Time() / OrbitalPeriod()))
+                       orbit.Matrix(PI * 2.0 * (GetSimulation().Time() / OrbitalPeriod()))
                        * glm::eulerAngleXY(axis_tilt.x, axis_tilt.y);
                inverse_orbital =
                        glm::eulerAngleYX(-axis_tilt.y, -axis_tilt.x)
-                       * orbit.InverseMatrix(PI_2p0 * (GetSimulation().Time() / OrbitalPeriod()));
+                       * orbit.InverseMatrix(PI * 2.0 * (GetSimulation().Time() / OrbitalPeriod()));
        } else {
                orbital = glm::eulerAngleXY(axis_tilt.x, axis_tilt.y);
                inverse_orbital = glm::eulerAngleYX(-axis_tilt.y, -axis_tilt.x);
@@ -187,13 +192,13 @@ void Body::CheckCollision() noexcept {
        collisions.clear();
        auto end = Creatures().end();
        for (auto i = Creatures().begin(); i != end; ++i) {
-               math::AABB i_box((*i)->CollisionBox());
+               math::AABB i_box((*i)->CollisionBounds());
                glm::dmat4 i_mat((*i)->CollisionTransform());
                for (auto j = (i + 1); j != end; ++j) {
                        glm::dvec3 diff((*i)->GetSituation().Position() - (*j)->GetSituation().Position());
                        double max_dist = ((*i)->Size() + (*j)->Size()) * 1.74;
-                       if (length2(diff) > max_dist * max_dist) continue;
-                       math::AABB j_box((*j)->CollisionBox());
+                       if (glm::length2(diff) > max_dist * max_dist) continue;
+                       math::AABB j_box((*j)->CollisionBounds());
                        glm::dmat4 j_mat((*j)->CollisionTransform());
                        glm::dvec3 normal;
                        double depth;
@@ -203,11 +208,12 @@ void Body::CheckCollision() noexcept {
                }
        }
        for (auto &c : collisions) {
+               c.A().OnCollide(c.B());
+               c.B().OnCollide(c.A());
                c.A().GetSituation().Move(c.Normal() * (c.Depth() * -0.5));
                c.B().GetSituation().Move(c.Normal() * (c.Depth() * 0.5));
-               c.A().GetSituation().Accelerate(c.Normal() * -dot(c.Normal(), c.AVel()));
-               c.B().GetSituation().Accelerate(c.Normal() * -dot(c.Normal(), c.BVel()));
-               // TODO: notify participants so they can be annoyed
+               c.A().GetSituation().Accelerate(c.Normal() * -glm::dot(c.Normal(), c.AVel()));
+               c.B().GetSituation().Accelerate(c.Normal() * -glm::dot(c.Normal(), c.BVel()));
        }
 }
 
@@ -317,7 +323,7 @@ double mean2eccentric(double M, double e) {
        for (int i = 0; i < 100; ++i) {
                double dE = (E - e * sin(E) - M) / (1 - e * cos(E));
                E -= dE;
-               if (abs(dE) < 1.0e-6) break;
+               if (std::abs(dE) < 1.0e-6) break;
        }
        return E;
 }
@@ -358,8 +364,8 @@ Planet::~Planet() {
 namespace {
 /// map p onto cube, s gives the surface, u and v the position in [-1,1]
 void cubemap(const glm::dvec3 &p, int &s, double &u, double &v) noexcept {
-       const glm::dvec3 p_abs(abs(p));
-       const glm::bvec3 p_pos(greaterThan(p, glm::dvec3(0.0)));
+       const glm::dvec3 p_abs(glm::abs(p));
+       const glm::bvec3 p_pos(glm::greaterThan(p, glm::dvec3(0.0)));
        double max_axis = 0.0;
 
        if (p_pos.x && p_abs.x >= p_abs.y && p_abs.x >= p_abs.z) {
@@ -455,7 +461,7 @@ const TileType &Planet::TypeAt(int srf, int x, int y) const noexcept {
 glm::dvec3 Planet::TileCenter(int srf, int x, int y, double e) const noexcept {
        double u = (double(x) - Radius() + 0.5) / Radius();
        double v = (double(y) - Radius() + 0.5) / Radius();
-       return normalize(cubeunmap(srf, u, v)) * (Radius() + e);
+       return glm::normalize(cubeunmap(srf, u, v)) * (Radius() + e);
 }
 
 void Planet::BuildVAO(const Set<TileType> &ts) {
@@ -479,43 +485,43 @@ void Planet::BuildVAO(const Set<TileType> &ts) {
                for (int index = 0, surface = 0; surface < 6; ++surface) {
                        for (int y = 0; y < sidelength; ++y) {
                                for (int x = 0; x < sidelength; ++x, ++index) {
-                                       glm::vec3 pos[5];
-                                       pos[0][(surface + 0) % 3] = x + 0 - offset;
-                                       pos[0][(surface + 1) % 3] = y + 0 - offset;
-                                       pos[0][(surface + 2) % 3] = surface < 3 ? offset : -offset;
-                                       pos[1][(surface + 0) % 3] = x + 0 - offset;
-                                       pos[1][(surface + 1) % 3] = y + 1 - offset;
-                                       pos[1][(surface + 2) % 3] = surface < 3 ? offset : -offset;
-                                       pos[2][(surface + 0) % 3] = x + 1 - offset;
-                                       pos[2][(surface + 1) % 3] = y + 0 - offset;
-                                       pos[2][(surface + 2) % 3] = surface < 3 ? offset : -offset;
-                                       pos[3][(surface + 0) % 3] = x + 1 - offset;
-                                       pos[3][(surface + 1) % 3] = y + 1 - offset;
-                                       pos[3][(surface + 2) % 3] = surface < 3 ? offset : -offset;
+                                       glm::vec3 pos[4];
+                                       pos[0][(surface + 0) % 3] = float(x + 0) - offset;
+                                       pos[0][(surface + 1) % 3] = float(y + 0) - offset;
+                                       pos[0][(surface + 2) % 3] = offset;
+                                       pos[1][(surface + 0) % 3] = float(x + 0) - offset;
+                                       pos[1][(surface + 1) % 3] = float(y + 1) - offset;
+                                       pos[1][(surface + 2) % 3] = offset;
+                                       pos[2][(surface + 0) % 3] = float(x + 1) - offset;
+                                       pos[2][(surface + 1) % 3] = float(y + 0) - offset;
+                                       pos[2][(surface + 2) % 3] = offset;
+                                       pos[3][(surface + 0) % 3] = float(x + 1) - offset;
+                                       pos[3][(surface + 1) % 3] = float(y + 1) - offset;
+                                       pos[3][(surface + 2) % 3] = offset;
 
                                        float tex = ts[TileAt(surface, x, y).type].texture;
                                        const float tex_v_begin = surface < 3 ? 1.0f : 0.0f;
                                        const float tex_v_end = surface < 3 ? 0.0f : 1.0f;
 
-                                       attrib[4 * index + 0].position = normalize(pos[0]) * offset;
+                                       attrib[4 * index + 0].position = glm::normalize(pos[0]) * (surface < 3 ? offset : -offset);
                                        attrib[4 * index + 0].normal = pos[0];
                                        attrib[4 * index + 0].tex_coord[0] = 0.0f;
                                        attrib[4 * index + 0].tex_coord[1] = tex_v_begin;
                                        attrib[4 * index + 0].tex_coord[2] = tex;
 
-                                       attrib[4 * index + 1].position = normalize(pos[1]) * offset;
+                                       attrib[4 * index + 1].position = glm::normalize(pos[1]) * (surface < 3 ? offset : -offset);
                                        attrib[4 * index + 1].normal = pos[1];
                                        attrib[4 * index + 1].tex_coord[0] = 0.0f;
                                        attrib[4 * index + 1].tex_coord[1] = tex_v_end;
                                        attrib[4 * index + 1].tex_coord[2] = tex;
 
-                                       attrib[4 * index + 2].position = normalize(pos[2]) * offset;
+                                       attrib[4 * index + 2].position = glm::normalize(pos[2]) * (surface < 3 ? offset : -offset);
                                        attrib[4 * index + 2].normal = pos[2];
                                        attrib[4 * index + 2].tex_coord[0] = 1.0f;
                                        attrib[4 * index + 2].tex_coord[1] = tex_v_begin;
                                        attrib[4 * index + 2].tex_coord[2] = tex;
 
-                                       attrib[4 * index + 3].position = normalize(pos[3]) * offset;
+                                       attrib[4 * index + 3].position = glm::normalize(pos[3]) * (surface < 3 ? offset : -offset);
                                        attrib[4 * index + 3].normal = pos[3];
                                        attrib[4 * index + 3].tex_coord[0] = 1.0f;
                                        attrib[4 * index + 3].tex_coord[1] = tex_v_end;
@@ -607,7 +613,7 @@ void GenerateEarthlike(const Set<TileType> &tiles, Planet &p) noexcept {
                                }
                                float elevation = math::OctaveNoise(
                                        elevation_gen,
-                                       to_tile / p.Radius(),
+                                       glm::vec3(to_tile / p.Radius()),
                                        3,   // octaves
                                        0.5, // persistence
                                        5 / p.Radius(), // frequency
@@ -616,7 +622,7 @@ void GenerateEarthlike(const Set<TileType> &tiles, Planet &p) noexcept {
                                );
                                float variation = math::OctaveNoise(
                                        variation_gen,
-                                       to_tile / p.Radius(),
+                                       glm::vec3(to_tile / p.Radius()),
                                        3,   // octaves
                                        0.5, // persistence
                                        16 / p.Radius(), // frequency
@@ -689,7 +695,9 @@ void GenerateTest(const Set<TileType> &tiles, Planet &p) noexcept {
 
 
 Sun::Sun()
-: Body() {
+: Body()
+, color(1.0)
+, luminosity(1.0) {
 }
 
 Sun::~Sun() {