From 3e1863387aa46a58b26eed129a2a7bbc571aaec5 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Wed, 16 May 2018 21:31:28 +0200 Subject: [PATCH] More error handling on Floating Space --- FloatingSpace/FloatingLayer.js | 1020 +++++++++++++++++--------------- 1 file changed, 537 insertions(+), 483 deletions(-) diff --git a/FloatingSpace/FloatingLayer.js b/FloatingSpace/FloatingLayer.js index 24b47ceb5..53a45a781 100644 --- a/FloatingSpace/FloatingLayer.js +++ b/FloatingSpace/FloatingLayer.js @@ -66,7 +66,7 @@ function newFloatingLayer() { } } - function killFloatingObject(pFloatingObjectHandle) { + function killFloatingObject(pFloatingObjectHandle, callBackFunction) { try { @@ -169,8 +169,8 @@ function newFloatingLayer() { dyingFloatingObjects.push(pFloatingObject); - if (INFO_LOG === true) { logger.write("[INFO] killFloatingObject -> Adding floatingObject to dyingFloatingObjects."); } - if (INFO_LOG === true) { logger.write("[INFO] killFloatingObject -> dyingFloatingObjects.length = " + dyingFloatingObjects.length); } + if (INFO_LOG === true) { logger.write("[INFO] killFloatingObject -> sendToDie -> Adding floatingObject to dyingFloatingObjects."); } + if (INFO_LOG === true) { logger.write("[INFO] killFloatingObject -> sendToDie -> dyingFloatingObjects.length = " + dyingFloatingObjects.length); } if (callBackFunction !== undefined) { callBackFunction(GLOBAL.DEFAULT_OK_RESPONSE); } @@ -231,7 +231,6 @@ function newFloatingLayer() { } } catch (err) { - if (ERROR_LOG === true) { logger.write("[ERROR] getFloatingObject -> err = " + err); } } } @@ -245,354 +244,454 @@ function newFloatingLayer() { function physicsLoop() { - if (INTENSIVE_LOG === true) { logger.write("[INFO] physicsLoop -> Entering function."); } + try { - /* This function makes all the calculations to apply phisycs on all floatingObjects in this space. */ + if (INTENSIVE_LOG === true) { logger.write("[INFO] physicsLoop -> Entering function."); } - for (let i = 0; i < visibleFloatingObjects.length; i++) { + /* This function makes all the calculations to apply phisycs on all floatingObjects in this space. */ - let floatingObject = visibleFloatingObjects[i]; + for (let i = 0; i < visibleFloatingObjects.length; i++) { - /* Change position based on speed */ + let floatingObject = visibleFloatingObjects[i]; - floatingObject.currentPosition.x = floatingObject.currentPosition.x + floatingObject.currentSpeed.x; - floatingObject.currentPosition.y = floatingObject.currentPosition.y + floatingObject.currentSpeed.y; + /* Change position based on speed */ - /* Apply some friction to desacelerate */ + floatingObject.currentPosition.x = floatingObject.currentPosition.x + floatingObject.currentSpeed.x; + floatingObject.currentPosition.y = floatingObject.currentPosition.y + floatingObject.currentSpeed.y; - floatingObject.currentSpeed.x = floatingObject.currentSpeed.x * floatingObject.friction; // Desaceleration factor. - floatingObject.currentSpeed.y = floatingObject.currentSpeed.y * floatingObject.friction; // Desaceleration factor. + /* Apply some friction to desacelerate */ - // Gives a minimun speed towards their taget. + floatingObject.currentSpeed.x = floatingObject.currentSpeed.x * floatingObject.friction; // Desaceleration factor. + floatingObject.currentSpeed.y = floatingObject.currentSpeed.y * floatingObject.friction; // Desaceleration factor. - let payload = { - position: undefined, - visible: false - }; + // Gives a minimun speed towards their taget. - switch (floatingObject.type) { + let payload = { + position: undefined, + visible: false + }; - case "Profile Ball": { + switch (floatingObject.type) { - payload.position = floatingObject.payload.profile.position; - payload.visible = floatingObject.payload.profile.visible; - break; + case "Profile Ball": { + + payload.position = floatingObject.payload.profile.position; + payload.visible = floatingObject.payload.profile.visible; + break; + } + case "Note": { + + payload.position = floatingObject.payload.notes[floatingObject.payloadNoteIndex].position; + payload.visible = floatingObject.payload.notes[floatingObject.payloadNoteIndex].visible; + + break; + } + default: { + + break; + } } - case "Note": { - payload.position = floatingObject.payload.notes[floatingObject.payloadNoteIndex].position; - payload.visible = floatingObject.payload.notes[floatingObject.payloadNoteIndex].visible; - - break; - } - default: { - - break; - } - } - - if (floatingObject.currentPosition.x < payload.position.x) { - floatingObject.currentSpeed.x = floatingObject.currentSpeed.x + .005; - } else { - floatingObject.currentSpeed.x = floatingObject.currentSpeed.x - .005; - } - - if (floatingObject.currentPosition.y < payload.position.y) { - floatingObject.currentSpeed.y = floatingObject.currentSpeed.y + .005; - } else { - floatingObject.currentSpeed.y = floatingObject.currentSpeed.y - .005; - } - - // Lets put a maximun speed also. - - const MAX_SPEED = 50; - - if (floatingObject.currentSpeed.x > MAX_SPEED) { - floatingObject.currentSpeed.x = MAX_SPEED; - } - - if (floatingObject.currentSpeed.y > MAX_SPEED) { - floatingObject.currentSpeed.y = MAX_SPEED; - } - - if (floatingObject.currentSpeed.x < -MAX_SPEED) { - floatingObject.currentSpeed.x = -MAX_SPEED; - } - - if (floatingObject.currentSpeed.y < -MAX_SPEED) { - floatingObject.currentSpeed.y = -MAX_SPEED; - } - - // The radius also have a target. - - if (Math.abs(floatingObject.currentRadius - floatingObject.targetRadius) >= 1) { - - if (floatingObject.currentRadius < floatingObject.targetRadius) { - floatingObject.currentRadius = floatingObject.currentRadius + .5; + if (floatingObject.currentPosition.x < payload.position.x) { + floatingObject.currentSpeed.x = floatingObject.currentSpeed.x + .005; } else { - floatingObject.currentRadius = floatingObject.currentRadius - .5; + floatingObject.currentSpeed.x = floatingObject.currentSpeed.x - .005; } - } - // The imageSize also have a target. - - if (Math.abs(floatingObject.currentImageSize - floatingObject.targetImageSize) >= 1) { - - if (floatingObject.currentImageSize < floatingObject.targetImageSize) { - floatingObject.currentImageSize = floatingObject.currentImageSize + 1; + if (floatingObject.currentPosition.y < payload.position.y) { + floatingObject.currentSpeed.y = floatingObject.currentSpeed.y + .005; } else { - floatingObject.currentImageSize = floatingObject.currentImageSize - 1; + floatingObject.currentSpeed.y = floatingObject.currentSpeed.y - .005; } + + // Lets put a maximun speed also. + + const MAX_SPEED = 50; + + if (floatingObject.currentSpeed.x > MAX_SPEED) { + floatingObject.currentSpeed.x = MAX_SPEED; + } + + if (floatingObject.currentSpeed.y > MAX_SPEED) { + floatingObject.currentSpeed.y = MAX_SPEED; + } + + if (floatingObject.currentSpeed.x < -MAX_SPEED) { + floatingObject.currentSpeed.x = -MAX_SPEED; + } + + if (floatingObject.currentSpeed.y < -MAX_SPEED) { + floatingObject.currentSpeed.y = -MAX_SPEED; + } + + // The radius also have a target. + + if (Math.abs(floatingObject.currentRadius - floatingObject.targetRadius) >= 1) { + + if (floatingObject.currentRadius < floatingObject.targetRadius) { + floatingObject.currentRadius = floatingObject.currentRadius + .5; + } else { + floatingObject.currentRadius = floatingObject.currentRadius - .5; + } + } + + // The imageSize also have a target. + + 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 < visibleFloatingObjects.length; k++) { + + if (colliding(visibleFloatingObjects[i], visibleFloatingObjects[k])) { + + resolveCollision(visibleFloatingObjects[k], visibleFloatingObjects[i]); + + } + } + + /* Calculate repulsion force produced by all other floatingObjects */ + + currentRepulsionForce(i); + + targetRepulsionForce(i); + + gravityForce(floatingObject, payload); + } + /* We draw all the visibleFloatingObjects. */ - /* Collision Control */ + for (let i = 0; i < visibleFloatingObjects.length; i++) { + let floatingObject = visibleFloatingObjects[i]; + floatingObject.drawBackground(); + } - for (let k = i + 1; k < visibleFloatingObjects.length; k++) { + for (let i = 0; i < visibleFloatingObjects.length; i++) { + let floatingObject = visibleFloatingObjects[visibleFloatingObjects.length - i - 1]; + floatingObject.drawForeground(); + } - if (colliding(visibleFloatingObjects[i], visibleFloatingObjects[k])) { + /* Now we check if any of the created FloatingObjects where enabled to run under the Physics Engine. */ - resolveCollision(visibleFloatingObjects[k], visibleFloatingObjects[i]); + for (let i = 0; i < invisibleFloatingObjects.length; i++) { + + let floatingObject = invisibleFloatingObjects[i]; + + let payload = { + position: undefined, + visible: false + }; + + switch (floatingObject.type) { + + case "Profile Ball": { + + payload.position = floatingObject.payload.profile.position; + payload.visible = floatingObject.payload.profile.visible; + break; + } + case "Note": { + + payload.position = floatingObject.payload.notes[floatingObject.payloadNoteIndex].position; + payload.visible = floatingObject.payload.notes[floatingObject.payloadNoteIndex].visible; + break; + } + default: { + + break; + } + } + + if (payload.visible === true) { + + if (INFO_LOG === true) { logger.write("[INFO] physicsLoop -> payload.visible = " + payload.visible); } + + /* The first time that the floatingObject becomes visible, we need to do this. */ + + floatingObject.radomizeCurrentPosition(payload.position); + floatingObject.radomizeCurrentSpeed(); + + visibleFloatingObjects.push(floatingObject); + + if (INFO_LOG === true) { logger.write("[INFO] physicsLoop -> floatingObject.handle = " + floatingObject.handle); } + if (INFO_LOG === true) { logger.write("[INFO] physicsLoop -> floatingObject added to visibleFloatingObjects"); } + if (INFO_LOG === true) { logger.write("[INFO] physicsLoop -> visibleFloatingObjects.length = " + visibleFloatingObjects.length); } + + invisibleFloatingObjects.splice(i, 1); // Delete item from array. + + if (INFO_LOG === true) { logger.write("[INFO] physicsLoop -> floatingObject.handle = " + floatingObject.handle); } + if (INFO_LOG === true) { logger.write("[INFO] physicsLoop -> floatingObject removed from invisibleFloatingObjects"); } + if (INFO_LOG === true) { logger.write("[INFO] physicsLoop -> invisibleFloatingObjects.length = " + invisibleFloatingObjects.length); } + + return; // Only one at the time. } } - /* Calculate repulsion force produced by all other floatingObjects */ + /* Finally we check if any of the currently visible floatingObjects has become invisible and must be removed from the Physics Engine. */ - currentRepulsionForce(i); + for (let i = 0; i < visibleFloatingObjects.length; i++) { - targetRepulsionForce(i); + let floatingObject = visibleFloatingObjects[i]; - gravityForce(floatingObject, payload); + let payload = { + position: undefined, + visible: true + }; - } + switch (floatingObject.type) { - /* We draw all the visibleFloatingObjects. */ + case "Profile Ball": { - for (let i = 0; i < visibleFloatingObjects.length; i++) { - let floatingObject = visibleFloatingObjects[i]; - floatingObject.drawBackground(); - } + payload.visible = floatingObject.payload.profile.visible; + break; + } + case "Note": { - for (let i = 0; i < visibleFloatingObjects.length; i++) { - let floatingObject = visibleFloatingObjects[visibleFloatingObjects.length - i - 1]; - floatingObject.drawForeground(); - } + payload.visible = floatingObject.payload.notes[floatingObject.payloadNoteIndex].visible; + break; + } + default: { - /* Now we check if any of the created FloatingObjects where enabled to run under the Physics Engine. */ - - for (let i = 0; i < invisibleFloatingObjects.length; i++) { - - let floatingObject = invisibleFloatingObjects[i]; - - let payload = { - position: undefined, - visible: false - }; - - switch (floatingObject.type) { - - case "Profile Ball": { - - payload.position = floatingObject.payload.profile.position; - payload.visible = floatingObject.payload.profile.visible; - break; + break; + } } - case "Note": { - payload.position = floatingObject.payload.notes[floatingObject.payloadNoteIndex].position; - payload.visible = floatingObject.payload.notes[floatingObject.payloadNoteIndex].visible; - break; - } - default: { + if (payload.visible === false) { + + if (INFO_LOG === true) { logger.write("[INFO] physicsLoop -> payload.visible = " + payload.visible); } + + invisibleFloatingObjects.push(floatingObject); + + if (INFO_LOG === true) { logger.write("[INFO] physicsLoop -> floatingObject.handle = " + floatingObject.handle); } + if (INFO_LOG === true) { logger.write("[INFO] physicsLoop -> floatingObject added to invisibleFloatingObjects"); } + if (INFO_LOG === true) { logger.write("[INFO] physicsLoop -> invisibleFloatingObjects.length = " + invisibleFloatingObjects.length); } + + visibleFloatingObjects.splice(i, 1); // Delete item from array. + + if (INFO_LOG === true) { logger.write("[INFO] physicsLoop -> floatingObject.handle = " + floatingObject.handle); } + if (INFO_LOG === true) { logger.write("[INFO] physicsLoop -> floatingObject removed from visibleFloatingObjects"); } + if (INFO_LOG === true) { logger.write("[INFO] physicsLoop -> visibleFloatingObjects.length = " + visibleFloatingObjects.length); } + + return; // Only one at the time. - break; } } - if (payload.visible === true) { + /* We animate some parts of the dying objects */ - if (INFO_LOG === true) { logger.write("[INFO] physicsLoop -> payload.visible = " + payload.visible); } + for (let i = 0; i < dyingFloatingObjects.length; i++) { - /* The first time that the floatingObject becomes visible, we need to do this. */ + let floatingObject = dyingFloatingObjects[i]; - floatingObject.radomizeCurrentPosition(payload.position); - floatingObject.radomizeCurrentSpeed(); + if (Math.abs(floatingObject.currentRadius - floatingObject.targetRadius) >= 5) { - visibleFloatingObjects.push(floatingObject); + let speed = Math.random(); - if (INFO_LOG === true) { logger.write("[INFO] physicsLoop -> floatingObject.handle = " + floatingObject.handle); } - if (INFO_LOG === true) { logger.write("[INFO] physicsLoop -> floatingObject added to visibleFloatingObjects"); } - if (INFO_LOG === true) { logger.write("[INFO] physicsLoop -> visibleFloatingObjects.length = " + visibleFloatingObjects.length); } + floatingObject.currentRadius = floatingObject.currentRadius - speed * 3; - invisibleFloatingObjects.splice(i, 1); // Delete item from array. + } else { - if (INFO_LOG === true) { logger.write("[INFO] physicsLoop -> floatingObject.handle = " + floatingObject.handle); } - if (INFO_LOG === true) { logger.write("[INFO] physicsLoop -> floatingObject removed from invisibleFloatingObjects"); } - if (INFO_LOG === true) { logger.write("[INFO] physicsLoop -> invisibleFloatingObjects.length = " + invisibleFloatingObjects.length); } + /* Here is when the floatingObjects are definetelly killed. */ - return; // Only one at the time. - - } - } - - /* Finally we check if any of the currently visible floatingObjects has become invisible and must be removed from the Physics Engine. */ - - for (let i = 0; i < visibleFloatingObjects.length; i++) { - - let floatingObject = visibleFloatingObjects[i]; - - let payload = { - position: undefined, - visible: true - }; - - switch (floatingObject.type) { - - case "Profile Ball": { - - payload.visible = floatingObject.payload.profile.visible; - break; - } - case "Note": { - - payload.visible = floatingObject.payload.notes[floatingObject.payloadNoteIndex].visible; - break; - } - default: { - - break; + dyingFloatingObjects.splice(i, 1); // Delete item from array. + break; // only one at the time. } } - if (payload.visible === false) { - - if (INFO_LOG === true) { logger.write("[INFO] physicsLoop -> payload.visible = " + payload.visible); } - - invisibleFloatingObjects.push(floatingObject); - - if (INFO_LOG === true) { logger.write("[INFO] physicsLoop -> floatingObject.handle = " + floatingObject.handle); } - if (INFO_LOG === true) { logger.write("[INFO] physicsLoop -> floatingObject added to invisibleFloatingObjects"); } - if (INFO_LOG === true) { logger.write("[INFO] physicsLoop -> invisibleFloatingObjects.length = " + invisibleFloatingObjects.length); } - - visibleFloatingObjects.splice(i, 1); // Delete item from array. - - if (INFO_LOG === true) { logger.write("[INFO] physicsLoop -> floatingObject.handle = " + floatingObject.handle); } - if (INFO_LOG === true) { logger.write("[INFO] physicsLoop -> floatingObject removed from visibleFloatingObjects"); } - if (INFO_LOG === true) { logger.write("[INFO] physicsLoop -> visibleFloatingObjects.length = " + visibleFloatingObjects.length); } - - return; // Only one at the time. + /* We also draw all the dyingFloatingObjects */ + for (let i = 0; i < dyingFloatingObjects.length; i++) { + let floatingObject = dyingFloatingObjects[i]; + floatingObject.drawBackground(); } - } - /* We animate some parts of the dying objects */ - - for (let i = 0; i < dyingFloatingObjects.length; i++) { - - let floatingObject = dyingFloatingObjects[i]; - - if (Math.abs(floatingObject.currentRadius - floatingObject.targetRadius) >= 5) { - - let speed = Math.random(); - - floatingObject.currentRadius = floatingObject.currentRadius - speed * 3; - - } else { - - /* Here is when the floatingObjects are definetelly killed. */ - - dyingFloatingObjects.splice(i, 1); // Delete item from array. - break; // only one at the time. + for (let i = 0; i < dyingFloatingObjects.length; i++) { + let floatingObject = dyingFloatingObjects[dyingFloatingObjects.length - i - 1]; + floatingObject.drawForeground(); } - } - /* We also draw all the dyingFloatingObjects */ - - for (let i = 0; i < dyingFloatingObjects.length; i++) { - let floatingObject = dyingFloatingObjects[i]; - floatingObject.drawBackground(); - } - - for (let i = 0; i < dyingFloatingObjects.length; i++) { - let floatingObject = dyingFloatingObjects[dyingFloatingObjects.length - i - 1]; - floatingObject.drawForeground(); + } catch (err) { + if (ERROR_LOG === true) { logger.write("[ERROR] physicsLoop -> err = " + err); } } } function gravityForce(floatingObject, payload) { - if (INTENSIVE_LOG === true) { logger.write("[INFO] gravityForce -> Entering function."); } + try { - /* We simulate a kind of gravity towards the target point of each floatingObject. This force will make the floatingObject to keep pushing to reach that point. */ + if (INTENSIVE_LOG === true) { logger.write("[INFO] gravityForce -> Entering function."); } - const coulomb = .00001; - const minForce = 0.01; + /* We simulate a kind of gravity towards the target point of each floatingObject. This force will make the floatingObject to keep pushing to reach that point. */ - 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 ... + const coulomb = .00001; + const minForce = 0.01; - var force = coulomb * d * d / floatingObject.currentMass; // In this case the mass of the floatingObject affects the gravity force that it receives, that gives priority to target position to bigger floatingObjects. + 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 ... - if (force < minForce) { // We need this attraction force to overcome the friction we imposed to the system. - force = minForce; + var force = coulomb * d * d / floatingObject.currentMass; // In this case the mass of the floatingObject affects the gravity force that it receives, that gives priority to target position to bigger floatingObjects. + + if (force < minForce) { // We need this attraction force to overcome the friction we imposed to the system. + force = minForce; + } + + var pos1 = { + x: floatingObject.currentPosition.x, + y: floatingObject.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 add the force vector to the speed vector */ + + floatingObject.currentSpeed.x = floatingObject.currentSpeed.x + forceVector.x; + floatingObject.currentSpeed.y = floatingObject.currentSpeed.y + forceVector.y; + + } catch (err) { + if (ERROR_LOG === true) { logger.write("[ERROR] gravityForce -> err = " + err); } } - - var pos1 = { - x: floatingObject.currentPosition.x, - y: floatingObject.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 add the force vector to the speed vector */ - - floatingObject.currentSpeed.x = floatingObject.currentSpeed.x + forceVector.x; - floatingObject.currentSpeed.y = floatingObject.currentSpeed.y + forceVector.y; - } function currentRepulsionForce(currentFloatingObject) { - if (INTENSIVE_LOG === true) { logger.write("[INFO] currentRepulsionForce -> Entering function."); } + try { - /* We generate a repulsion force between floatingObjects, that prevents them to be collisioning so often. */ + if (INTENSIVE_LOG === true) { logger.write("[INFO] currentRepulsionForce -> Entering function."); } - const coulomb = 2; + /* We generate a repulsion force between floatingObjects, that prevents them to be collisioning so often. */ - var floatingObject1 = visibleFloatingObjects[currentFloatingObject]; + const coulomb = 2; - for (var i = 0; i < visibleFloatingObjects.length; i++) { // The force to be applied is considering all other floatingObjects... + var floatingObject1 = visibleFloatingObjects[currentFloatingObject]; - if (i !== currentFloatingObject) { // ... except for the current one. + for (var i = 0; i < visibleFloatingObjects.length; i++) { // The force to be applied is considering all other floatingObjects... + + if (i !== currentFloatingObject) { // ... except for the current one. + + var floatingObject2 = visibleFloatingObjects[i]; // So, for each floatingObject... + + var d = Math.sqrt(Math.pow(floatingObject2.currentPosition.x - floatingObject1.currentPosition.x, 2) + Math.pow(floatingObject2.currentPosition.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 > 1) { + force = 1; + } + + var pos1 = { + x: floatingObject1.currentPosition.x, + y: floatingObject1.currentPosition.y + }; + + var pos2 = { + x: floatingObject2.currentPosition.x, + y: floatingObject2.currentPosition.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; + + } + + } + + } catch (err) { + if (ERROR_LOG === true) { logger.write("[ERROR] currentRepulsionForce -> err = " + err); } + } + } + + function targetRepulsionForce(currentFloatingObject) { + + try { + + if (INTENSIVE_LOG === true) { logger.write("[INFO] targetRepulsionForce -> Entering function."); } + + /* We generate a repulsion force between floatingObjects, that prevents them to be collisioning so often. */ + + const coulomb = 2; + + var floatingObject1 = visibleFloatingObjects[currentFloatingObject]; + + for (var i = 0; i < visibleFloatingObjects.length; i++) { // The force to be applied is considering all other floatingObjects... var floatingObject2 = visibleFloatingObjects[i]; // So, for each floatingObject... - var d = Math.sqrt(Math.pow(floatingObject2.currentPosition.x - floatingObject1.currentPosition.x, 2) + Math.pow(floatingObject2.currentPosition.y - floatingObject1.currentPosition.y, 2)); // ... we calculate the distance ... + let payload = { + position: undefined + }; + + switch (floatingObject2.type) { + + case "Profile Ball": { + + payload.position = floatingObject2.payload.profile.position; + break; + } + case "Note": { + + payload.position = floatingObject2.payload.notes[floatingObject2.payloadNoteIndex].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 > 1) { - force = 1; + if (force > maxTargetRepulsionForce) { + force = maxTargetRepulsionForce; } var pos1 = { @@ -601,8 +700,8 @@ function newFloatingLayer() { }; var pos2 = { - x: floatingObject2.currentPosition.x, - y: floatingObject2.currentPosition.y + x: payload.position.x, + y: payload.position.y }; var posDiff = { // Next we need the vector resulting from the 2 positions. @@ -627,253 +726,208 @@ function newFloatingLayer() { } + } catch (err) { + if (ERROR_LOG === true) { logger.write("[ERROR] targetRepulsionForce -> err = " + err); } } - } - function targetRepulsionForce(currentFloatingObject) { + function changeTargetRepulsion(pDelta) { - if (INTENSIVE_LOG === true) { logger.write("[INFO] targetRepulsionForce -> Entering function."); } + try { - /* We generate a repulsion force between floatingObjects, that prevents them to be collisioning so often. */ + if (INTENSIVE_LOG === true) { logger.write("[INFO] changeTargetRepulsion -> Entering function."); } - const coulomb = 2; + if (pDelta > 0) { - var floatingObject1 = visibleFloatingObjects[currentFloatingObject]; + pDelta = 1; - for (var i = 0; i < visibleFloatingObjects.length; i++) { // The force to be applied is considering all other floatingObjects... + } else { - var floatingObject2 = visibleFloatingObjects[i]; // So, for each floatingObject... + pDelta = -1; - let payload = { - position: undefined - }; - - switch (floatingObject2.type) { - - case "Profile Ball": { - - payload.position = floatingObject2.payload.profile.position; - break; - } - case "Note": { - - payload.position = floatingObject2.payload.notes[floatingObject2.payloadNoteIndex].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 ... + maxTargetRepulsionForce = maxTargetRepulsionForce + pDelta / 10000; - var force = coulomb * floatingObject2.currentMass / (d * d); // ... and with it the repulsion force. + if (maxTargetRepulsionForce < 0.0001) { - /* 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 > maxTargetRepulsionForce) { - force = maxTargetRepulsionForce; + maxTargetRepulsionForce = 0.0001 } - + + } catch (err) { + if (ERROR_LOG === true) { logger.write("[ERROR] changeTargetRepulsion -> err = " + err); } + } + } + + function colliding(floatingObject1, floatingObject2) { + + try { + + if (INTENSIVE_LOG === true) { logger.write("[INFO] colliding -> Entering function."); } + + /* This function detects weather 2 floatingObjects collide with each other. */ + + var r1 = floatingObject1.currentRadius; + var r2 = floatingObject2.currentRadius; + + var distance = Math.sqrt(Math.pow(floatingObject2.currentPosition.x - floatingObject1.currentPosition.x, 2) + Math.pow(floatingObject2.currentPosition.y - floatingObject1.currentPosition.y, 2)); + + if (distance > (r1 + r2)) { + // No solutions, the circles are too far apart. + return false; + } + else if (distance <= r1 + r2) { + // One circle contains the other. + return true; + } + else if ((distance === 0) && (r1 === r2)) { + // the circles coincide. + return true; + } + else return true; + + } catch (err) { + if (ERROR_LOG === true) { logger.write("[ERROR] colliding -> err = " + err); } + } + } + + function isInside(x, y) { + + try { + + if (INTENSIVE_LOG === true) { logger.write("[INFO] isInside -> Entering function."); } + + /* This function detects weather the point x,y is inside any of the floatingObjects. */ + + for (var i = 0; i < visibleFloatingObjects.length; i++) { + var floatingObject = visibleFloatingObjects[i]; + var distance = Math.sqrt(Math.pow(floatingObject.currentPosition.x - x, 2) + Math.pow(floatingObject.currentPosition.y - y, 2)); + + if (distance < floatingObject.currentRadius) { + return i; + } + } + return -1; + + } catch (err) { + if (ERROR_LOG === true) { logger.write("[ERROR] isInside -> err = " + err); } + } + } + + function isInsideFloatingObject(floatingObjectIndex, x, y) { + + try { + + if (INTENSIVE_LOG === true) { logger.write("[INFO] isInsideFloatingObject -> Entering function."); } + + /* This function detects weather the point x,y is inside one particular floatingObjects. */ + + var floatingObject = visibleFloatingObjects[floatingObjectIndex]; + var distance = Math.sqrt(Math.pow(floatingObject.currentPosition.x - x, 2) + Math.pow(floatingObject.currentPosition.y - y, 2)); + + if (distance < floatingObject.currentRadius) { + return true; + } + + return false; + + } catch (err) { + if (ERROR_LOG === true) { logger.write("[ERROR] isInsideFloatingObject -> err = " + err); } + } + } + + function distance(x1, y1, x2, y2) { + + try { + + if (INTENSIVE_LOG === true) { logger.write("[INFO] distance -> Entering function."); } + + return Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)); + + } catch (err) { + if (ERROR_LOG === true) { logger.write("[ERROR] distance -> err = " + err); } + } + } + + function resolveCollision(floatingObject1, floatingObject2) { + + try { + + if (INTENSIVE_LOG === true) { logger.write("[INFO] resolveCollision -> Entering function."); } + + /* This function changes speed and position of floatingObjects that are in collision */ + + var collisionision_angle = Math.atan2((floatingObject2.currentPosition.y - floatingObject1.currentPosition.y), (floatingObject2.currentPosition.x - floatingObject1.currentPosition.x)); + + var speed1 = Math.sqrt(floatingObject1.currentSpeed.x * floatingObject1.currentSpeed.x + floatingObject1.currentSpeed.y * floatingObject1.currentSpeed.y); // Magnitude of Speed Vector for floatingObject 1 + var speed2 = Math.sqrt(floatingObject2.currentSpeed.x * floatingObject2.currentSpeed.x + floatingObject2.currentSpeed.y * floatingObject2.currentSpeed.y); // Magnitude of Speed Vector for floatingObject 2 + + var direction_1 = Math.atan2(floatingObject1.currentSpeed.y, floatingObject1.currentSpeed.x); + var direction_2 = Math.atan2(floatingObject2.currentSpeed.y, floatingObject2.currentSpeed.x); + + var new_xspeed_1 = speed1 * Math.cos(direction_1 - collisionision_angle); + var new_yspeed_1 = speed1 * Math.sin(direction_1 - collisionision_angle); + var new_xspeed_2 = speed2 * Math.cos(direction_2 - collisionision_angle); + var new_yspeed_2 = speed2 * Math.sin(direction_2 - collisionision_angle); + + var final_xspeed_1 = ((floatingObject1.currentMass - floatingObject2.currentMass) * new_xspeed_1 + (floatingObject2.currentMass + floatingObject2.currentMass) * new_xspeed_2) / (floatingObject1.currentMass + floatingObject2.currentMass); + var final_xspeed_2 = ((floatingObject1.currentMass + floatingObject1.currentMass) * new_xspeed_1 + (floatingObject2.currentMass - floatingObject1.currentMass) * new_xspeed_2) / (floatingObject1.currentMass + floatingObject2.currentMass); + var final_yspeed_1 = new_yspeed_1; + var final_yspeed_2 = new_yspeed_2; + + var cosAngle = Math.cos(collisionision_angle); + var sinAngle = Math.sin(collisionision_angle); + + floatingObject1.currentSpeed.x = cosAngle * final_xspeed_1 - sinAngle * final_yspeed_1; + floatingObject1.currentSpeed.y = sinAngle * final_xspeed_1 + cosAngle * final_yspeed_1; + + floatingObject2.currentSpeed.x = cosAngle * final_xspeed_2 - sinAngle * final_yspeed_2; + floatingObject2.currentSpeed.y = sinAngle * final_xspeed_2 + cosAngle * final_yspeed_2; + var pos1 = { x: floatingObject1.currentPosition.x, y: floatingObject1.currentPosition.y }; var pos2 = { - x: payload.position.x, - y: payload.position.y + x: floatingObject2.currentPosition.x, + y: floatingObject2.currentPosition.y }; - var posDiff = { // Next we need the vector resulting from the 2 positions. - x: pos2.x - pos1.x, - y: pos2.y - pos1.y + // get the mtd + var posDiff = { + x: pos1.x - pos2.x, + y: pos1.y - pos2.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 d = Math.sqrt(Math.pow(floatingObject2.currentPosition.x - floatingObject1.currentPosition.x, 2) + Math.pow(floatingObject2.currentPosition.y - floatingObject1.currentPosition.y, 2)); + + // minimum translation distance to push floatingObjects apart after intersecting + var scalar = (((floatingObject1.currentRadius + floatingObject2.currentRadius) - d) / d); + + var minTD = { + x: posDiff.x * scalar, + y: posDiff.y * scalar }; - var forceVector = { - x: unitVector.x * force, - y: unitVector.y * force - }; + // resolve intersection -- + // computing inverse mass quantities + var im1 = 1 / floatingObject1.currentMass; + var im2 = 1 / floatingObject2.currentMass; - /* We substract the force vector to the speed vector of the current floatingObject */ + // push-pull them apart based off their mass - floatingObject1.currentSpeed.x = floatingObject1.currentSpeed.x - forceVector.x; - floatingObject1.currentSpeed.y = floatingObject1.currentSpeed.y - forceVector.y; + pos1.x = pos1.x + minTD.x * (im1 / (im1 + im2)); + pos1.y = pos1.y + minTD.y * (im1 / (im1 + im2)); + pos2.x = pos2.x - minTD.x * (im2 / (im1 + im2)); + pos2.y = pos2.y - minTD.y * (im2 / (im1 + im2)); + floatingObject1.currentPosition = pos1; + floatingObject2.currentPosition = pos2; + + } catch (err) { + if (ERROR_LOG === true) { logger.write("[ERROR] resolveCollision -> err = " + err); } } - - } - - function changeTargetRepulsion(pDelta) { - - if (INTENSIVE_LOG === true) { logger.write("[INFO] changeTargetRepulsion -> Entering function."); } - - if (pDelta > 0) { - - pDelta = 1; - - } else { - - pDelta = -1; - - } - - maxTargetRepulsionForce = maxTargetRepulsionForce + pDelta / 10000; - - if (maxTargetRepulsionForce < 0.0001) { - - maxTargetRepulsionForce = 0.0001 - } - } - - function colliding(floatingObject1, floatingObject2) { - - if (INTENSIVE_LOG === true) { logger.write("[INFO] colliding -> Entering function."); } - - /* This function detects weather 2 floatingObjects collide with each other. */ - - var r1 = floatingObject1.currentRadius; - var r2 = floatingObject2.currentRadius; - - var distance = Math.sqrt(Math.pow(floatingObject2.currentPosition.x - floatingObject1.currentPosition.x, 2) + Math.pow(floatingObject2.currentPosition.y - floatingObject1.currentPosition.y, 2)); - - if (distance > (r1 + r2)) { - // No solutions, the circles are too far apart. - return false; - } - else if (distance <= r1 + r2) { - // One circle contains the other. - return true; - } - else if ((distance === 0) && (r1 === r2)) { - // the circles coincide. - return true; - } - else return true; - } - - function isInside(x, y) { - - if (INTENSIVE_LOG === true) { logger.write("[INFO] isInside -> Entering function."); } - - /* This function detects weather the point x,y is inside any of the floatingObjects. */ - - for (var i = 0; i < visibleFloatingObjects.length; i++) { - var floatingObject = visibleFloatingObjects[i]; - var distance = Math.sqrt(Math.pow(floatingObject.currentPosition.x - x, 2) + Math.pow(floatingObject.currentPosition.y - y, 2)); - - if (distance < floatingObject.currentRadius) { - return i; - } - } - return -1; - - } - - function isInsideFloatingObject(floatingObjectIndex, x, y) { - - if (INTENSIVE_LOG === true) { logger.write("[INFO] isInsideFloatingObject -> Entering function."); } - - /* This function detects weather the point x,y is inside one particular floatingObjects. */ - - var floatingObject = visibleFloatingObjects[floatingObjectIndex]; - var distance = Math.sqrt(Math.pow(floatingObject.currentPosition.x - x, 2) + Math.pow(floatingObject.currentPosition.y - y, 2)); - - if (distance < floatingObject.currentRadius) { - return true; - } - - return false; - - } - - function distance(x1, y1, x2, y2) { - - if (INTENSIVE_LOG === true) { logger.write("[INFO] distance -> Entering function."); } - - return Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)); - } - - function resolveCollision(floatingObject1, floatingObject2) { - - if (INTENSIVE_LOG === true) { logger.write("[INFO] resolveCollision -> Entering function."); } - - /* This function changes speed and position of floatingObjects that are in collision */ - - var collisionision_angle = Math.atan2((floatingObject2.currentPosition.y - floatingObject1.currentPosition.y), (floatingObject2.currentPosition.x - floatingObject1.currentPosition.x)); - - var speed1 = Math.sqrt(floatingObject1.currentSpeed.x * floatingObject1.currentSpeed.x + floatingObject1.currentSpeed.y * floatingObject1.currentSpeed.y); // Magnitude of Speed Vector for floatingObject 1 - var speed2 = Math.sqrt(floatingObject2.currentSpeed.x * floatingObject2.currentSpeed.x + floatingObject2.currentSpeed.y * floatingObject2.currentSpeed.y); // Magnitude of Speed Vector for floatingObject 2 - - var direction_1 = Math.atan2(floatingObject1.currentSpeed.y, floatingObject1.currentSpeed.x); - var direction_2 = Math.atan2(floatingObject2.currentSpeed.y, floatingObject2.currentSpeed.x); - - var new_xspeed_1 = speed1 * Math.cos(direction_1 - collisionision_angle); - var new_yspeed_1 = speed1 * Math.sin(direction_1 - collisionision_angle); - var new_xspeed_2 = speed2 * Math.cos(direction_2 - collisionision_angle); - var new_yspeed_2 = speed2 * Math.sin(direction_2 - collisionision_angle); - - var final_xspeed_1 = ((floatingObject1.currentMass - floatingObject2.currentMass) * new_xspeed_1 + (floatingObject2.currentMass + floatingObject2.currentMass) * new_xspeed_2) / (floatingObject1.currentMass + floatingObject2.currentMass); - var final_xspeed_2 = ((floatingObject1.currentMass + floatingObject1.currentMass) * new_xspeed_1 + (floatingObject2.currentMass - floatingObject1.currentMass) * new_xspeed_2) / (floatingObject1.currentMass + floatingObject2.currentMass); - var final_yspeed_1 = new_yspeed_1; - var final_yspeed_2 = new_yspeed_2; - - var cosAngle = Math.cos(collisionision_angle); - var sinAngle = Math.sin(collisionision_angle); - - floatingObject1.currentSpeed.x = cosAngle * final_xspeed_1 - sinAngle * final_yspeed_1; - floatingObject1.currentSpeed.y = sinAngle * final_xspeed_1 + cosAngle * final_yspeed_1; - - floatingObject2.currentSpeed.x = cosAngle * final_xspeed_2 - sinAngle * final_yspeed_2; - floatingObject2.currentSpeed.y = sinAngle * final_xspeed_2 + cosAngle * final_yspeed_2; - - var pos1 = { - x: floatingObject1.currentPosition.x, - y: floatingObject1.currentPosition.y - }; - - var pos2 = { - x: floatingObject2.currentPosition.x, - y: floatingObject2.currentPosition.y - }; - - // get the mtd - var posDiff = { - x: pos1.x - pos2.x, - y: pos1.y - pos2.y - }; - - var d = Math.sqrt(Math.pow(floatingObject2.currentPosition.x - floatingObject1.currentPosition.x, 2) + Math.pow(floatingObject2.currentPosition.y - floatingObject1.currentPosition.y, 2)); - - // minimum translation distance to push floatingObjects apart after intersecting - var scalar = (((floatingObject1.currentRadius + floatingObject2.currentRadius) - d) / d); - - var minTD = { - x: posDiff.x * scalar, - y: posDiff.y * scalar - }; - - // resolve intersection -- - // computing inverse mass quantities - var im1 = 1 / floatingObject1.currentMass; - var im2 = 1 / floatingObject2.currentMass; - - // push-pull them apart based off their mass - - pos1.x = pos1.x + minTD.x * (im1 / (im1 + im2)); - pos1.y = pos1.y + minTD.y * (im1 / (im1 + im2)); - pos2.x = pos2.x - minTD.x * (im2 / (im1 + im2)); - pos2.y = pos2.y - minTD.y * (im2 / (im1 + im2)); - - floatingObject1.currentPosition = pos1; - floatingObject2.currentPosition = pos2; } }