2 #include "CreatureCreatureCollision.hpp"
5 #include "Resource.hpp"
7 #include "Simulation.hpp"
10 #include "TileType.hpp"
12 #include "../app/Assets.hpp"
13 #include "../creature/Composition.hpp"
14 #include "../creature/Creature.hpp"
15 #include "../graphics/Viewport.hpp"
16 #include "../math/const.hpp"
17 #include "../math/geometry.hpp"
18 #include "../math/OctaveNoise.hpp"
19 #include "../math/SimplexNoise.hpp"
24 #include <glm/gtc/matrix_transform.hpp>
25 #include <glm/gtx/euler_angles.hpp>
26 #include <glm/gtx/io.hpp>
27 #include <glm/gtx/transform.hpp>
48 , surface_tilt(0.0, 0.0)
53 , inverse_orbital(1.0)
61 for (creature::Creature *c : creatures) {
66 void Body::SetSimulation(Simulation &s) noexcept {
68 for (auto child : children) {
69 child->SetSimulation(s);
73 void Body::SetParent(Body &p) {
78 parent->AddChild(*this);
81 void Body::UnsetParent() {
82 if (!HasParent()) return;
83 parent->RemoveChild(*this);
87 void Body::AddChild(Body &c) {
88 children.push_back(&c);
89 c.SetSimulation(*sim);
92 void Body::RemoveChild(Body &c) {
93 auto entry = std::find(children.begin(), children.end(), &c);
94 if (entry != children.end()) {
95 children.erase(entry);
99 double Body::Inertia() const noexcept {
100 // assume solid sphere for now
101 return (2.0/5.0) * Mass() * pow(Radius(), 2);
104 double Body::GravitationalParameter() const noexcept {
108 double Body::OrbitalPeriod() const noexcept {
110 return PI_2p0 * sqrt(pow(orbit.SemiMajorAxis(), 3) / (G * (parent->Mass() + Mass())));
116 double Body::RotationalPeriod() const noexcept {
117 if (std::abs(angular) < std::numeric_limits<double>::epsilon()) {
118 return std::numeric_limits<double>::infinity();
120 return PI_2p0 * Inertia() / angular;
124 glm::dmat4 Body::ToUniverse() const noexcept {
126 const Body *b = this;
127 while (b->HasParent()) {
128 m = b->ToParent() * m;
134 glm::dmat4 Body::FromUniverse() const noexcept {
136 const Body *b = this;
137 while (b->HasParent()) {
138 m *= b->FromParent();
145 std::vector<creature::Creature *> ccache;
146 std::vector<CreatureCreatureCollision> collisions;
149 void Body::Tick(double dt) {
150 rotation += dt * AngularMomentum() / Inertia();
152 ccache = Creatures();
153 for (creature::Creature *c : ccache) {
156 // first remove creatures so they don't collide
157 for (auto c = Creatures().begin(); c != Creatures().end();) {
158 if ((*c)->Removable()) {
160 c = Creatures().erase(c);
168 void Body::Cache() noexcept {
171 orbit.Matrix(PI_2p0 * (GetSimulation().Time() / OrbitalPeriod()))
172 * glm::eulerAngleXY(axis_tilt.x, axis_tilt.y);
174 glm::eulerAngleYX(-axis_tilt.y, -axis_tilt.x)
175 * orbit.InverseMatrix(PI_2p0 * (GetSimulation().Time() / OrbitalPeriod()));
177 orbital = glm::eulerAngleXY(axis_tilt.x, axis_tilt.y);
178 inverse_orbital = glm::eulerAngleYX(-axis_tilt.y, -axis_tilt.x);
181 glm::eulerAngleY(rotation)
182 * glm::eulerAngleXY(surface_tilt.x, surface_tilt.y);
184 glm::eulerAngleYX(-surface_tilt.y, -surface_tilt.x)
185 * glm::eulerAngleY(-rotation);
188 void Body::CheckCollision() noexcept {
189 if (Creatures().size() < 2) return;
191 auto end = Creatures().end();
192 for (auto i = Creatures().begin(); i != end; ++i) {
193 math::AABB i_box((*i)->CollisionBox());
194 glm::dmat4 i_mat((*i)->CollisionTransform());
195 for (auto j = (i + 1); j != end; ++j) {
196 glm::dvec3 diff((*i)->GetSituation().Position() - (*j)->GetSituation().Position());
197 double max_dist = ((*i)->Size() + (*j)->Size()) * 1.74;
198 if (length2(diff) > max_dist * max_dist) continue;
199 math::AABB j_box((*j)->CollisionBox());
200 glm::dmat4 j_mat((*j)->CollisionTransform());
203 if (Intersect(i_box, i_mat, j_box, j_mat, normal, depth)) {
204 collisions.push_back({ **i, **j, normal, depth });
208 for (auto &c : collisions) {
209 c.A().GetSituation().Move(c.Normal() * (c.Depth() * -0.5));
210 c.B().GetSituation().Move(c.Normal() * (c.Depth() * 0.5));
211 // TODO: adjust velocities as well
212 // TODO: notify participants so they can be annoyed
216 void Body::AddCreature(creature::Creature *c) {
217 creatures.push_back(c);
220 void Body::RemoveCreature(creature::Creature *c) {
221 auto entry = std::find(creatures.begin(), creatures.end(), c);
222 if (entry != creatures.end()) {
223 creatures.erase(entry);
228 CreatureCreatureCollision::~CreatureCreatureCollision() {
231 const glm::dvec3 &CreatureCreatureCollision::APos() const noexcept {
232 return a->GetSituation().Position();
235 const glm::dvec3 &CreatureCreatureCollision::BPos() const noexcept {
236 return b->GetSituation().Position();
252 double Orbit::SemiMajorAxis() const noexcept {
256 Orbit &Orbit::SemiMajorAxis(double s) noexcept {
261 double Orbit::Eccentricity() const noexcept {
265 Orbit &Orbit::Eccentricity(double e) noexcept {
270 double Orbit::Inclination() const noexcept {
274 Orbit &Orbit::Inclination(double i) noexcept {
279 double Orbit::LongitudeAscending() const noexcept {
283 Orbit &Orbit::LongitudeAscending(double l) noexcept {
288 double Orbit::ArgumentPeriapsis() const noexcept {
292 Orbit &Orbit::ArgumentPeriapsis(double a) noexcept {
297 double Orbit::MeanAnomaly() const noexcept {
301 Orbit &Orbit::MeanAnomaly(double m) noexcept {
308 double mean2eccentric(double M, double e) {
309 double E = M; // eccentric anomaly, solve M = E - e sin E
310 // limit to 100 steps to prevent deadlocks in impossible situations
311 for (int i = 0; i < 100; ++i) {
312 double dE = (E - e * sin(E) - M) / (1 - e * cos(E));
314 if (abs(dE) < 1.0e-6) break;
321 glm::dmat4 Orbit::Matrix(double t) const noexcept {
323 double E = mean2eccentric(M, ecc);
325 // coordinates in orbital plane, P=x, Q=-z
326 double P = sma * (cos(E) - ecc);
327 double Q = sma * sin(E) * sqrt(1 - (ecc * ecc));
329 return glm::yawPitchRoll(asc, inc, arg) * glm::translate(glm::dvec3(P, 0.0, -Q));
332 glm::dmat4 Orbit::InverseMatrix(double t) const noexcept {
334 double E = mean2eccentric(M, ecc);
335 double P = sma * (cos(E) - ecc);
336 double Q = sma * sin(E) * sqrt(1 - (ecc * ecc));
337 return glm::translate(glm::dvec3(-P, 0.0, Q)) * glm::transpose(glm::yawPitchRoll(asc, inc, arg));
341 Planet::Planet(int sidelength)
343 , sidelength(sidelength)
344 , tiles(TilesTotal())
346 Radius(double(sidelength) / 2.0);
352 const TileType &Planet::TypeAt(int srf, int x, int y) const {
353 return GetSimulation().TileTypes()[TileAt(srf, x, y).type];
356 glm::ivec2 Planet::SurfacePosition(int srf, const glm::dvec3 &pos) const noexcept {
358 PositionToTile(pos[(srf + 0) % 3]),
359 PositionToTile(pos[(srf + 1) % 3]));
362 double Planet::SurfaceElevation(int srf, const glm::dvec3 &pos) const noexcept {
364 ? pos[(srf + 2) % 3] - Radius()
365 : -pos[(srf + 2) % 3] - Radius();
368 glm::dvec3 Planet::TileCenter(int srf, int x, int y, double e) const noexcept {
369 glm::dvec3 center(0.0f);
370 center[(srf + 0) % 3] = x + 0.5 - Radius();
371 center[(srf + 1) % 3] = y + 0.5 - Radius();
372 center[(srf + 2) % 3] = srf < 3 ? (Radius() + e) : -(Radius() + e);
376 void Planet::BuildVAO(const Set<TileType> &ts) {
377 vao.reset(new graphics::SimpleVAO<Attributes, unsigned int>);
379 vao->BindAttributes();
380 vao->EnableAttribute(0);
381 vao->EnableAttribute(1);
382 vao->AttributePointer<glm::vec3>(0, false, offsetof(Attributes, position));
383 vao->AttributePointer<glm::vec3>(1, false, offsetof(Attributes, tex_coord));
384 vao->ReserveAttributes(TilesTotal() * 4, GL_STATIC_DRAW);
386 auto attrib = vao->MapAttributes(GL_WRITE_ONLY);
387 float offset = Radius();
390 // up +Z +X +Y -Z -X -Y
392 for (int index = 0, surface = 0; surface < 6; ++surface) {
393 for (int y = 0; y < sidelength; ++y) {
394 for (int x = 0; x < sidelength; ++x, ++index) {
395 float tex = ts[TileAt(surface, x, y).type].texture;
396 const float tex_v_begin = surface < 3 ? 1.0f : 0.0f;
397 const float tex_v_end = surface < 3 ? 0.0f : 1.0f;
398 attrib[4 * index + 0].position[(surface + 0) % 3] = x + 0 - offset;
399 attrib[4 * index + 0].position[(surface + 1) % 3] = y + 0 - offset;
400 attrib[4 * index + 0].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
401 attrib[4 * index + 0].tex_coord[0] = 0.0f;
402 attrib[4 * index + 0].tex_coord[1] = tex_v_begin;
403 attrib[4 * index + 0].tex_coord[2] = tex;
405 attrib[4 * index + 1].position[(surface + 0) % 3] = x + 0 - offset;
406 attrib[4 * index + 1].position[(surface + 1) % 3] = y + 1 - offset;
407 attrib[4 * index + 1].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
408 attrib[4 * index + 1].tex_coord[0] = 0.0f;
409 attrib[4 * index + 1].tex_coord[1] = tex_v_end;
410 attrib[4 * index + 1].tex_coord[2] = tex;
412 attrib[4 * index + 2].position[(surface + 0) % 3] = x + 1 - offset;
413 attrib[4 * index + 2].position[(surface + 1) % 3] = y + 0 - offset;
414 attrib[4 * index + 2].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
415 attrib[4 * index + 2].tex_coord[0] = 1.0f;
416 attrib[4 * index + 2].tex_coord[1] = tex_v_begin;
417 attrib[4 * index + 2].tex_coord[2] = tex;
419 attrib[4 * index + 3].position[(surface + 0) % 3] = x + 1 - offset;
420 attrib[4 * index + 3].position[(surface + 1) % 3] = y + 1 - offset;
421 attrib[4 * index + 3].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
422 attrib[4 * index + 3].tex_coord[0] = 1.0f;
423 attrib[4 * index + 3].tex_coord[1] = tex_v_end;
424 attrib[4 * index + 3].tex_coord[2] = tex;
430 vao->ReserveElements(TilesTotal() * 6, GL_STATIC_DRAW);
432 auto element = vao->MapElements(GL_WRITE_ONLY);
434 for (int surface = 0; surface < 3; ++surface) {
435 for (int y = 0; y < sidelength; ++y) {
436 for (int x = 0; x < sidelength; ++x, ++index) {
437 element[6 * index + 0] = 4 * index + 0;
438 element[6 * index + 1] = 4 * index + 2;
439 element[6 * index + 2] = 4 * index + 1;
440 element[6 * index + 3] = 4 * index + 1;
441 element[6 * index + 4] = 4 * index + 2;
442 element[6 * index + 5] = 4 * index + 3;
446 for (int surface = 3; surface < 6; ++surface) {
447 for (int y = 0; y < sidelength; ++y) {
448 for (int x = 0; x < sidelength; ++x, ++index) {
449 element[6 * index + 0] = 4 * index + 0;
450 element[6 * index + 1] = 4 * index + 1;
451 element[6 * index + 2] = 4 * index + 2;
452 element[6 * index + 3] = 4 * index + 2;
453 element[6 * index + 4] = 4 * index + 1;
454 element[6 * index + 5] = 4 * index + 3;
462 void Planet::Draw(app::Assets &assets, graphics::Viewport &viewport) {
466 const glm::mat4 &MV = assets.shaders.planet_surface.MV();
467 assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(0.0f, 0.0f, 1.0f, 0.0f)));
468 vao->DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 0);
469 assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(1.0f, 0.0f, 0.0f, 0.0f)));
470 vao->DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 1);
471 assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(0.0f, 1.0f, 0.0f, 0.0f)));
472 vao->DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 2);
473 assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(0.0f, 0.0f, -1.0f, 0.0f)));
474 vao->DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 3);
475 assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(-1.0f, 0.0f, 0.0f, 0.0f)));
476 vao->DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 4);
477 assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(0.0f, -1.0f, 0.0f, 0.0f)));
478 vao->DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 5);
482 void GenerateEarthlike(const Set<TileType> &tiles, Planet &p) noexcept {
483 math::SimplexNoise elevation_gen(0);
484 math::SimplexNoise variation_gen(45623752346);
486 const int ice = tiles["ice"].id;
487 const int ocean = tiles["ocean"].id;
488 const int water = tiles["water"].id;
489 const int sand = tiles["sand"].id;
490 const int grass = tiles["grass"].id;
491 const int tundra = tiles["tundra"].id;
492 const int taiga = tiles["taiga"].id;
493 const int desert = tiles["desert"].id;
494 const int mntn = tiles["mountain"].id;
495 const int algae = tiles["algae"].id;
496 const int forest = tiles["forest"].id;
497 const int jungle = tiles["jungle"].id;
498 const int rock = tiles["rock"].id;
499 const int wheat = tiles["wheat"].id;
501 constexpr double ocean_thresh = -0.2;
502 constexpr double water_thresh = 0.0;
503 constexpr double beach_thresh = 0.05;
504 constexpr double highland_thresh = 0.4;
505 constexpr double mountain_thresh = 0.5;
507 const glm::dvec3 axis(glm::dvec4(0.0, 1.0, 0.0, 0.0) * glm::eulerAngleXY(p.SurfaceTilt().x, p.SurfaceTilt().y));
508 const double cap_thresh = std::abs(std::cos(p.AxialTilt().x));
509 const double equ_thresh = std::abs(std::sin(p.AxialTilt().x)) / 2.0;
510 const double fzone_start = equ_thresh - (equ_thresh - cap_thresh) / 3.0;
511 const double fzone_end = cap_thresh + (equ_thresh - cap_thresh) / 3.0;
513 for (int surface = 0; surface <= 5; ++surface) {
514 for (int y = 0; y < p.SideLength(); ++y) {
515 for (int x = 0; x < p.SideLength(); ++x) {
516 glm::dvec3 to_tile = p.TileCenter(surface, x, y);
517 double near_axis = std::abs(glm::dot(glm::normalize(to_tile), axis));
518 if (near_axis > cap_thresh) {
519 p.TileAt(surface, x, y).type = ice;
522 float elevation = math::OctaveNoise(
524 to_tile / p.Radius(),
527 5 / p.Radius(), // frequency
531 float variation = math::OctaveNoise(
533 to_tile / p.Radius(),
536 16 / p.Radius(), // frequency
540 if (elevation < ocean_thresh) {
541 p.TileAt(surface, x, y).type = ocean;
542 } else if (elevation < water_thresh) {
543 if (variation > 0.3) {
544 p.TileAt(surface, x, y).type = algae;
546 p.TileAt(surface, x, y).type = water;
548 } else if (elevation < beach_thresh) {
549 p.TileAt(surface, x, y).type = sand;
550 } else if (elevation < highland_thresh) {
551 if (near_axis < equ_thresh) {
552 if (variation > 0.6) {
553 p.TileAt(surface, x, y).type = grass;
554 } else if (variation > 0.2) {
555 p.TileAt(surface, x, y).type = sand;
557 p.TileAt(surface, x, y).type = desert;
559 } else if (near_axis < fzone_start) {
560 if (variation > 0.4) {
561 p.TileAt(surface, x, y).type = forest;
562 } else if (variation < -0.5) {
563 p.TileAt(surface, x, y).type = jungle;
564 } else if (variation > -0.02 && variation < 0.02) {
565 p.TileAt(surface, x, y).type = wheat;
567 p.TileAt(surface, x, y).type = grass;
569 } else if (near_axis < fzone_end) {
570 p.TileAt(surface, x, y).type = tundra;
572 p.TileAt(surface, x, y).type = taiga;
574 } else if (elevation < mountain_thresh) {
575 if (variation > 0.3) {
576 p.TileAt(surface, x, y).type = mntn;
578 p.TileAt(surface, x, y).type = rock;
581 p.TileAt(surface, x, y).type = mntn;
589 void GenerateTest(const Set<TileType> &tiles, Planet &p) noexcept {
590 for (int surface = 0; surface <= 5; ++surface) {
591 for (int y = 0; y < p.SideLength(); ++y) {
592 for (int x = 0; x < p.SideLength(); ++x) {
593 if (x == p.SideLength() / 2 && y == p.SideLength() / 2) {
594 p.TileAt(surface, x, y).type = surface;
596 p.TileAt(surface, x, y).type = (x == p.SideLength()/2) + (y == p.SideLength()/2) + 6;
613 std::vector<TileType::Yield>::const_iterator TileType::FindResource(int r) const {
614 auto yield = resources.cbegin();
615 for (; yield != resources.cend(); ++yield) {
616 if (yield->resource == r) {
623 std::vector<TileType::Yield>::const_iterator TileType::FindBestResource(const creature::Composition &comp) const {
624 auto best = resources.cend();
625 double best_value = 0.0;
626 for (auto yield = resources.cbegin(); yield != resources.cend(); ++yield) {
627 double value = comp.Get(yield->resource);
628 if (value > best_value) {