From 1202cd02211fe6b9666c00318d29e98944791e9f Mon Sep 17 00:00:00 2001 From: TechRunner Date: Wed, 29 Oct 2025 16:39:04 -0500 Subject: [PATCH] Made new entities created use the old locations --- javascript/newsim.js | 77 +++++++++++++++++++++++++++++++++----------- 1 file changed, 59 insertions(+), 18 deletions(-) diff --git a/javascript/newsim.js b/javascript/newsim.js index b8b8f1c..8b7dec4 100644 --- a/javascript/newsim.js +++ b/javascript/newsim.js @@ -24,8 +24,28 @@ function getMass(type){ } } -function createEntity(x, y, vx, vy, ax, ay, rot, rotv, type){ - let entity = { +function createEntity(x, y, vx, vy, ax, ay, rot, rotv, type, id=0, iframes=Math.floor(Math.random()*100)){ + if (Settings.emptyEntries.length > 1){ + var entryID = Settings.emptyEntries.pop(); + if (Settings.devMode) console.warn("Creating new enity from old", entryID); + if (entryID <= simulator.length && entryID >= 0){ + entity = simulator[entryID]; + entity.x = x; + entity.y = y; + entity.vx = vx; + entity.vy = vy; + entity.ax = ax; + entity.ay = ay; + entity.rot = rot; + entity.rotv = rotv; + entity.iframes = 1000; + entity.show = true; + entity.type = type + }else{ + if (Settings.devMode) console.error("Entry ID out of bounds", Settings.emptyEntries.length, id, entryID); + } + }else{ + simulator.push({ x: x, y: y, ax: ax, @@ -38,8 +58,11 @@ function createEntity(x, y, vx, vy, ax, ay, rot, rotv, type){ forcex: 0.0, forcey: 0.0, type: type, + show: true, + ueid: id, + iframes: iframes + }); } - simulator.push(entity); } @@ -56,29 +79,47 @@ function getRandomType(){ } function physicsUpdate(entity){ + if (entity.show){ entity.vx += entity.ax; entity.vy += entity.ay; entity.x += entity.vx; entity.y += entity.vy; entity.rot += entity.rotv; - + if (entity.iframes > 0) entity.iframes -= 1; + if (entity.iframes == 0) simulator.forEach((other_entity) =>{ + checkCollision(entity, other_entity); + }); + } } -function onHit(entity1, entity2){ - if(entity1.type == entityTypes.METERORITE_LARGE){ - createEntity(entity1.x, entity1.y, entity2.vx-entity1.vx, entity2.vy-entity1.vy, 0.0,0.0,entity2.rot-entity1.rot, entity2.rotv-entity1.rotv, entityTypes.METERORITE_SMALL); - }if(entity1.type == entityTypes.COMET){ +function checkCollision(entity1, entity2){ + if (entity1.type == entityTypes.COMET && entity2.type == entityTypes.COMET){ - }else{ + }else if (entity1.ueid != entity2.ueid + && entity1.show + && entity2.show){ + var dist = distance(entity1, entity2); + if(dist < 15){ + if (Settings.devMode) { + stroke('magenta'); + strokeWeight(5); + line(entity1.x,entity1.y, entity2.x, entity2.y); + console.log("Collision at", entity1.x, entity1.y, entity1.iframes, entity2.x, entity2.y, dist); + } Explode(entity1); + Explode(entity2); + } + } } - if(entity2.type == entityTypes.METERORITE_LARGE){ - createEntity(entity2.x, entity2.y, entity1.vx-entity2.vx, entity1.vy-entity2.vy, 0.0,0.0,entity2.rot-entity1.rot, entity2.rotv-entity1.rotv, entityTypes.METERORITE_SMALL); - }if(entity1.type == entityTypes.COMET){ - - }else{ - Explode(entity2); +function Explode(entity){ + if(entity.type == entityTypes.METERORITE_SMALL){ + entity.show = false; + Settings.emptyEntries.push(entity.ueid); + }if(entity.type == entityTypes.METERORITE_LARGE){ + entity.type = entityTypes.METERORITE_SMALL + entity.iframes = 1000; + createEntity(entity.x, entity.y, entity.vx + Math.random(), entity.vy + Math.random(), 0.0,0.0,0.0,0.0, entityTypes.METERORITE_SMALL, id=entity.ueid, iframes=1000); } } @@ -127,9 +168,8 @@ function setup(){ canvas.position(0,0); worldScale = createSlider(); //Load Entities - let entityCount = 50; - let rotationSpeed = 100.0; - for(let i = 0; i <= entityCount; i+=1){ + + for(var i = 0; i <= entityCount; i+=1){ createEntity( Math.floor(Math.random() * windowWidth), //X Location Math.floor(Math.random() * windowHeight), //Y Location @@ -140,6 +180,7 @@ function setup(){ 0.0, //Current Rotation (Math.random() * 2 *(Math.PI / rotationSpeed))-(Math.PI / rotationSpeed), //Rotation Velocity getRandomType(), + id=i ); } }