From d488fd3438d993815df4ffc27951a3ac8844e4ac Mon Sep 17 00:00:00 2001
From: Jake <jake.read@cba.mit.edu>
Date: Tue, 11 Jun 2019 16:48:36 -0400
Subject: [PATCH] rectangular collision is satisfying, now links with direction
---
bootstrap.js | 4 ++--
view/frect.js | 52 +++++++++++++++-----------------------------------
view/vfloop.js | 10 +++++-----
3 files changed, 22 insertions(+), 44 deletions(-)
diff --git a/bootstrap.js b/bootstrap.js
index b7749b1..f5b4dfa 100644
--- a/bootstrap.js
+++ b/bootstrap.js
@@ -58,7 +58,7 @@ window.onload = () => {
// if you make links work properly (directionfull)
// you can just pin view outputs to the left, collected,
// and the rest should sort itself out
- view.defs[0].floaters[0].fixTo(700,200)
+ //view.defs[0].floaters[0].fixTo(700,200)
view.defs[1].unwrap()
view.defs[1].floaters[0].fixTo(400, 100)
//view.defs[1].floaters[1].fixTo(800, 300)
@@ -74,7 +74,7 @@ window.onload = () => {
})
view.zoomExtents()
})
- */
+ */
})
}, 100)
}).catch((err) => {
diff --git a/view/frect.js b/view/frect.js
index ca84fd7..da715ff 100644
--- a/view/frect.js
+++ b/view/frect.js
@@ -34,10 +34,14 @@ export default function(radius) {
tree = quadtree(nodes, x, y)
for (i = 0; i < n; ++i) {
node = nodes[i]
- // here
- ri = radii[node.index], ri2 = ri * ri
- xi = node.x + node.vx
- yi = node.y + node.vy
+ // node left, right (a note: .bb.x- should be 0-based)
+ var nxl = node.x + node.bb.x1
+ var nxr = node.x + node.bb.x2
+ var nyt = node.y + node.bb.y1
+ var nyb = node.y + node.bb.y2
+ // node center-by-bounding-box
+ var cnx = (nxr - nxl) / 2 + nxl
+ var cny = (nyb - nyt) / 2 + nyt
// for each node, visit each...
//console.log(`visit ${node.index}`)
tree.visit(apply)
@@ -52,27 +56,17 @@ export default function(radius) {
r = ri + rj
// prevents from op'ing twice
if (data.index > node.index) {
- let lg = false
- if(data.index === 2 && node.index === 0 && false){
- lg = true
- }
// quad.data is the node,
- // atm, this is websocket and link (I think)
// by domain intersections...
// | -------- x ------ |
// | ------------ x ------ |
// | ---- x --- |
- // node left, right (a note: .bb.x- should be 0-based
- let nxl = node.x + node.bb.x1
- let nxr = node.x + node.bb.x2
// data left, right
let dxl = data.x + data.bb.x1
let dxr = data.x + data.bb.x2
// dxr or dxl within nx,
if(nxl < dxr && dxr < nxr || nxl < dxl && dxl < nxr){
- // bc logging, here's a tonne
- let nyt = node.y + node.bb.y1
- let nyb = node.y + node.bb.y2
+ // now write these
let dyt = data.y + data.bb.y1
let dyb = data.y + data.bb.y2
// and check,
@@ -80,8 +74,6 @@ export default function(radius) {
updated = true
// ok then, compute centers to find a direction for force-exertion
// maybe should also do this in prepare also (and the node data, only once)
- let cnx = (nxr - nxl) / 2 + nxl
- let cny = (nyb - nyt) / 2 + nyt
let cdx = (dxr - dxl) / 2 + dxl
let cdy = (dyb - dyt) / 2 + dyt
// x overlap term
@@ -91,7 +83,6 @@ export default function(radius) {
} else {
xo = nxl - dxr
}
- if(lg) console.log('xo', xo)
// y overlap term
let yo = 0
if(cny < cdy){
@@ -99,30 +90,25 @@ export default function(radius) {
} else {
yo = nyt - dyb
}
- if(lg) console.log('yo', yo)
- let damp = 0.4
- // just assert one axis, otherwise everything ends up
+ // just assert one axis
if(Math.abs(xo) > Math.abs(yo)){
- node.vy -= yo * damp
- data.vy += yo * damp
+ node.vy -= yo * strength
+ data.vy += yo * strength
} else {
- node.vx -= xo * damp
- data.vx += xo * damp
+ node.vx -= xo * strength
+ data.vx += xo * strength
}
}
}
}
return
} // end if-data
- return updated //x0 > xi + r || x1 < xi - r || y0 > yi + r || y1 < yi - r
+ return updated
} // end apply()
}
function initialize() {
if (!nodes) return
- var i, n = nodes.length, node
- radii = new Array(n)
- for (i = 0; i < n; ++i) node = nodes[i], radii[node.index] = +radius(node, i, nodes)
}
force.initialize = function(_) {
@@ -130,17 +116,9 @@ export default function(radius) {
initialize()
}
- force.iterations = function(_) {
- return arguments.length ? (iterations = +_, force) : iterations
- }
-
force.strength = function(_) {
return arguments.length ? (strength = +_, force) : strength
}
- force.radius = function(_) {
- return arguments.length ? (radius = typeof _ === "function" ? _ : constant(+_), initialize(), force) : radius
- }
-
return force
}
diff --git a/view/vfloop.js b/view/vfloop.js
index 81df192..5af09c7 100644
--- a/view/vfloop.js
+++ b/view/vfloop.js
@@ -113,13 +113,13 @@ function Floop(View) {
// we restart the simulation by writing over it w/ a new one
this.sim = d3.forceSimulation(nodes)
+ // don't touch,
+ .force('rectcollide', frect().strength(0.7))
+ // spring 2gether
.force('link', d3.forceLink(links).distance((link) => {
// nodes are floaters, links have indice, could do like
- return 400
- }))
- .force('rectcollide', frect((node, i, nodes) => {
- return node.r
- }))
+ return 150
+ }).strength(1))
.on('tick', onTick)
.on('end', onEnd)
// to go manuel, call sim.stop()
--
GitLab