My implementation of the following can be found on jsfiddle.net
I have four divs. My goal is to make them draggable around the page but NOT to allow them to overlap one another. Each can be dragged around the page with a mousemove listener.
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`;
}
}
My collision detection function works, strictly speaking. I output the "Collision" event to a div so you can see this in the fiddle when you drag it around. BUT, you are still able to drag the divs on top of one another.
They kind of "stick" a little bit when you try to pull them apart, and if you keep pushing at them they'll overlap. The collision detection vacillates between true/false pretty rapidly at this point, so I'm guessing maybe some weirdness is going on here, but I can't figure it out.
I think one problem might be that the collision detection only outputs a collision when the borders are equal. That is, it returns false once a collision has happened and one element is inside another.
However, I can't see how my mousemove e.movementX and e.movementY events are able to get past the collision test and move the div.