From b72d20fe0f8fadc46ad71776cf10ec7b763389c3 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sat, 5 May 2018 15:53:47 +0200 Subject: [PATCH] Anti Gravity done --- .vs/AAWeb/v15/.suo | Bin 69632 -> 69120 bytes FloatingSpace/FloatingLayer.js | 111 +++++++++++++++++++++++++++------ server.js | 2 +- 3 files changed, 93 insertions(+), 20 deletions(-) diff --git a/.vs/AAWeb/v15/.suo b/.vs/AAWeb/v15/.suo index 5417763eeb2d063ec306c6a6d47864efd1f98bef..14b1d4a953b725cc4d14cac513babaf68b105c39 100644 GIT binary patch delta 526 zcmZozz|t^>WrD%v4|$vt0t^hI3=9m+K>Y9j|NlUWff0ypHU>&Fu_}C)c~ZW)km)xw z>jm{UT|g!qy8xrf=1Q^uwv5J`D?{21C-ZijO&&hf{oRimgYcMXHtk{-2 z`M@mR$x*X3HfOb4F*7a!3YAX&uv%d9`H2dfox8U&E$U;M+%Us?^7;t^lW!f>*jzKg zok{dJ$Z{}HI>5xlz#t1`yG&MSTRypda@C?KER$^~OHCG@;XK)QHV>CFP*NSFU@p`? zw#oM&yxM$f=3>Ul#j|_4jrkcEgn{zx3@Ek(jT6{>V2%UdWP`Tpj3B)ki`GaG21A{P7 z4Lbwe+R0{Z;+qf5ap0TW*zP%bZMWg%fQfC3)<_U?`%x=Kgxe>3b?;qtf`d6VDQ5Eh zZj;S+7Y;E`n#8{73KOF}&;|OF_e~U->^`X(k9Q_d)YjOnapML%c28~2e~`o|$N&!w zr2~RMcKGIpGbb@l&Y3-hTL-t-7#9J(kL0__ie)yF&)pZBTyU3ba@~E7%_%SDs4nAS GWC8%)LA9d* diff --git a/FloatingSpace/FloatingLayer.js b/FloatingSpace/FloatingLayer.js index 4663efac3..26a4fdaf7 100644 --- a/FloatingSpace/FloatingLayer.js +++ b/FloatingSpace/FloatingLayer.js @@ -200,17 +200,20 @@ function newFloatingLayer() { } else { floatingObject.currentRadius = floatingObject.currentRadius - .5; } - } // The imageSize also have a target. - if (floatingObject.currentImageSize < floatingObject.targetImageSize) { - floatingObject.currentImageSize = floatingObject.currentImageSize + 1; - } else { - floatingObject.currentImageSize = floatingObject.currentImageSize - 1; + if (Math.abs(floatingObject.currentImageSize - floatingObject.targetImageSize) >= 1) { + + if (floatingObject.currentImageSize < floatingObject.targetImageSize) { + floatingObject.currentImageSize = floatingObject.currentImageSize + 1; + } else { + floatingObject.currentImageSize = floatingObject.currentImageSize - 1; + } } + /* Collision Control */ for (let k = i + 1; k < thisObject.floatingObjects.length; k++) { @@ -224,7 +227,9 @@ function newFloatingLayer() { /* Calculate repulsion force produced by all other floatingObjects */ - repulsionForce(i); + currentRepulsionForce(i); + + targetRepulsionForce(i); gravityForce(floatingObject, payload); @@ -356,7 +361,6 @@ function newFloatingLayer() { const coulomb = .00001; const minForce = 0.01; - const minDistance = 200; var d = Math.sqrt(Math.pow(payload.position.x - floatingObject.currentPosition.x, 2) + Math.pow(payload.position.y - floatingObject.currentPosition.y, 2)); // ... we calculate the distance ... @@ -393,21 +397,12 @@ function newFloatingLayer() { /* We add the force vector to the speed vector */ - if (d > minDistance) { - - floatingObject.currentSpeed.x = floatingObject.currentSpeed.x + forceVector.x; - floatingObject.currentSpeed.y = floatingObject.currentSpeed.y + forceVector.y; - - } else { - - floatingObject.currentSpeed.x = floatingObject.currentSpeed.x - forceVector.x * 2; - floatingObject.currentSpeed.y = floatingObject.currentSpeed.y - forceVector.y * 2; - } - + floatingObject.currentSpeed.x = floatingObject.currentSpeed.x + forceVector.x; + floatingObject.currentSpeed.y = floatingObject.currentSpeed.y + forceVector.y; } - function repulsionForce(currentFloatingObject) { + function currentRepulsionForce(currentFloatingObject) { /* We generate a repulsion force between floatingObjects, that prevents them to be collisioning so often. */ @@ -467,6 +462,84 @@ function newFloatingLayer() { } + function targetRepulsionForce(currentFloatingObject) { + + /* We generate a repulsion force between floatingObjects, that prevents them to be collisioning so often. */ + + const coulomb = 2; + + var floatingObject1 = thisObject.floatingObjects[currentFloatingObject]; + + for (var i = 0; i < thisObject.floatingObjects.length; i++) { // The force to be applied is considering all other floatingObjects... + + var floatingObject2 = thisObject.floatingObjects[i]; // So, for each floatingObject... + + let payload = { + position: undefined + }; + + switch (floatingObject2.type) { + + case "Profile Ball": { + + payload.position = floatingObject2.payload.profile.position; + break; + } + case "Bubble": { + + payload.position = floatingObject2.payload.bubbles[floatingObject2.payloadBubbleIndex].position; + break; + } + default: { + + break; + } + } + + var d = Math.sqrt(Math.pow(payload.position.x - floatingObject1.currentPosition.x, 2) + Math.pow(payload.position.y - floatingObject1.currentPosition.y, 2)); // ... we calculate the distance ... + + var force = coulomb * floatingObject2.currentMass / (d * d); // ... and with it the repulsion force. + + /* We need to put a hard limit to this force, in order to to eject very little floatingObjects to the infinite and beyond. */ + + if (force > 0.0005) { + force = 0.0005; + } + + var pos1 = { + x: floatingObject1.currentPosition.x, + y: floatingObject1.currentPosition.y + }; + + var pos2 = { + x: payload.position.x, + y: payload.position.y + }; + + var posDiff = { // Next we need the vector resulting from the 2 positions. + x: pos2.x - pos1.x, + y: pos2.y - pos1.y + }; + + var unitVector = { // To find the unit vector, we divide each component by the magnitude of the vector. + x: posDiff.x / d, + y: posDiff.y / d + }; + + var forceVector = { + x: unitVector.x * force, + y: unitVector.y * force + }; + + /* We substract the force vector to the speed vector of the current floatingObject */ + + floatingObject1.currentSpeed.x = floatingObject1.currentSpeed.x - forceVector.x; + floatingObject1.currentSpeed.y = floatingObject1.currentSpeed.y - forceVector.y; + + } + + } + function colliding(floatingObject1, floatingObject2) { /* This function detects weather 2 floatingObjects collide with each other. */ diff --git a/server.js b/server.js index 626140bfd..f4b180fca 100644 --- a/server.js +++ b/server.js @@ -6,7 +6,7 @@ if (CONSOLE_LOG === true) { } -const DEBUG_MODE = true; // This forces the server to read Plotters from the local drive. +const DEBUG_MODE = false; // This forces the server to read Plotters from the local drive. if (CONSOLE_LOG === true && DEBUG_MODE === true) {