// TODO: extend to nearby bodies as well
for (auto c : cam.Reference().Creatures()) {
assets.shaders.creature_skin.SetM(cam.Model(cam.Reference()) * glm::mat4(c->LocalTransform()));
- assets.shaders.creature_skin.SetBaseColor(c->BaseColor());
- assets.shaders.creature_skin.SetHighlightColor(c->HighlightColor());
+ assets.shaders.creature_skin.SetBaseColor(glm::vec3(c->BaseColor()));
+ assets.shaders.creature_skin.SetHighlightColor(glm::vec4(c->HighlightColor()));
c->Draw(viewport);
}
bool Creature::PerceptionTest(const glm::dvec3 &p) const noexcept {
const glm::dvec3 diff(p - situation.Position());
double omni_range = PerceptionOmniRange();
- if (length2(diff) < omni_range * omni_range) return true;
+ if (glm::length2(diff) < omni_range * omni_range) return true;
double range = PerceptionRange();
- if (length2(diff) > range * range) return false;
- return dot(normalize(diff), situation.Heading()) > PerceptionField();
+ if (glm::length2(diff) > range * range) return false;
+ return glm::dot(glm::normalize(diff), situation.Heading()) > PerceptionField();
}
double Creature::OffspringChance() const noexcept {
state.pos += f.vel * dt;
state.vel += f.acc * dt;
situation.EnforceConstraints(state);
- if (length2(state.vel) > 0.000001) {
- glm::dvec3 nvel(normalize(state.vel));
- double ang = angle(nvel, state.dir);
+ if (glm::length2(state.vel) > 0.000001) {
+ glm::dvec3 nvel(glm::normalize(state.vel));
+ double ang = glm::angle(nvel, state.dir);
double turn_rate = PI * 0.75 * dt;
if (ang < turn_rate) {
- state.dir = normalize(state.vel);
+ state.dir = glm::normalize(state.vel);
} else if (std::abs(ang - PI) < 0.001) {
- state.dir = rotate(state.dir, turn_rate, situation.GetPlanet().NormalAt(state.pos));
+ state.dir = glm::rotate(state.dir, turn_rate, situation.GetPlanet().NormalAt(state.pos));
} else {
- state.dir = rotate(state.dir, turn_rate, normalize(cross(state.dir, nvel)));
+ state.dir = glm::rotate(state.dir, turn_rate, glm::normalize(glm::cross(state.dir, nvel)));
}
}
situation.SetState(state);
// work is force times distance
- DoWork(length(f.acc) * Mass() * length(f.vel) * dt);
+ DoWork(glm::length(f.acc) * Mass() * glm::length(f.vel) * dt);
}
Situation::Derivative Creature::Step(const Situation::Derivative &ds, double dt) const noexcept {
// if net force is applied and in contact with surface
if (!allzero(force) && std::abs(std::abs(elevation) - situation.GetPlanet().Radius()) < 0.001) {
// apply friction = -|normal force| * tangential force * coefficient
- glm::dvec3 fn(normal * dot(force, normal));
+ glm::dvec3 fn(normal * glm::dot(force, normal));
glm::dvec3 ft(force - fn);
double u = 0.4;
- glm::dvec3 friction(-length(fn) * ft * u);
+ glm::dvec3 friction(-glm::length(fn) * ft * u);
force += friction;
}
return {
glm::dmat3 orient;
orient[1] = situation.GetPlanet().NormalAt(pos);
orient[2] = situation.Heading();
- if (std::abs(dot(orient[1], orient[2])) > 0.999) {
+ if (std::abs(glm::dot(orient[1], orient[2])) > 0.999) {
orient[2] = glm::dvec3(orient[1].z, orient[1].x, orient[1].y);
}
- orient[0] = normalize(cross(orient[1], orient[2]));
- orient[2] = normalize(cross(orient[0], orient[1]));
+ orient[0] = glm::normalize(glm::cross(orient[1], orient[2]));
+ orient[2] = glm::normalize(glm::cross(orient[0], orient[1]));
return glm::translate(glm::dvec3(pos.x, pos.y, pos.z))
* glm::dmat4(orient)
* glm::translate(glm::dvec3(0.0, half_size, 0.0));
void Situation::EnforceConstraints(State &s) noexcept {
if (OnSurface()) {
double r = GetPlanet().Radius();
- if (length2(s.pos) < r * r) {
- s.pos = normalize(s.pos) * r;
+ if (glm::length2(s.pos) < r * r) {
+ s.pos = glm::normalize(s.pos) * r;
}
}
}
for (auto &other : s.GetPlanet().Creatures()) {
if (&*other == &c) continue;
glm::dvec3 diff = s.Position() - other->GetSituation().Position();
- if (length2(diff) > max_look * max_look) continue;
+ if (glm::length2(diff) > max_look * max_look) continue;
if (!c.PerceptionTest(other->GetSituation().Position())) continue;
- double sep = glm::clamp(length(diff) - other->Size() * 0.707 - c.Size() * 0.707, 0.0, min_dist);
- repulse += normalize(diff) * (1.0 - sep / min_dist) * force;
+ double sep = glm::clamp(glm::length(diff) - other->Size() * 0.707 - c.Size() * 0.707, 0.0, min_dist);
+ repulse += glm::normalize(diff) * (1.0 - sep / min_dist) * force;
}
result += repulse;
}
if (seeking) {
glm::dvec3 diff = target - s.pos;
if (!allzero(diff)) {
- result += TargetVelocity(s, (normalize(diff) * speed), force);
+ result += TargetVelocity(s, (glm::normalize(diff) * speed), force);
}
}
if (arriving) {
glm::dvec3 diff = target - s.pos;
- double dist = length(diff);
+ double dist = glm::length(diff);
if (!allzero(diff) && dist > std::numeric_limits<double>::epsilon()) {
result += TargetVelocity(s, diff * std::min(dist * force, speed) / dist, force);
}
}
- if (length2(result) > max_force * max_force) {
- result = normalize(result) * max_force;
+ if (glm::length2(result) > max_force * max_force) {
+ result = glm::normalize(result) * max_force;
}
return result;
}
const world::Planet &planet = GetSituation().GetPlanet();
const glm::dvec3 &pos = GetSituation().Position();
const glm::dvec3 normal(planet.NormalAt(pos));
- const glm::dvec3 step_x(normalize(cross(normal, glm::dvec3(normal.z, normal.x, normal.y))));
- const glm::dvec3 step_y(normalize(cross(step_x, normal)));
+ const glm::dvec3 step_x(glm::normalize(glm::cross(normal, glm::dvec3(normal.z, normal.x, normal.y))));
+ const glm::dvec3 step_y(glm::normalize(glm::cross(step_x, normal)));
constexpr int search_radius = 2;
double rating[2 * search_radius + 1][2 * search_radius + 1] = {0};
for (int y = -search_radius; y < search_radius + 1; ++y) {
for (int x = -search_radius; x < search_radius + 1; ++x) {
const glm::dvec3 tpos(pos + (double(x) * step_x) + (double(y) * step_y));
- if (length2(tpos - c->GetSituation().Position()) < 1.0) {
+ if (glm::length2(tpos - c->GetSituation().Position()) < 1.0) {
rating[y + search_radius][x + search_radius] *= 0.8;
}
}
if (best_rating > 0.0) {
found = true;
searching = false;
- target_pos = normalize(pos + (double(best_pos.x) * step_x) + (double(best_pos.y) * step_y)) * planet.Radius();
+ target_pos = glm::normalize(pos + (double(best_pos.x) * step_x) + (double(best_pos.y) * step_y)) * planet.Radius();
GetSteering().GoTo(target_pos);
} else if (!searching) {
found = false;
target_pos += Random().SNorm() * step_y;
// bias towards current heading
target_pos += GetSituation().Heading() * 1.5;
- target_pos = normalize(target_pos) * planet.Radius();
+ target_pos = glm::normalize(target_pos) * planet.Radius();
GetSteering().GoTo(target_pos);
}
}
bool LocateResourceGoal::NearTarget() const noexcept {
const Situation &s = GetSituation();
- return s.OnSurface() && length2(s.Position() - target_pos) < 0.5;
+ return s.OnSurface() && glm::length2(s.Position() - target_pos) < 0.5;
}
}
void StrollGoal::Action() {
- if (length2(next - GetSituation().Position()) < 0.0001) {
+ if (glm::length2(next - GetSituation().Position()) < 0.0001) {
PickTarget();
}
}
next += GetSituation().Heading() * 1.5;
const glm::dvec3 normal(GetSituation().GetPlanet().NormalAt(GetSituation().Position()));
glm::dvec3 rand_x(GetSituation().Heading());
- if (std::abs(dot(normal, rand_x)) > 0.999) {
+ if (std::abs(glm::dot(normal, rand_x)) > 0.999) {
rand_x = glm::dvec3(normal.z, normal.x, normal.y);
}
- glm::dvec3 rand_y = cross(normal, rand_x);
+ glm::dvec3 rand_y = glm::cross(normal, rand_x);
next += ((rand_x * Random().SNorm()) + (rand_y * Random().SNorm())) * 1.5;
- next = normalize(next) * GetSituation().GetPlanet().Radius();
+ next = glm::normalize(next) * GetSituation().GetPlanet().Radius();
GetSteering().GoTo(next);
}
Reference(s.GetPlanet());
track_orient = true;
up = s.GetPlanet().NormalAt(s.Position());
- glm::dvec3 ref(normalize(cross(up, glm::dvec3(up.z, up.x, up.y))));
+ glm::dvec3 ref(glm::normalize(glm::cross(up, glm::dvec3(up.z, up.x, up.y))));
dir =
- glm::dmat3(ref, up, cross(ref, up))
+ glm::dmat3(ref, up, glm::cross(ref, up))
* glm::dmat3(glm::eulerAngleYX(-angle.y, -angle.x))
* dir;
} else {
if (&b == ref) {
return track_orient ? glm::mat4(1.0f) : glm::mat4(ref->LocalTransform());
} else if (b.HasParent() && &b.Parent() == ref) {
- return track_orient
+ return glm::mat4(track_orient
? ref->InverseTransform() * b.FromParent() * b.LocalTransform()
- : b.FromParent() * b.LocalTransform();
+ : b.FromParent() * b.LocalTransform());
} else if (ref->HasParent() && &ref->Parent() == &b) {
- return track_orient
+ return glm::mat4(track_orient
? ref->InverseTransform() * ref->ToParent() * b.LocalTransform()
- : ref->ToParent() * b.LocalTransform();
+ : ref->ToParent() * b.LocalTransform());
} else {
- return track_orient
+ return glm::mat4(track_orient
? ref->InverseTransform() * ref->ToUniverse() * b.FromUniverse() * b.LocalTransform()
- : ref->ToUniverse() * b.FromUniverse() * b.LocalTransform();
+ : ref->ToUniverse() * b.FromUniverse() * b.LocalTransform());
}
}
glm::dvec3(b_m[0]),
glm::dvec3(b_m[1]),
glm::dvec3(b_m[2]),
- normalize(cross(glm::dvec3(a_m[0]), glm::dvec3(b_m[0]))),
- normalize(cross(glm::dvec3(a_m[0]), glm::dvec3(b_m[1]))),
- normalize(cross(glm::dvec3(a_m[0]), glm::dvec3(b_m[2]))),
- normalize(cross(glm::dvec3(a_m[1]), glm::dvec3(b_m[0]))),
- normalize(cross(glm::dvec3(a_m[1]), glm::dvec3(b_m[1]))),
- normalize(cross(glm::dvec3(a_m[1]), glm::dvec3(b_m[2]))),
- normalize(cross(glm::dvec3(a_m[2]), glm::dvec3(b_m[0]))),
- normalize(cross(glm::dvec3(a_m[2]), glm::dvec3(b_m[1]))),
- normalize(cross(glm::dvec3(a_m[2]), glm::dvec3(b_m[2]))),
+ glm::normalize(glm::cross(glm::dvec3(a_m[0]), glm::dvec3(b_m[0]))),
+ glm::normalize(glm::cross(glm::dvec3(a_m[0]), glm::dvec3(b_m[1]))),
+ glm::normalize(glm::cross(glm::dvec3(a_m[0]), glm::dvec3(b_m[2]))),
+ glm::normalize(glm::cross(glm::dvec3(a_m[1]), glm::dvec3(b_m[0]))),
+ glm::normalize(glm::cross(glm::dvec3(a_m[1]), glm::dvec3(b_m[1]))),
+ glm::normalize(glm::cross(glm::dvec3(a_m[1]), glm::dvec3(b_m[2]))),
+ glm::normalize(glm::cross(glm::dvec3(a_m[2]), glm::dvec3(b_m[0]))),
+ glm::normalize(glm::cross(glm::dvec3(a_m[2]), glm::dvec3(b_m[1]))),
+ glm::normalize(glm::cross(glm::dvec3(a_m[2]), glm::dvec3(b_m[2]))),
};
depth = std::numeric_limits<double>::infinity();
int cur_axis = 0;
for (const glm::dvec3 &axis : axes) {
- if (any(isnan(axis))) {
+ if (glm::any(glm::isnan(axis))) {
// can result from the cross products if A and B have parallel axes
++cur_axis;
continue;
double a_min = std::numeric_limits<double>::infinity();
double a_max = -std::numeric_limits<double>::infinity();
for (const glm::dvec3 &corner : a_corners) {
- double val = dot(corner, axis);
+ double val = glm::dot(corner, axis);
a_min = std::min(a_min, val);
a_max = std::max(a_max, val);
}
double b_min = std::numeric_limits<double>::infinity();
double b_max = -std::numeric_limits<double>::infinity();
for (const glm::dvec3 &corner : b_corners) {
- double val = dot(corner, axis);
+ double val = glm::dot(corner, axis);
b_min = std::min(b_min, val);
b_max = std::max(b_max, val);
}
const glm::dvec3 &n,
double depth
) : a(&a), b(&b), normal(n), depth(depth) {
- if (dot(normal, BPos() - APos()) < 0.0) {
+ if (glm::dot(normal, BPos() - APos()) < 0.0) {
// make sure normal always is in direction from A to B
normal *= -1.0;
}
public:
/// surface normal
- glm::dvec3 NormalAt(const glm::dvec3 &p) const noexcept { return normalize(p); }
+ glm::dvec3 NormalAt(const glm::dvec3 &p) const noexcept { return glm::normalize(p); }
/// height over surface
- double ElevationAt(const glm::dvec3 &p) const noexcept { return length(p) - Radius(); }
+ double ElevationAt(const glm::dvec3 &p) const noexcept { return glm::length(p) - Radius(); }
/// distance to planet center
- double DistanceAt(const glm::dvec3 &p) const noexcept { return length(p); }
+ double DistanceAt(const glm::dvec3 &p) const noexcept { return glm::length(p); }
/// get ground tile
Tile &TileAt(const glm::dvec3 &) noexcept;
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;
+ if (glm::length2(diff) > max_dist * max_dist) continue;
math::AABB j_box((*j)->CollisionBox());
glm::dmat4 j_mat((*j)->CollisionTransform());
glm::dvec3 normal;
for (auto &c : collisions) {
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()));
+ c.A().GetSituation().Accelerate(c.Normal() * -glm::dot(c.Normal(), c.AVel()));
+ c.B().GetSituation().Accelerate(c.Normal() * -glm::dot(c.Normal(), c.BVel()));
// TODO: notify participants so they can be annoyed
}
}
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;
}
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) {
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) {
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]) * (surface < 3 ? offset : -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]) * (surface < 3 ? offset : -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]) * (surface < 3 ? offset : -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]) * (surface < 3 ? offset : -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;
}
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
);
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
glm::dvec3 normal(0);
AABB box{ { -1, -1, -1 }, { 1, 1, 1 } }; // 2x2x2 cube centered around origin
- glm::mat4 Ma(1); // identity
- glm::mat4 Mb(1); // identity
+ glm::dmat4 Ma(1); // identity
+ glm::dmat4 Mb(1); // identity
// they're identical, so should probably intersect ^^
CPPUNIT_ASSERT_MESSAGE(
);
CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
"bad penetration depth (with rotation)",
- 0.0142134428024292, depth, delta
+ 0.014213562373095, depth, delta
);
AssertEqual(
"bad intersection normal (with rotation)",
- glm::dvec3(1, 0, 0), abs(normal) // normal can be in + or - x, therefore abs()
+ glm::dvec3(1, 0, 0), glm::abs(normal) // normal can be in + or - x, therefore abs()
);
Mb = glm::translate(glm::dvec3(3, 0, 0)); // 3 to the right