javascript - Vanilla JS Div 碰撞检测

标签 javascript css collision detection

我的以下实现可以在 jsfiddle.net 上找到

我有四个 div。我的目标是让它们可以在页面上拖动,但不允许它们相互重叠。每个都可以使用鼠标移动监听器在页面上拖动。

container.addEventListener('mousemove',mouseMove);
  function mouseMove(e) {
    if (!mouseDown) {return;}
    let coords=e.target.getBoundingClientRect();
    let movX=e.movementX;
    let movY=e.movementY;
    if (!collision(movX,movY,e.target.classList[1],coords)){
      e.target.style.left=`${coords.left+movX}px`;
      e.target.style.top=`${coords.top+movY}px`;
    }
  }

严格来说,我的碰撞检测功能有效。我将“Collision”事件输出到一个 div,这样当你拖动它时你可以在 fiddle 中看到它。但是,您仍然可以将 div 相互拖动。

当您尝试将它们拉开时,它们有点“粘”在一起,如果您继续插入它们,它们就会重叠。此时碰撞检测在真/假之间摇摆不定,所以我猜这里可能发生了一些奇怪的事情,但我无法弄清楚。

我认为一个问题可能是碰撞检测仅在边界相等时才输出碰撞。也就是说,一旦发生碰撞并且一个元素在另一个元素内部,它就会返回 false。

但是,我看不到我的 mousemove e.movementX 和 e.movementY 事件如何能够通过碰撞测试并移动 div。

最佳答案

您将让对象与不止 1 个碰撞。脚本将为您提供所有碰撞。但是我猜接受/移动它或不接受它的逻辑取决于你想要实现的目标。借自intersects

脚本:

function mouseMove(e) {
  if (!mouseDown) {
    return;
  }
  let coords = e.target.getBoundingClientRect();
  let movX = e.movementX;
  let movY = e.movementY;

  collision(movX, movY, e.target.classList[1], coords) //check all collisions. Item can collide with more than one polygon.

  e.target.style.left = `${coords.left+movX}px`;
  e.target.style.top = `${coords.top+movY}px`;
  /* if (!) {

  }*/
}

function collision(newX, newY, movingPart, movingRect) {
  let takenPositions = []; //array of arrays of rects' L, R, Top, Bottom coords
  let newCoords = {
    id: movingPart,
    width: 100,
    height: 100,
    x: movingRect.left + newX,
    y: movingRect.top + newY
  };

  let collision = false;
  let collisions = []; //store collisions. 
  divs.forEach((d) => {
    if (d.classList[1] !== movingPart) { // a thing can't collide with itself
      let c = d.getBoundingClientRect();
      takenPositions.push({
        id: d.classList[1],
        width: 100,
        height: 100,
         x: c.left,//updated this part x,y are undefined :|
         y: c.top //and updated this
      });
    }
  });

  takenPositions.forEach((p) => {
    var tw = p.width;
    var th = p.height;
    var rw = newCoords.width;
    var rh = newCoords.height;
    if (rw <= 0 || rh <= 0 || tw <= 0 || th <= 0) {
      collision = false;
    } else {
      var tx = p.x;
      var ty = p.y;
      var rx = newCoords.x;
      var ry = newCoords.y;
      rw += rx;
      rh += ry;
      tw += tx;
      th += ty;
      collision = ((rw < rx || rw > tx) && (rh < ry || rh > ty) && (tw < tx || tw > rx) && (th < ty || th > ry));
      collisions.push({
        parentId: newCoords.id,
        destId: p.id,
        collision: collision
      });
    }
  });
  let info = document.querySelector('div.info');
  info.innerHTML = "";
  collisions.forEach(function(element) {
    info.innerHTML += `${element.parentId} collision with ${element.destId} is ${element.collision}.  <br/>`;
  });
}

关于javascript - Vanilla JS Div 碰撞检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41538882/

相关文章:

html - 如何使用 CSS 创建具有多个色标的水平渐变?

input - 运动对象未检测到任何碰撞 - Godot

c++ - 碰撞不一致

javascript - 如何在 side Array 中创建一个对象并将其推送到 object-javascript ?

javascript - CSS半固定元素?

javascript - 在 MouseOver 的 DataList ItemTemplate 内的 div 上使用 Eval?

javascript - HtmlUnit:HtmlPage 序列化异常

javascript - 悬停后如何将内容放入框中?

xcode - 我有两个物体,当碰撞时我想互相反弹,我怎样才能做到这一点,这样它就不会感觉到碰撞?

javascript - 如何使用jquery获取UL的if子属性?