Repository navigation
Expand file tree
/
Copy pathMoon.cpp
More file actions
69 lines (57 loc) · 1.91 KB
/
Copy pathMoon.cpp
File metadata and controls
69 lines (57 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "Moon.h"
#include "Planet.h"
#include <GL/glew.h>
#include <cmath>
Moon::Moon(const std::string &name, float radius, const std::string &texturePath,
Planet *parentPlanet, float orbitRadius, float orbitSpeed)
: CelestialBody(name, radius, texturePath),
m_parentPlanet(parentPlanet), m_orbitRadius(orbitRadius),
m_orbitSpeed(orbitSpeed), m_orbitAngle(0.0f), m_time(0.0f)
{
m_rotationSpeed = orbitSpeed * 0.8f; // Fixed: Added missing '*' operator
updateOrbitalPosition();
}
void Moon::update(float deltaTime)
{
m_time += deltaTime;
// Update orbital position around parent planet
m_orbitAngle += m_orbitSpeed * deltaTime;
updateOrbitalPosition();
// Update rotation on own axis
m_rotation.y = m_time * m_rotationSpeed;
}
void Moon::render(Shader &shader, unsigned int sphereVAO, int vertexCount)
{
if (m_texture)
{
glActiveTexture(GL_TEXTURE0);
m_texture->bind();
shader.setInt("texture1", 0);
}
// Set model matrix
shader.setMat4("model", getModelMatrix());
// Render the moon
glBindVertexArray(sphereVAO);
glDrawArrays(GL_TRIANGLES, 0, vertexCount);
}
glm::mat4 Moon::getModelMatrix() const
{
glm::mat4 model = glm::mat4(1.0f);
// Apply transformations
model = glm::translate(model, m_position);
model = glm::rotate(model, m_rotation.y, glm::vec3(0.0f, 1.0f, 0.0f));
model = glm::scale(model, glm::vec3(m_radius));
return model;
}
void Moon::updateOrbitalPosition()
{
if (m_parentPlanet)
{
// Get parent planet's position
glm::vec3 planetPos = m_parentPlanet->getPosition();
// Calculate position in orbit around parent planet
m_position.x = planetPos.x + m_orbitRadius * cos(m_orbitAngle);
m_position.z = planetPos.z + m_orbitRadius * sin(m_orbitAngle);
m_position.y = planetPos.y; // Keep moons in same plane as planet for now
}
}