Skip to content

Some code I worked on #226

@XXOTIC-1

Description

@XXOTIC-1

"""
Quantum Spacetime Simulation
Author: A. Rahman
Description:
Real-time visualization of dynamic quantum spacetime distortions.
Built with PyOpenGL + Pygame. Features gravity wells, Perlin noise fields,
tunneling events, and cinematic camera control.
"""

import sys, math, random, time
import numpy as np
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
from noise import pnoise3

======================================================

Core Config

======================================================

PARTICLE_COUNT = 5000
FIELD_RADIUS = 100
WARP_INTENSITY = 0.075
GRAVITY_WELL_COUNT = 3
SINGULARITY_PULL = 0.0035
TUNNELING_RATE = 0.0018
TIME_LOOP_FACTOR = 0.55
COLOR_SHIFT = 0.012
CAMERA_RADIUS = 320

======================================================

Particle

======================================================

class Particle:
slots = ('pos', 'vel', 'color')

def __init__(self):
    self.pos = np.array([
        random.uniform(-FIELD_RADIUS, FIELD_RADIUS),
        random.uniform(-FIELD_RADIUS, FIELD_RADIUS),
        random.uniform(-FIELD_RADIUS, FIELD_RADIUS)
    ], dtype=np.float32)

    self.vel = np.zeros(3, dtype=np.float32)
    self.color = [random.random(), random.random(), 1.0]

def update(self, wells, t):
    # Spacetime turbulence — Perlin noise as velocity field
    noise_vec = np.array([
        pnoise3(self.pos[0]*0.022, self.pos[1]*0.022, t),
        pnoise3(self.pos[1]*0.022, self.pos[2]*0.022, t+57),
        pnoise3(self.pos[2]*0.022, self.pos[0]*0.022, t+131)
    ]) * WARP_INTENSITY
    self.vel += noise_vec

    # Gravity well interaction
    for w in wells:
        delta = w - self.pos
        dist = np.linalg.norm(delta) + 0.001
        self.vel += (delta / (dist ** 1.5)) * SINGULARITY_PULL

    # Tunneling / teleportation
    if random.random() < TUNNELING_RATE:
        self.pos[:] = np.random.uniform(-FIELD_RADIUS, FIELD_RADIUS, 3)
        self.vel *= 0

    self.pos += self.vel
    self.vel *= 0.985  # dampen motion slightly

    # Looping boundary
    if np.linalg.norm(self.pos) > FIELD_RADIUS * 2:
        self.pos *= TIME_LOOP_FACTOR

    # Subtle color drift
    self.color[0] = (self.color[0] + COLOR_SHIFT) % 1
    self.color[1] = (self.color[1] + COLOR_SHIFT * 0.5) % 1
    self.color[2] = (self.color[2] + COLOR_SHIFT * 0.25) % 1

======================================================

Gravity Wells

======================================================

def generate_wells(n):
wells = []
for _ in range(n):
wells.append(np.array([
random.uniform(-50, 50),
random.uniform(-50, 50),
random.uniform(-50, 50)
], dtype=np.float32))
return wells

======================================================

Camera System

======================================================

class Camera:
def init(self):
self.angle = 0
self.height = 0

def update(self):
    self.angle += 0.018
    self.height = math.sin(self.angle * 0.25) * 40
    glLoadIdentity()
    gluLookAt(
        math.sin(self.angle) * CAMERA_RADIUS,
        self.height,
        math.cos(self.angle) * CAMERA_RADIUS,
        0, 0, 0,
        0, 1, 0
    )

======================================================

Initialization

======================================================

def init_display():
pygame.init()
display = (1280, 720)
pygame.display.set_mode(display, DOUBLEBUF | OPENGL)
gluPerspective(70, (display[0] / display[1]), 0.1, 2000.0)
glEnable(GL_POINT_SMOOTH)
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
glPointSize(2)

======================================================

Main Loop

======================================================

def main():
init_display()

particles = [Particle() for _ in range(PARTICLE_COUNT)]
wells = generate_wells(GRAVITY_WELL_COUNT)
cam = Camera()
clock = pygame.time.Clock()
t = 0.0

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    cam.update()

    glBegin(GL_POINTS)
    for p in particles:
        p.update(wells, t)
        glColor3f(*p.color)
        glVertex3f(p.pos[0], p.pos[1], p.pos[2])
    glEnd()

    pygame.display.flip()
    clock.tick(60)
    t += 0.01

if name == "main":
main()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions