Simulating Gravity In C++ (Amazing)

Gizmo j

Golden Member
Nov 9, 2013
1,498
410
136



I posted this in my other thread but I thought this video deserved it's own thread.

He's currently making a video simulating a black hole in C++.
 

Red Squirrel

No Lifer
May 24, 2003
70,193
13,579
126
www.anyf.ca
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.
 

mv2devnull

Golden Member
Apr 13, 2010
1,526
160
106
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.
 

Gizmo j

Golden Member
Nov 9, 2013
1,498
410
136
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:
  • Like
Reactions: Kaido

nakedfrog

No Lifer
Apr 3, 2001
61,883
17,629
136
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?
 
  • Haha
  • Like
Reactions: yottabit and Ken g6

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,633
4,562
75
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
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,633
4,562
75
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. :(
 

SteveGrabowski

Diamond Member
Oct 20, 2014
8,752
7,357
136
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
 
  • Wow
Reactions: Ken g6

yottabit

Golden Member
Jun 5, 2008
1,622
749
146
It’s funny that your GPT code scaled down Jupiter. This is a great video on solar system modeling and how Jupiter causes problems