Lunokhod Mars Rover Simulator

by maposaag in Design > Software

16 Views, 1 Favorites, 0 Comments

Lunokhod Mars Rover Simulator

2.png

I wanted a video game simulator of the first space robot that explored the moon in 1970, Lunokhod , and it was the international inspiration for all the following generations of space exploration robots.

Supplies

1.png

computer whit fedora 41

GCC compliler

C libraries

Glut and OpenGL

I prefer C because it is very efficient, lightweight, and much faster to run on any computer than other heavier and inefficient languages.

Complete Code Explanation

lunokhod2-2929700902.jpg

1. PROGRAM STRUCTURE AND HEADERS

#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>

These are the necessary libraries:

  1. GL/glut.h: For OpenGL graphics and window management
  2. stdio.h: For input/output operations (printf)
  3. stdlib.h: For random number generation (rand)
  4. math.h: For mathematical functions (sin, cos)
  5. stdbool.h: For boolean data type

2. GLOBAL VARIABLES AND DATA STRUCTURES

Robot Control Variables:

float robotX = 0.0f, robotY = 0.0f, robotZ = 0.0f;
float robotRotY = 0.0f;
float wheelRotation = 0.0f;
  1. robotX, robotY, robotZ: 3D position of the robot in world coordinates
  2. robotRotY: Rotation angle around Y-axis (facing direction)
  3. wheelRotation: Current rotation of wheels for animation

Camera Control Variables:

float camDistance = 10.0f;
float camAngleX = -25.0f, camAngleY = 0.0f;
int lastMouseX, lastMouseY;
bool mousePressed = false;
  1. camDistance: Distance from camera to robot
  2. camAngleX, camAngleY: Camera rotation angles
  3. lastMouseX, lastMouseY: Store previous mouse position
  4. mousePressed: Track if mouse button is pressed

Star Structure:

typedef struct {
float x, y, z;
float intensity;
} Star;
Star stars[2000];

Represents stars in the sky with position and brightness.

Enhanced Planet Structure:

typedef struct {
float x, y, z;
float size;
float r, g, b;
float rotation;
float rotationSpeed;
bool hasRings;
bool hasClouds;
bool hasCraters;
float atmosphere;
} Planet;
Planet planets[10];

Enhanced planet structure with:

  1. Position, size, and color
  2. Rotation and rotation speed
  3. Special features: rings, clouds, craters, atmosphere

Rock Structure:

typedef struct {
float x, y, z;
float size;
float rotation;
bool isLarge;
} Rock;
Rock rocks[150];

Represents rocks on Martian surface with size variations.

3. INITIALIZATION FUNCTIONS

initStars() - Create Starfield:

void initStars() {
for (int i = 0; i < 1500; i++) {
stars[i].x = (rand() % 400 - 200);
stars[i].y = (rand() % 100);
stars[i].z = (rand() % 400 - 200);
stars[i].intensity = 0.3 + (rand() % 70) / 100.0f;
}
// More stars...
}

Creates 2000 stars with random positions and intensities to fill the sky.

initPlanets() - Solar System Setup:

void initPlanets() {
// Sun - Star with corona
planets[0] = (Planet){-25, 15, -50, 3.5, 1.0, 0.7, 0.1, 0.0, 0.5, false, true, false, 0.8};

// Mercury - Rocky planet with craters
planets[1] = (Planet){15, 8, -40, 0.8, 0.6, 0.5, 0.4, 0.0, 1.2, false, false, true, 0.1};
// More planets...
}

Initializes 8 planets with realistic properties:

  1. Sun: Large, yellow, with corona effect
  2. Rocky planets: Mercury, Mars with craters
  3. Atmospheric planets: Venus, Earth with clouds
  4. Gas giants: Jupiter, Saturn, Uranus with rings

initRocks() - Martian Landscape:

void initRocks() {
int rockIndex = 0;
// Large rocks (20)
// Medium rocks (50)
// Small rocks (80)
}

Creates 150 rocks of varying sizes distributed across the Martian surface.

4. DRAWING FUNCTIONS

drawStars() - Render Starfield:

