• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Simulating Gravity And Black Holes In C++ (Amazing)

Real men code using a hex editor creating the binary directly. You start by typing MZ, then a bunch of other steps, then you have a program that works.

I joke but when I was a kid that's actually how I thought it was done before I knew about the concept of coding and compiling/linking.
 
Real men code using a hex editor creating the binary directly. You start by typing MZ, then a bunch of other steps, then you have a program that works.

I joke but when I was a kid that's actually how I thought it was done before I knew about the concept of coding and compiling/linking.
There was a period before invention of programming languages, when binary had to be "written" directly.
 
Below is ChatGPT simulating the Solar System in C++:

Code:
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <iostream>
#include <vector>
#include <cmath>

const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;

void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow *window);

// Orbital params
struct Planet {
    float distance;  // from sun
    float size;
    float speed;     // angular speed
    glm::vec3 color;
};

// Planets
std::vector<Planet> planets = {
    { 0.4f, 0.05f, 4.7f, glm::vec3(0.5f, 0.5f, 0.5f) }, // Mercury
    { 0.7f, 0.08f, 3.5f, glm::vec3(0.8f, 0.6f, 0.2f) }, // Venus
    { 1.0f, 0.09f, 2.9f, glm::vec3(0.2f, 0.5f, 1.0f) }, // Earth
    { 1.5f, 0.06f, 2.4f, glm::vec3(1.0f, 0.2f, 0.2f) }, // Mars
    { 2.5f, 0.15f, 1.3f, glm::vec3(1.0f, 0.8f, 0.2f) }, // Jupiter (scaled down)
};

int main() {
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "Solar System 3D", NULL, NULL);
    if (!window) {
        std::cerr << "Failed to create window.\n";
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);
    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
        std::cerr << "Failed to initialize GLAD\n";
        return -1;
    }

    glEnable(GL_DEPTH_TEST);

    // Simplified rendering loop
    float time = 0.0f;

    while (!glfwWindowShouldClose(window)) {
        processInput(window);
        time += 0.01f;

        glClearColor(0.0f, 0.0f, 0.05f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        // Projection and view
        glm::mat4 projection = glm::perspective(glm::radians(45.0f),
                                (float)SCR_WIDTH / SCR_HEIGHT, 0.1f, 100.0f);
        glm::mat4 view = glm::lookAt(glm::vec3(0, 5, 10),
                                     glm::vec3(0, 0, 0),
                                     glm::vec3(0, 1, 0));

        // Draw Sun
        // Draw planet positions
        for (auto& planet : planets) {
            float angle = time * planet.speed;
            float x = cos(angle) * planet.distance;
            float z = sin(angle) * planet.distance;

            // Replace this with your drawSphere(x, 0, z, planet.size)
        }

        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

// Resize window
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
    glViewport(0, 0, width, height);
}

// Close on Esc
void processInput(GLFWwindow *window) {
    if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
        glfwSetWindowShouldClose(window, true);
}

Below is ChatGPT simulating the milky way in c++



Code:
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <vector>
#include <cstdlib>
#include <ctime>

const int STAR_COUNT = 5000;
struct Star {
    glm::vec3 position;
    float angle; // for rotation
};

std::vector<Star> stars;

void generateStars() {
    srand(time(0));
    for (int i = 0; i < STAR_COUNT; i++) {
        float radius = static_cast<float>(rand()) / RAND_MAX * 50.0f;
        float angle = static_cast<float>(rand()) / RAND_MAX * 2 * 3.14159f;
        float height = (static_cast<float>(rand()) / RAND_MAX - 0.5f) * 2.0f; // vertical spread

        Star star;
        star.position = glm::vec3(radius * cos(angle), height, radius * sin(angle));
        star.angle = angle;
        stars.push_back(star);
    }
}

void updateStars(float deltaTime) {
    for (auto& star : stars) {
        star.angle += deltaTime * 0.1f;
        float radius = glm::length(glm::vec2(star.position.x, star.position.z));
        star.position.x = radius * cos(star.angle);
        star.position.z = radius * sin(star.angle);
    }
}

void drawStars() {
    glBegin(GL_POINTS);
    for (const auto& star : stars) {
        glVertex3f(star.position.x, star.position.y, star.position.z);
    }
    glEnd();
}

int main() {
    if (!glfwInit()) return -1;
    GLFWwindow* window = glfwCreateWindow(800, 600, "Milky Way 3D", nullptr, nullptr);
    if (!window) return -1;
    glfwMakeContextCurrent(window);
    glewInit();

    glEnable(GL_DEPTH_TEST);
    glPointSize(1.5f);

    generateStars();
    float lastTime = glfwGetTime();

    while (!glfwWindowShouldClose(window)) {
        float currentTime = glfwGetTime();
        float deltaTime = currentTime - lastTime;
        lastTime = currentTime;

        updateStars(deltaTime);

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(60.0, 800.0 / 600.0, 0.1, 500.0);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        gluLookAt(0, 20, 100, 0, 0, 0, 0, 1, 0);

        drawStars();

        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glfwDestroyWindow(window);
    glfwTerminate();
    return 0;
}
 
Last edited:
Below is ChatGPT simulating the Solar System in C++:

Code:
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <iostream>
#include <vector>
#include <cmath>

const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;

void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow *window);

// Orbital params
struct Planet {
    float distance;  // from sun
    float size;
    float speed;     // angular speed
    glm::vec3 color;
};

// Planets
std::vector<Planet> planets = {
    { 0.4f, 0.05f, 4.7f, glm::vec3(0.5f, 0.5f, 0.5f) }, // Mercury
    { 0.7f, 0.08f, 3.5f, glm::vec3(0.8f, 0.6f, 0.2f) }, // Venus
    { 1.0f, 0.09f, 2.9f, glm::vec3(0.2f, 0.5f, 1.0f) }, // Earth
    { 1.5f, 0.06f, 2.4f, glm::vec3(1.0f, 0.2f, 0.2f) }, // Mars
    { 2.5f, 0.15f, 1.3f, glm::vec3(1.0f, 0.8f, 0.2f) }, // Jupiter (scaled down)
};

int main() {
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "Solar System 3D", NULL, NULL);
    if (!window) {
        std::cerr << "Failed to create window.\n";
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);
    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
        std::cerr << "Failed to initialize GLAD\n";
        return -1;
    }

    glEnable(GL_DEPTH_TEST);

    // Simplified rendering loop
    float time = 0.0f;

    while (!glfwWindowShouldClose(window)) {
        processInput(window);
        time += 0.01f;

        glClearColor(0.0f, 0.0f, 0.05f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        // Projection and view
        glm::mat4 projection = glm::perspective(glm::radians(45.0f),
                                (float)SCR_WIDTH / SCR_HEIGHT, 0.1f, 100.0f);
        glm::mat4 view = glm::lookAt(glm::vec3(0, 5, 10),
                                     glm::vec3(0, 0, 0),
                                     glm::vec3(0, 1, 0));

        // Draw Sun
        // Draw planet positions
        for (auto& planet : planets) {
            float angle = time * planet.speed;
            float x = cos(angle) * planet.distance;
            float z = sin(angle) * planet.distance;

            // Replace this with your drawSphere(x, 0, z, planet.size)
        }

        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

// Resize window
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
    glViewport(0, 0, width, height);
}

// Close on Esc
void processInput(GLFWwindow *window) {
    if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
        glfwSetWindowShouldClose(window, true);
}

Below is ChatGPT simulating the milky way in c++



Code:
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <vector>
#include <cstdlib>
#include <ctime>

const int STAR_COUNT = 5000;
struct Star {
    glm::vec3 position;
    float angle; // for rotation
};

std::vector<Star> stars;

void generateStars() {
    srand(time(0));
    for (int i = 0; i < STAR_COUNT; i++) {
        float radius = static_cast<float>(rand()) / RAND_MAX * 50.0f;
        float angle = static_cast<float>(rand()) / RAND_MAX * 2 * 3.14159f;
        float height = (static_cast<float>(rand()) / RAND_MAX - 0.5f) * 2.0f; // vertical spread

        Star star;
        star.position = glm::vec3(radius * cos(angle), height, radius * sin(angle));
        star.angle = angle;
        stars.push_back(star);
    }
}

void updateStars(float deltaTime) {
    for (auto& star : stars) {
        star.angle += deltaTime * 0.1f;
        float radius = glm::length(glm::vec2(star.position.x, star.position.z));
        star.position.x = radius * cos(star.angle);
        star.position.z = radius * sin(star.angle);
    }
}

void drawStars() {
    glBegin(GL_POINTS);
    for (const auto& star : stars) {
        glVertex3f(star.position.x, star.position.y, star.position.z);
    }
    glEnd();
}

int main() {
    if (!glfwInit()) return -1;
    GLFWwindow* window = glfwCreateWindow(800, 600, "Milky Way 3D", nullptr, nullptr);
    if (!window) return -1;
    glfwMakeContextCurrent(window);
    glewInit();

    glEnable(GL_DEPTH_TEST);
    glPointSize(1.5f);

    generateStars();
    float lastTime = glfwGetTime();

    while (!glfwWindowShouldClose(window)) {
        float currentTime = glfwGetTime();
        float deltaTime = currentTime - lastTime;
        lastTime = currentTime;

        updateStars(deltaTime);

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(60.0, 800.0 / 600.0, 0.1, 500.0);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        gluLookAt(0, 20, 100, 0, 0, 0, 0, 1, 0);

        drawStars();

        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glfwDestroyWindow(window);
    glfwTerminate();
    return 0;
}
Have you run the code to see how it behaves?
 
Real men code using a hex editor creating the binary directly. You start by typing MZ, then a bunch of other steps, then you have a program that works.

I joke but when I was a kid that's actually how I thought it was done before I knew about the concept of coding and compiling/linking.
Nobody added the obligatory XKCD yet?
real_programmers.png
 
Typically in programming, this is when we'd begin troubleshooting. You may not have the necessary include files.
Yep. I gave it a shot. The code seems to require a custom include file generated by glad, the contents of which would be hard to guess. 🙁
 
Real men code using a hex editor creating the binary directly. You start by typing MZ, then a bunch of other steps, then you have a program that works.

I joke but when I was a kid that's actually how I thought it was done before I knew about the concept of coding and compiling/linking.
Real men program in Knuth's MIX with self modifying code
 
It’s funny that your GPT code scaled down Jupiter. This is a great video on solar system modeling and how Jupiter causes problems

 
Now the same guy just released a vid on simulating atoms in C++.

I've tried to install opengl but it wasn't working.

 
Back
Top