From 05ee3d9b63e19d5521e31ae46ee3292c3f56928e Mon Sep 17 00:00:00 2001 From: TechRunner Date: Mon, 27 Oct 2025 20:38:40 -0500 Subject: [PATCH] Added start to the background physics sim --- index.html | 6 +- javascript/newsim.js | 151 +++++++++++++++++++++++++++++++++++++++++++ pages/simonly.html | 15 +++++ 3 files changed, 169 insertions(+), 3 deletions(-) create mode 100644 javascript/newsim.js create mode 100644 pages/simonly.html diff --git a/index.html b/index.html index d94dbfe..f3b4bfb 100644 --- a/index.html +++ b/index.html @@ -7,11 +7,12 @@ The Void System - + + + -

Welcome to the Void System

@@ -155,7 +156,6 @@
- diff --git a/javascript/newsim.js b/javascript/newsim.js new file mode 100644 index 0000000..4b2e188 --- /dev/null +++ b/javascript/newsim.js @@ -0,0 +1,151 @@ +let images = {}; +let simulator = []; +let maxSpeed = 1; +let backgroundImage; + +let LEFT = [-1,0]; + +const entityTypes = { + METERORITE_SMALL: "meteorite_small", + METERORITE_LARGE: "meteorite_large", + COMET: "comet", +}; + +let Settings = { + windowWrap: true, + physics: true, +} + +function getMass(type){ + switch (type){ + case entityTypes.COMET: return 1400.0; + case entityTypes.METERORITE_SMALL: return 500.0; + case entityTypes.METERORITE_LARGE: return 1000.0; + } +} + +function createEntity(x, y, vx, vy, type){ + let entity = { + x: x, + y: y, + ax: 0.0, + ay: 0.0, + vx: vx, + vy: vy, + rot: 0, + mass: getMass(type), + type: type, + } + simulator.push(entity); +} + + +function getRandomType(){ + let rand = Math.random(); + if (rand > 0.955){ + console.log("Comet made"); + return entityTypes.COMET; + }if(rand > 0.4){ + return entityTypes.METERORITE_SMALL; + }else{ + return entityTypes.METERORITE_LARGE; + } +} + +function physicsUpdate(entity){ + entity.vx += entity.ax; + entity.vy += entity.ay; + entity.x += entity.vx; + entity.y += entity.vy; +} + +function onHit(entity){ + +} + +function drawBackground(refImage){ + let w = windowWidth / refImage.width; + let h = windowHeight / refImage.height; + for (let iy = 0; iy <= h; iy++){ + for (let ix = 0; ix <= w; ix++){ + image(refImage, refImage.width * ix, refImage.height * iy); + } + } +} + +function drawEntity(entity){ + push(); + imageMode(CENTER); + translate(entity.x, entity.y, 0); + rotate((Math.atan2(entity.vx, entity.vy) - Math.atan2(-1,0)) * 180/Math.PI); + image(images[entity.type], 0,0); + pop(); +} + +function preload(){ + images[entityTypes.METERORITE_SMALL] = loadImage("/media/Art/meteorite_small_1.png"); + images[entityTypes.METERORITE_LARGE] = loadImage("/media/Art/Meteorite_Large_1_100.png"); + images[entityTypes.COMET] = loadImage("/media/Art/comet.png"); + //images["ship"] = loadImage("/media/Art/ship.png"); #Make Ship asset for game portion + images["background"] = loadImage("/media/Art/EmptySpace.png"); +} + +function setup(){ + let canvas = createCanvas(windowWidth, windowHeight); + background(255); + describe("A space physics simulator"); + + canvas.position(0,0); + + //Load Entities + let entityCount = 50; + for(let i = 0; i <= entityCount; i+=1){ + createEntity( + Math.floor(Math.random() * windowWidth), + Math.floor(Math.random() * windowHeight), + (Math.random() * maxSpeed * 2)-maxSpeed, + (Math.random() * maxSpeed * 2)-maxSpeed, + getRandomType(), + ); + } +} + +function mouseClicked(){ + console.log(mouseX, mouseY); + simulator.forEach((entity) =>{ + + }) +} + +function draw(){ + //background(images["background"]); + //Draw Tiled Background + drawBackground(images["background"]); + + for(let e = 1; e < simulator.length; e++){ + let entity = simulator[e] + + //Physics Update + if (Settings.physics){ + physicsUpdate(entity) + } + + //Window Wrap + if (Settings.windowWrap){ + if (entity.x > windowWidth){ + entity.x = 0; + } + if (entity.x < 0){ + entity.x = windowWidth; + } + if (entity.y > windowHeight){ + entity.y = 0; + } + if (entity.y < 0){ + entity.y = windowHeight; + } + } + //Draw Entity + drawEntity(entity); + } +} \ No newline at end of file diff --git a/pages/simonly.html b/pages/simonly.html new file mode 100644 index 0000000..ffd94cd --- /dev/null +++ b/pages/simonly.html @@ -0,0 +1,15 @@ + + + + + + + + + + + Background Physics Sim only + + + + \ No newline at end of file