void drawStars() {
glPointSize(1.0);
glBegin(GL_POINTS);
for (int i = 0; i < 2000; i++) {
glColor3f(stars[i].intensity, stars[i].intensity, stars[i].intensity);
glVertex3f(stars[i].x, stars[i].y, stars[i].z);
}
glEnd();
}

Draws all stars as white points with varying intensity.

drawPlanetWithDetails() - Enhanced Planet Rendering:

void drawPlanetWithDetails(Planet p) {
glPushMatrix();
glTranslatef(p.x, p.y, p.z);
glRotatef(p.rotation, 0, 1, 0);

// Main planet sphere
glColor3f(p.r, p.g, p.b);
glutSolidSphere(p.size, 32, 32);

// Crater details for rocky planets
if (p.hasCraters) {
// Draw crater points...
}

// Cloud layers for atmospheric planets
if (p.hasClouds) {
// Draw cloud sphere...
}

// Atmospheric glow effect
if (p.atmosphere > 0.3) {
// Draw atmosphere layer...
}

// Ring systems for gas giants
if (p.hasRings) {
// Draw multiple rings...
}

// Solar corona for Sun
if (p.r > 0.9 && p.g > 0.6) {
// Draw corona and solar rays...
}

glPopMatrix();
}

This function creates detailed planets with:

  1. High-resolution spheres (32 segments)
  2. Craters for rocky planets
  3. Cloud layers for atmospheric planets
  4. Atmospheric glow effects
  5. Multiple ring systems
  6. Solar corona and rays for the Sun

drawLunokhod() - Robot Rendering:

void drawLunokhod() {
glPushMatrix();
glTranslatef(robotX, robotY + 0.3f, robotZ);
glRotatef(robotRotY, 0, 1, 0);

// Red direction arrow
glDisable(GL_LIGHTING);
glColor3f(1.0, 0.0, 0.0);
glLineWidth(4.0);
glBegin(GL_LINES);
glVertex3f(0, 0.5f, 0);
glVertex3f(2.5f, 0.5f, 0);
glEnd();
// ... more robot components
glPopMatrix();
}

Draws the Soviet Lunokhod robot with:

  1. Main body and compartments
  2. Solar panel
  3. Antenna
  4. 8 wheels that rotate
  5. Red direction arrow
  6. Front camera

5. CAMERA SYSTEM

updateCamera() - Third-Person Camera:

void updateCamera() {
float camX = robotX + camDistance * sin(camAngleY * 3.14159 / 180.0) * cos(camAngleX * 3.14159 / 180.0);
float camY = robotY + 2.0f + camDistance * sin(camAngleX * 3.14159 / 180.0);
float camZ = robotZ + camDistance * cos(camAngleY * 3.14159 / 180.0) * cos(camAngleX * 3.14159 / 180.0);

gluLookAt(camX, camY, camZ,
robotX, robotY + 0.8f, robotZ,
0, 1, 0);
}

Creates a third-person camera that:

  1. Orbits around the robot
  2. Always looks at the robot
  3. Can be controlled with mouse
  4. Maintains consistent distance

6. INPUT HANDLING

keyboard() - Movement Control:

void keyboard(unsigned char key, int x, int y) {
float moveSpeed = 0.3f;
float rotSpeed = 3.0f;

switch(key) {
case 'w': case 'W':
// Forward movement along red arrow direction
robotX += moveSpeed * cos(robotRotY * 3.14159 / 180.0);
robotZ += moveSpeed * sin(robotRotY * 3.14159 / 180.0);
wheelRotation += 20.0f;
break;
case 's': case 'S':
// Backward movement (opposite to red arrow)
robotX -= moveSpeed * cos(robotRotY * 3.14159 / 180.0);
robotZ -= moveSpeed * sin(robotRotY * 3.14159 / 180.0);
wheelRotation -= 20.0f;
break;
case 'a': case 'A':
robotRotY += rotSpeed; // Turn left
break;
case 'd': case 'D':
robotRotY -= rotSpeed; // Turn right
break;
case 'r': case 'R':
// Reset position
break;
}
}

Movement system:

  1. W/S: Move forward/backward along current direction
  2. A/D: Rotate the robot (changes red arrow direction)
  3. R: Reset to starting position
  4. Wheel rotation provides visual feedback

mouse() and motion() - Camera Control:

