Ich versuche eine Kollisionserkennung in meinem erzwungenen Layout svg
(d3js) zu erstellen. Ich habe this Tutorial gefolgt, die eine Kreisform Kollision macht.rect Kollisionserkennung d3js
Aus irgendeinem Grund funktioniert es nicht für die rect Form. Ich habe versucht, mit den Parametern in einem Schleier zu spielen.
Hier ist mein Code:
var node = svg.selectAll(".node")
.data(json.nodes)
.enter().append("g")
.attr("class", "node")
.call(force.drag);
node
.append("rect")
.attr("class", "tagHolder")
.attr("width", 60)
.attr("height", 60)
.attr("rx", 5)
.attr("ry", 5)
.attr("x", -10)
.attr("y", -10);
und diese:
svg.selectAll(".node")
.attr("x", function(d) { return d.x; })
.attr("y", function(d) { return d.y; });
link.attr("x1", function(d)
{
return d.source.x;
})
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });
node.attr("transform", function(d)
{
return "translate(" + d.x + "," + d.y + ")";
});
});
und die Kollisionsfunktion:
function collide(node) {
var r = 30,
nx1 = node.x - r,
nx2 = node.x + r,
ny1 = node.y - r,
ny2 = node.y + r;
return function(quad, x1, y1, x2, y2)
{
if (quad.point && (quad.point !== node))
{
var x = node.x - quad.point.x,
y = node.y - quad.point.y,
l = Math.sqrt(x * x + y * y),
r = 30 + quad.point.radius;
if (l < r)
{
l = (l - r)/l * .5;
node.x -= x *= l;
node.y -= y *= l;
quad.point.x += x;
quad.point.y += y;
}
}
return x1 > nx2 || x2 < nx1 || y1 > ny2 || y2 < ny1;
};
}
Wie kann ich die Kollision für rect erkennen?
Danke !!!
Ich habe die gleiche Frage, obwohl vielleicht ein wenig weiter als Ihr Beispiel. Etwas stimmt nicht mit den Änderungen, die ich an der Kollisionsfunktion gemacht habe, weil ich zu viel Abstoßung zwischen Rechtecken bekomme: http://bl.ocks.org/dobbs/1d353282475013f5c156 –