üç boyutlu uzay ve kütleçekimle etkileşen küreler

entry1 galeri
    1.
  1. yaşadığımız dünyanın simülasyon olup olmadığını sorgulatan karelere evsahipliği yapar.



    #include <GL/glut.h>
    #include <glm/glm.hpp>
    #include <cmath>
    #include <vector>
    #include <cstdlib>
    #include <ctime>

    using namespace glm;

    // Fizik sabitleri
    const float G = 6.67430e-11f; // Kütleçekim sabiti
    const float dt = 20.51f; // Zaman adımı
    const int NUM_SPHERES = 402; // 100 rastgele küre ve 2 başlangıç küresi

    // Ekran boyutları
    int windowWidth = 1920;
    int windowHeight = 1080;

    // Küre yapısı
    struct Sphere {
    vec3 position;
    vec3 velocity;
    vec3 acceleration;
    float mass;
    float radius;
    float angularVelocity; // Açısal hız
    float spin; // Dönme açısı
    vec3 color; // Renk
    };

    // Küre tanımları
    std::vector<Sphere> spheres;

    // Işık kaynağı
    vec3 lightPosition = vec3(0.8f, -0.8f, 0.8f);

    // Kütleçekim kuvvetini hesaplayan fonksiyon
    vec3 computeGravitationalForce(Sphere& a, Sphere& b) {
    vec3 direction = b.position - a.position;
    float distance = length(direction);

    // Kütleçekim kuvveti, mesafe sıfıra inmemeli
    if (distance == 0) return vec3(0.0f);

    vec3 force = G * a.mass * b.mass / (distance * distance) * normalize(direction);
    return force;
    }

    // Güncelleme fonksiyonu
    void updatePhysics() {
    for (size_t i = 0; i < spheres.size(); ++i) {
    spheres[i].acceleration = vec3(0.0f); // Her küre için ivmeyi sıfırla
    }

    // Kütleçekim kuvvetlerini hesapla
    for (size_t i = 0; i < spheres.size(); ++i) {
    for (size_t j = i + 1; j < spheres.size(); ++j) {
    vec3 force = computeGravitationalForce(spheres[i], spheres[j]);
    spheres[i].acceleration += force / spheres[i].mass;
    spheres[j].acceleration -= force / spheres[j].mass; // Üzerine etki eden kuvvetin zıttı
    }
    }

    // Hız ve konum güncellemeleri
    for (Sphere& sphere : spheres) {
    sphere.velocity += sphere.acceleration * dt;
    sphere.position += sphere.velocity * dt;

    // Ekranın dışına çıkma kontrolü
    if (sphere.position.x < -1.0f - sphere.radius) {
    sphere.position.x = 1.0f + sphere.radius; // Sol dışına çıktıysa sağdan gir
    } else if (sphere.position.x > 1.0f + sphere.radius) {
    sphere.position.x = -1.0f - sphere.radius; // Sağ dışına çıktıysa soldan gir
    }

    if (sphere.position.y < -1.0f - sphere.radius) {
    sphere.position.y = 1.0f + sphere.radius; // Alt dışına çıktıysa üstten gir
    } else if (sphere.position.y > 1.0f + sphere.radius) {
    sphere.position.y = -1.0f - sphere.radius; // Üst dışına çıktıysa alttan gir
    }

    // Açısal dönüşü güncelle
    sphere.spin += sphere.angularVelocity * dt;
    }
    }

    // Aydınlatmayı ayarla
    void setupLighting() {
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);

    GLfloat lightPos[] = {lightPosition.x, lightPosition.y, lightPosition.z, 1.0f};
    GLfloat lightColor[] = {1.0f, 1.0f, 0.8f, 1.0f}; // Sarımsı ışık
    glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor);
    }

    // Küreleri çizen fonksiyon
    void drawSphere(const Sphere& sphere) {
    glPushMatrix();
    glTranslatef(sphere.position.x, sphere.position.y, sphere.position.z);
    glRotatef(sphere.spin * (180.0f / M_PI), 0.0f, 0.0f, 1.0f); // Z ekseni etrafında döndür

    // Renk ataması
    GLfloat color[] = {sphere.color.r, sphere.color.g, sphere.color.b, 1.0f};
    glMaterialfv(GL_FRONT, GL_DIFFUSE, color);

    glutSolidSphere(sphere.radius, 50, 50);
    glPopMatrix();
    }

    // Çizim fonksiyonu
    void display() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    setupLighting();

    // Küreleri çiz
    for (const Sphere& sphere : spheres) {
    drawSphere(sphere);
    }

    glutSwapBuffers();
    }

    // Ana döngü
    void idle() {
    updatePhysics();
    glutPostRedisplay();
    }

    // Rastgele bir renk oluşturma fonksiyonu
    vec3 randomColor() {
    return vec3(static_cast<float>(rand()) / RAND_MAX, // Renk bileşeni 0.0 ile 1.0 arasında
    static_cast<float>(rand()) / RAND_MAX,
    static_cast<float>(rand()) / RAND_MAX);
    }

    // Başlangıç ayarları
    void init() {
    glEnable(GL_DEPTH_TEST);
    glClearColor(0.1f, 0.1f, 0.1f, 1.0f);

    // Rastgele küreleri oluştur
    std::srand(static_cast<unsigned int>(std::time(0))); // Rastgele sayı üreteci için başlangıç
    for (int i = 0; i < NUM_SPHERES; ++i) {
    float radius = static_cast<float>(rand() % 5 + 1) * 0.01f; // Küre yarıçapı 0.001 ile 0.08 arasında
    float mass = radius * 10.0f; // Kütle, yarıçap ile orantılı
    vec3 position = vec3(static_cast<float>(rand()) / RAND_MAX * 2.0f - 1.0f, // -1 ile 1 arasında rastgele pozisyon
    static_cast<float>(rand()) / RAND_MAX * 2.0f - 1.0f,
    0.0f);
    vec3 velocity = vec3(static_cast<float>(rand()) / RAND_MAX * 0.02f - 0.01f, // -0.01 ile 0.01 arasında rastgele hız
    static_cast<float>(rand()) / RAND_MAX * 0.02f - 0.01f,
    0.0f);
    float angularVelocity = static_cast<float>(rand()) / RAND_MAX * 2.0f - 1.0f; // Rastgele açısal hız

    // Rastgele renk atama
    vec3 color = randomColor();

    spheres.push_back({position, velocity, vec3(0.0f), mass, radius, angularVelocity, 0.0f, color});
    }

    // ilk iki küreyi başlangıçta ekleyin
    spheres.insert(spheres.begin(), {
    {vec3(-0.5f, 0.0f, 0.0f), vec3(0.0f, 0.0f, 0.0f), vec3(0.0f), 5.0f, 0.1f, 1.0f, 0.0f, vec3(1.0f, 0.0f, 0.0f)}, // Kırmızı
    {vec3(0.5f, 0.0f, 0.0f), vec3(0.0f, 0.0f, 0.0f), vec3(0.0f), 5.0f, 0.1f, -1.0f, 0.0f, vec3(0.0f, 0.0f, 1.0f)} // Mavi
    });
    }

    // Program başlangıcı
    int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(windowWidth, windowHeight);
    glutCreateWindow("Kütleçekim Simülasyonu");

    init();
    glutDisplayFunc(display);
    glutIdleFunc(idle);
    glutMainLoop();

    return 0;
    }
    2 ...