void mouse(int button, int state, int x, int y) {
if (button == GLUT_LEFT_BUTTON) {
mousePressed = (state == GLUT_DOWN);
lastMouseX = x;
lastMouseY = y;
}
}

void motion(int x, int y) {
if (mousePressed) {
camAngleY += (x - lastMouseX) * 0.5f;
camAngleX += (y - lastMouseY) * 0.5f;
// Limit vertical rotation
if (camAngleX > 80) camAngleX = 80;
if (camAngleX < -80) camAngleX = -80;
lastMouseX = x;
lastMouseY = y;
glutPostRedisplay();
}
}

Mouse controls:

  1. Left click + drag: Rotate camera around robot
  2. Vertical rotation limited to ±80 degrees
  3. Smooth camera movement

7. MAIN RENDERING LOOP

display() - Main Drawing Function:

void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

updateCamera();

// Draw background stars
glDisable(GL_LIGHTING);
drawStars();
glEnable(GL_LIGHTING);

// Draw solar system
for (int i = 0; i < numPlanets; i++) {
drawPlanetWithDetails(planets[i]);
}

// Draw Martian surface and robot
drawMartianSurface();
drawLunokhod();

// Draw HUD information
glDisable(GL_LIGHTING);
// Position and direction info
// Control instructions
// Planet system info
glEnable(GL_LIGHTING);

glutSwapBuffers();
}

The main rendering pipeline:

  1. Clear screen and depth buffer
  2. Update camera position
  3. Draw stars (without lighting)
  4. Draw all planets with lighting
  5. Draw Martian surface and rocks
  6. Draw the robot
  7. Draw HUD text information
  8. Swap buffers for smooth animation

8. ANIMATION AND TIMING

update() - Animation Loop:

void update(int value) {
for (int i = 0; i < numPlanets; i++) {
planets[i].rotation += planets[i].rotationSpeed;
if (planets[i].rotation > 360) {
planets[i].rotation = 0;
}
}
glutPostRedisplay();
glutTimerFunc(100, update, 0);
}

Animation system:

  1. Rotates planets at different speeds
  2. Calls itself every 100ms (10 FPS)
  3. Triggers screen redraw

9. INITIALIZATION AND MAIN FUNCTION

init() - OpenGL Setup:

void init() {
glClearColor(0.0, 0.0, 0.1, 1.0); // Dark blue background
glEnable(GL_DEPTH_TEST); // Enable depth testing
glEnable(GL_BLEND); // Enable transparency
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

// Lighting setup
GLfloat lightPos[] = {-50.0, 50.0, -50.0, 1.0};
GLfloat lightAmbient[] = {0.4, 0.4, 0.4, 1.0};
GLfloat lightDiffuse[] = {0.9, 0.9, 0.9, 1.0};

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuse);

// Initialize game objects
initStars();
initRocks();
initPlanets();
}

main() - Program Entry Point:

int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(1200, 800);
glutInitWindowPosition(100, 100);
glutCreateWindow("Simulador Lunokhod - SISTEMA SOLAR MEJORADO");

init();

// Register callback functions
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutSpecialFunc(specialKeys);
glutTimerFunc(100, update, 0);

printf("=== ENHANCED SOLAR SYSTEM ===\n");
// Print instructions...

glutMainLoop();
return 0;
}

10. KEY FEATURES SUMMARY

Visual Features:

  1. Realistic solar system with 8 detailed planets
  2. 2000 stars in background
  3. 150 rocks on Martian surface
  4. Soviet Lunokhod robot with animated wheels
  5. Third-person camera system
  6. Real-time HUD information

Technical Features:

  1. OpenGL 3D rendering
  2. Lighting and material properties
  3. Transparency effects for atmospheres
  4. Smooth camera controls
  5. Real-time object rotation
  6. Collision-free movement system

Controls:

  1. W/S: Move forward/backward
  2. A/D: Rotate robot
  3. Mouse: Control camera
  4. R: Reset position
  5. +/-: Zoom camera

This simulator creates an immersive experience of exploring Mars with a historically accurate (in the Moon) Soviet rover while observing a detailed solar system in the sky above.


Compiler with: gcc -o lunokhod_simulator lunokhod_simulator.c -lGL -lGLU -lglut -lm

Run with: ./lunokhod_simulator