planet distribution is nice now and one sun is always placed with bigger radius and space nearby.

This commit is contained in:
Andreas Ortmann 2016-09-30 23:15:51 +02:00
parent f2bdadd49f
commit 0b71aad1f7
5 changed files with 86 additions and 45 deletions

View file

@ -19,7 +19,7 @@
#include "util.hpp"
namespace game {
void State::init(bool devMode)
void State::init(int numPlanets, bool devMode)
{
m_nextId = 0;
m_time = 0.0;
@ -30,31 +30,72 @@ namespace game {
m_maxNumTraces = 10;
m_developerMode = devMode;
bool planetsOnCircle = false;
Planet::Material mat = Planet::Material::Rock;
int numPlanets = 10;
for (int i=0; i<numPlanets; i++) {
float t = i / static_cast<float>(numPlanets);
t *= 2.0*M_PI;
switch(i) {
case 0:
mat = Planet::Material::Sun;
break;
// distribute but not in the center
int tries = 0;
glm::vec2 pos;
do {
float cr = 0.7;
if (planetsOnCircle) {
pos = cr * glm::vec2(std::sin(t), std::cos(t));
} else {
pos = cr * util::randv2_m1_1();
}
} while(glm::length(pos) < 0.2 && tries++ < 1000);
case 1:
case 2:
mat = Planet::Material::Water;
break;
Planet::Material mat = Planet::Material::Rock;
if (i == 0) {
mat = Planet::Material::Sun;
case 3:
case 4:
mat = Planet::Material::Sand;
break;
case 5:
mat = Planet::Material::Metal;
break;
default:
mat = Planet::Material::Rock;
}
planets.push_back(new Planet(pos, i, 0.03 + 0.07*util::randf_0_1(), mat));
glm::vec2 pos;
float radius = 0.03 + 0.07*util::randf_0_1();
if (i == 0) {
// sun is bigger but not too big
radius += 0.05;
if (radius > 0.9) {
radius = 0.9;
}
}
bool tooNearToCenter = true;
bool collidesWithOtherPlanet = true;
// distribute but not in the center and not next to other planets
int tries = 0;
do {
pos = util::randv2_m1_1();
collidesWithOtherPlanet = false;
tooNearToCenter = glm::length(pos) < 0.1;
if (!tooNearToCenter) {
for (const Planet *other : planets) {
float d = glm::distance(other->position, pos);
float extraDist = (other->material == Planet::Material::Sun)
? 4.0
: 1.0;
if (d < extraDist*other->radius + radius) {
collidesWithOtherPlanet = true;
break;
}
}
}
} while((collidesWithOtherPlanet || tooNearToCenter) && tries++ < 1000);
planets.push_back(new Planet(pos, i, radius, mat));
}
}

View file

@ -40,7 +40,7 @@ namespace game {
// called to setup the state (randomize planets, kill
// traces/missiles/ships etc.)
void init(bool devMode=false);
void init(int numPlanets=15, bool devMode=false);
// main method to advance the simulation by the given timestamp in
// seconds.