Attaching Implemented
This commit is contained in:
1 parent
85d0974e5c
commit
96413ad733
3 files changed
+129
-43
No files matched your search
@@ -394,6 +394,8 @@ function newFloatingLayer () {
|
||||
targetRepulsionForce(i)
|
||||
|
||||
gravityForce(floatingObject, payload)
|
||||
|
||||
proximityBetweenFloatingObjects(i)
|
||||
}
|
||||
} catch (err) {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] physics -> applyPhysics -> err= ' + err.stack) }
|
||||
@@ -542,58 +544,34 @@ function newFloatingLayer () {
|
||||
function proximityBetweenFloatingObjects (currentFloatingObject) {
|
||||
try {
|
||||
let floatingObject1 = visibleFloatingObjects[currentFloatingObject]
|
||||
|
||||
floatingObject1.nearbyFloatingObjects = []
|
||||
for (let i = 0; i < visibleFloatingObjects.length; i++) {
|
||||
// The force to be applied is considering all other floatingObjects...
|
||||
|
||||
if (i !== currentFloatingObject) {
|
||||
// ... except for the current one.
|
||||
|
||||
let floatingObject2 = visibleFloatingObjects[i] // So, for each floatingObject...
|
||||
|
||||
let d = Math.sqrt(Math.pow(floatingObject2.container.frame.position.x - floatingObject1.container.frame.position.x, 2) + Math.pow(floatingObject2.container.frame.position.y - floatingObject1.container.frame.position.y, 2)) // ... we calculate the distance ...
|
||||
|
||||
let 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 (floatingObject1.nearbyFloatingObjects.length === 0) {
|
||||
floatingObject1.nearbyFloatingObjects.push([d, floatingObject2])
|
||||
} else {
|
||||
let recordAdded = false
|
||||
for (let j = 0; j < floatingObject1.nearbyFloatingObjects.length; j++) {
|
||||
let recordedDistance = floatingObject1.nearbyFloatingObjects[0]
|
||||
if (d < recordedDistance) {
|
||||
floatingObject1.nearbyFloatingObjects.splice(j, 0, [d, floatingObject2])
|
||||
recordAdded = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if (recordAdded === false) {
|
||||
floatingObject1.nearbyFloatingObjects.push([d, floatingObject2])
|
||||
}
|
||||
}
|
||||
|
||||
let pos1 = {
|
||||
x: floatingObject1.container.frame.position.x,
|
||||
y: floatingObject1.container.frame.position.y
|
||||
}
|
||||
|
||||
let pos2 = {
|
||||
x: floatingObject2.container.frame.position.x,
|
||||
y: floatingObject2.container.frame.position.y
|
||||
}
|
||||
|
||||
let posDiff = { // Next we need the vector resulting from the 2 positions.
|
||||
x: pos2.x - pos1.x,
|
||||
y: pos2.y - pos1.y
|
||||
}
|
||||
|
||||
let unitVector = { // To find the unit vector, we divide each component by the magnitude of the vector.
|
||||
x: posDiff.x / d,
|
||||
y: posDiff.y / d
|
||||
}
|
||||
|
||||
let 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
|
||||
floatingObject1.nearbyFloatingObjects.splice(10, floatingObject1.nearbyFloatingObjects.length)
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] repulsionForceBetweenFloatingObjects -> err= ' + err.stack) }
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] proximityBetweenFloatingObjects -> err= ' + err.stack) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -806,4 +784,3 @@ function newFloatingLayer () {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ function newFloatingObject () {
|
||||
rawRadius: 0, // This is the radius of this floating object without zoom.
|
||||
targetRadius: 0, // This is the target radius of the floating object with zoom applied. It should be animated until reaching this value.
|
||||
isPinned: false,
|
||||
nearbyFloatingObjects: [],
|
||||
pinToggle: pinToggle,
|
||||
physics: physics,
|
||||
initializeMass: initializeMass,
|
||||
@@ -65,6 +66,7 @@ function newFloatingObject () {
|
||||
thisObject.container = undefined
|
||||
thisObject.payload = undefined
|
||||
thisObject.fitFunction = undefined
|
||||
thisObject.nearbyFloatingObjects = undefined
|
||||
}
|
||||
|
||||
function initialize (type, payload) {
|
||||
|
||||
@@ -17,6 +17,8 @@ function newStrategyPart () {
|
||||
codeEditor: undefined,
|
||||
partTitle: undefined,
|
||||
isExecuting: undefined,
|
||||
getReadyToAttach: getReadyToAttach,
|
||||
showAvailabilityToAttach: showAvailabilityToAttach,
|
||||
highlight: highlight,
|
||||
unHighlight: unHighlight,
|
||||
physics: physics,
|
||||
@@ -48,6 +50,12 @@ function newStrategyPart () {
|
||||
let highlightCounter = 0
|
||||
|
||||
let previousDistance
|
||||
|
||||
let readyToAttachCounter
|
||||
let isReadyToAttach
|
||||
let availableToAttachCounter
|
||||
let isAvailableToAttach
|
||||
|
||||
return thisObject
|
||||
|
||||
function finalize () {
|
||||
@@ -138,6 +146,75 @@ function newStrategyPart () {
|
||||
iconPhysics()
|
||||
highlightPhisycs()
|
||||
detachingPhysics()
|
||||
attachingPhysics()
|
||||
}
|
||||
|
||||
function attachingPhysics () {
|
||||
attacchingCounters()
|
||||
|
||||
if (thisObject.isOnFocus !== true) { return }
|
||||
|
||||
let nearbyFloatingObjects = thisObject.payload.floatingObject.nearbyFloatingObjects
|
||||
let compatibleType
|
||||
let compatibleSubType
|
||||
switch (thisObject.payload.node.type) {
|
||||
case 'Strategy':
|
||||
compatibleType = 'Trading System'
|
||||
compatibleSubType = undefined
|
||||
break
|
||||
case 'Phase':
|
||||
compatibleType = 'Stop'
|
||||
compatibleSubType = undefined
|
||||
break
|
||||
case 'Situation':
|
||||
compatibleType = 'Phase'
|
||||
compatibleSubType = undefined
|
||||
break
|
||||
case 'Condition':
|
||||
compatibleType = 'Situation'
|
||||
compatibleSubType = undefined
|
||||
break
|
||||
}
|
||||
for (let i = 0; i < nearbyFloatingObjects.length; i++) {
|
||||
let nearby = nearbyFloatingObjects[i]
|
||||
let distance = nearby[0]
|
||||
let floatingObject = nearby[1]
|
||||
let nearbyNode = floatingObject.payload.node
|
||||
if (nearbyNode.type === compatibleType) {
|
||||
if (i === 0) {
|
||||
if (distance < thisObject.container.frame.radius + floatingObject.container.frame.radius) {
|
||||
nearbyNode.payload.uiObject.getReadyToAttach()
|
||||
}
|
||||
}
|
||||
nearbyNode.payload.uiObject.showAvailabilityToAttach()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function attacchingCounters () {
|
||||
readyToAttachCounter--
|
||||
if (readyToAttachCounter < 0) {
|
||||
readyToAttachCounter = 0
|
||||
isReadyToAttach = false
|
||||
} else {
|
||||
isReadyToAttach = true
|
||||
}
|
||||
|
||||
availableToAttachCounter--
|
||||
if (availableToAttachCounter < 0) {
|
||||
availableToAttachCounter = 0
|
||||
isAvailableToAttach = false
|
||||
} else {
|
||||
isAvailableToAttach = true
|
||||
}
|
||||
}
|
||||
|
||||
function getReadyToAttach () {
|
||||
readyToAttachCounter = 10
|
||||
}
|
||||
|
||||
function showAvailabilityToAttach () {
|
||||
availableToAttachCounter = 10
|
||||
}
|
||||
|
||||
function detachingPhysics () {
|
||||
@@ -428,6 +505,36 @@ function newStrategyPart () {
|
||||
browserCanvasContext.setLineDash([4, 20])
|
||||
browserCanvasContext.stroke()
|
||||
}
|
||||
|
||||
if (isReadyToAttach === true) {
|
||||
VISIBLE_RADIUS = thisObject.container.frame.radius
|
||||
let OPACITY = readyToAttachCounter / 10
|
||||
|
||||
browserCanvasContext.beginPath()
|
||||
browserCanvasContext.arc(visiblePosition.x, visiblePosition.y, VISIBLE_RADIUS, 0, Math.PI * 2, true)
|
||||
browserCanvasContext.closePath()
|
||||
|
||||
browserCanvasContext.strokeStyle = 'rgba(' + UI_COLOR.RUSTED_RED + ', ' + OPACITY + ')'
|
||||
|
||||
browserCanvasContext.lineWidth = 10
|
||||
browserCanvasContext.setLineDash([16, 20])
|
||||
browserCanvasContext.stroke()
|
||||
}
|
||||
|
||||
if (isAvailableToAttach === true) {
|
||||
VISIBLE_RADIUS = thisObject.container.frame.radius
|
||||
let OPACITY = availableToAttachCounter / 10
|
||||
|
||||
browserCanvasContext.beginPath()
|
||||
browserCanvasContext.arc(visiblePosition.x, visiblePosition.y, VISIBLE_RADIUS, 0, Math.PI * 2, true)
|
||||
browserCanvasContext.closePath()
|
||||
|
||||
browserCanvasContext.strokeStyle = 'rgba(' + UI_COLOR.TURQUOISE + ', ' + OPACITY + ')'
|
||||
|
||||
browserCanvasContext.lineWidth = 10
|
||||
browserCanvasContext.setLineDash([8, 20])
|
||||
browserCanvasContext.stroke()
|
||||
}
|
||||
}
|
||||
|
||||
/* Image */
|
||||
|
||||
Reference in new issue
Block a user