Cogs.Core
CurvedEarthPositionComponent.cpp
1#include <limits>
2#include <glm/gtx/quaternion.hpp>
3#include "CurvedEarthPositionComponent.h"
4#include "Components/Core/TransformComponent.h"
5
6#include "Types.h"
7
8using namespace Cogs::Reflection;
9
10void Cogs::Core::CurvedEarthPositionComponent::registerType()
11{
12 Field fields[] = {
13 Field(Name("position"), &CurvedEarthPositionComponent::position)
14 };
15 Method methods[] = {
16 Method(Name("update"), &CurvedEarthPositionComponent::update)
17 };
18
19 DynamicComponent::registerDerivedType<CurvedEarthPositionComponent>().setMethods(methods).setFields(fields);
20}
21
22
23void Cogs::Core::CurvedEarthPositionComponent::update()
24{
25 const float R = 6371000.f;
26
27 if (!hasChanged()) return;
28
29 auto * trComp = getComponent<TransformComponent>();
30
31 float d = glm::length(glm::vec2(position));
32
33 trComp->position = glm::vec3(position.x,
34 position.y,
35 position.z - (R - glm::sqrt((R - d)*(R + d))));
36 const glm::vec3 a = glm::vec3(0, 0, 1);
37 const glm::vec3 b = glm::normalize(trComp->position - glm::vec3(0, 0, -R));
38 const glm::vec3 n = glm::cross(a, b);
39 const float n_l = glm::length(n);
40 if (std::numeric_limits<float>::epsilon() < glm::abs(n_l)) {
41 trComp->rotation = glm::angleAxis(glm::acos(glm::dot(a, b)), (1.f / n_l)*n);
42 }
43 else {
44 trComp->rotation = glm::quat();
45 }
46 trComp->setChanged();
47}
Field definition describing a single data member of a data structure.
Definition: Field.h:70
Simple method definition.
Definition: Method.h:72
Contains reflection support.
Definition: Component.h:11
Represents an unique name.
Definition: Name.h:70