From f8762cc2c689fc11c3c8e9927f0dd3a2b6650d77 Mon Sep 17 00:00:00 2001 From: Joursoir Date: Sat, 10 Apr 2021 10:35:22 +0000 Subject: game: add dependent celestial objects --- src/game/CelestialObject.cpp | 31 ++++++++++++++++++++++++------- src/game/CelestialObject.hpp | 14 +++++++++----- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/src/game/CelestialObject.cpp b/src/game/CelestialObject.cpp index 4016391..12153ae 100644 --- a/src/game/CelestialObject.cpp +++ b/src/game/CelestialObject.cpp @@ -34,9 +34,10 @@ void CelestialObject::SetRadius(float object_r, float orbital_r) orbital_radius = orbital_r; } -void CelestialObject::SetTilt(float angle) +void CelestialObject::SetTilts(float rot_axis, float rot_ecliptic) { - tilt_angle = radians(angle); + rotation_axis_tilt = radians(90.0f + rot_axis); + rotation_ecliptic_tilt = radians(rot_ecliptic); } void CelestialObject::SetPosition(glm::vec3 orbital) @@ -44,21 +45,30 @@ void CelestialObject::SetPosition(glm::vec3 orbital) orbital_pos = orbital; } +void CelestialObject::AddSlave(CelestialObject *slave) +{ + struct linked_list *list; + list = new struct linked_list(slave, slaves); + slaves = list; +} + void CelestialObject::Draw(float time, float game_speed, unsigned int model_loc) { float orb_speed = orbital_speed * game_speed, rot_speed = rotation_speed * game_speed, - moveX = sin(radians(time) * orb_speed) * orbital_radius, - moveZ = cos(radians(time) * orb_speed) * orbital_radius; + rt_angle = radians(time) * orb_speed, + move_x = sin(rt_angle) * orbital_radius, + move_y = 0, + move_z = cos(rt_angle) * orbital_radius; texture->Bind(); mat4 sphere_model(1.0f); - vec3 pos(orbital_pos.x - moveX, orbital_pos.y, orbital_pos.z - moveZ); + vec3 pos(orbital_pos.x - move_x, orbital_pos.y - move_y, orbital_pos.z - move_z); sphere_model = translate(sphere_model, pos); - sphere_model = rotate(sphere_model, tilt_angle, vec3(1.0f, 0.0f, 0.0f)); + sphere_model = rotate(sphere_model, rotation_axis_tilt, vec3(1.0f, 0.0f, 0.0f)); sphere_model = rotate(sphere_model, radians(time) * rot_speed, vec3(0.0f, 0.0f, 1.0f)); sphere_model = scale(sphere_model, vec3(object_radius, object_radius, object_radius)); glUniformMatrix4fv(model_loc, 1, GL_FALSE, value_ptr(sphere_model)); @@ -67,8 +77,15 @@ void CelestialObject::Draw(float time, float game_speed, if(orbital_radius != 0.0f) { mat4 circle_model(1.0f); circle_model = translate(circle_model, orbital_pos); + // circle_model = rotate(circle_model, rotation_ecliptic_tilt, vec3(1.0f, 0.0f, 0.0f)); circle_model = scale(circle_model, vec3(orbital_radius, orbital_radius, orbital_radius)); glUniformMatrix4fv(model_loc, 1, GL_FALSE, value_ptr(circle_model)); circle->Draw(); } -} \ No newline at end of file + + struct linked_list *ptr; + for(ptr = slaves; ptr; ptr = ptr->next) { + ptr->data->SetPosition(pos); + ptr->data->Draw(time, game_speed, model_loc); + } +} diff --git a/src/game/CelestialObject.hpp b/src/game/CelestialObject.hpp index 8997697..a883fc3 100644 --- a/src/game/CelestialObject.hpp +++ b/src/game/CelestialObject.hpp @@ -3,6 +3,8 @@ #include +#include "../xstdlib/xstdlib.hpp" + class Texture; class Mesh; @@ -12,6 +14,8 @@ class CelestialObject { static Mesh *sphere; static Mesh *circle; + struct linked_list *slaves; + /* time a given astronomical object takes to complete one orbit around another object */ float orbital_period; @@ -23,19 +27,19 @@ class CelestialObject { around its axis of rotation relative to the background stars */ float rotation_period; float rotation_speed; + float rotation_axis_tilt; + float rotation_ecliptic_tilt; float object_radius; - - float tilt_angle; public: - CelestialObject(Texture *t) - : texture(t) { } + CelestialObject(Texture *t) : texture(t), slaves(0) { } static void InitializeMesh(); void SetPeriod(float a_orbital, float a_rotation); void SetRadius(float object_r, float orbital_r); - void SetTilt(float angle); + void SetTilts(float rot_axis, float rot_ecliptic); void SetPosition(glm::vec3 orbital); + void AddSlave(CelestialObject *slave); void Draw(float time, float game_speed, unsigned int model_loc); }; -- cgit v1.2.3-18-g5258