9

我正在尝试在我的svg强制布局(d3js)中创建碰撞检测。我已关注这个教程进行了圆形碰撞。

由于某种原因,它不适用于矩形。我试图在面纱中使用参数。

这是我的代码:

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);  

和这个:

 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 + ")"; 
    });
});

和碰撞函数:

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;
     };
}

如何检测矩形的碰撞?

谢谢!!!

4

1 回答 1

12

The collision function in the d3 example calculates the overlap of circles by comparing the distance between their centers l = Math.sqrt(x * x + y * y) with the sum of their radii r = node.radius + quad.point.radius. When l < r the circles overlap and the two circles are moved away from each other to correct the overlap.

A similar collision function for rectangles works in the same way, by computing the overlap of the rectangles and moving each away from the other:

function collide(node) {
  var nx1, nx2, ny1, ny2, padding;
  padding = 32;
  nx1 = node.x - padding;
  nx2 = node.x2 + padding;
  ny1 = node.y - padding;
  ny2 = node.y2 + padding;
  return function(quad, x1, y1, x2, y2) {
    var dx, dy;
    if (quad.point && (quad.point !== node)) {
      if (overlap(node, quad.point)) {
        dx = Math.min(node.x2 - quad.point.x, quad.point.x2 - node.x) / 2;
        node.x -= dx;
        quad.point.x += dx;
        dy = Math.min(node.y2 - quad.point.y, quad.point.y2 - node.y) / 2;
        node.y -= dy;
        quad.point.y += dy;
      }
    }
    return x1 > nx2 || x2 < nx1 || y1 > ny2 || y2 < ny1;
  };
};

The overlap in width is dx = Math.min(node.x2 - quad.point.x, quad.point.x2 - node.x) / 2; where overlap in height is dy = Math.min(node.y2 - quad.point.y, quad.point.y2 - node.y) / 2; which shows that your nodes must know two corners of the rect: top left (x, y) and bottom right (x2, y2).

See a complete example here: http://bl.ocks.org/dobbs/1d353282475013f5c156. The example uses coffeescript, and only moves the rects away from each other along the y-axis 'cos that better matches what I need for my own case.

于 2014-08-17T18:51:14.060 回答