-->

拖动碰撞(Dragging collisions)

2019-07-22 13:33发布

我是很新的这两个帆布和Kineticjs但我觉得我所attemping要容易得多,然后我让出来的人。 基本上,这是我到目前为止有:

我试图使用的代码是从kineticjs停止拖动形状时,与另一重叠的解决方案,但无法得到它的工作。

请检查现场的jsfiddle 码

var isRectCollide = function(target, box) {
  if (target.x - target.width  >= box.x + box.width  &&
  target.y - target.height >= box.y + box.height &&
  target.x + target.width  <= box.x + box.width  &&
  target.x + target.height <= box.y - box.height )
    return false;
  else
    return true;
}

这样做的想法是粉红色的方形为可拖动而是由橙色盒子,一旦周围的橙色框拖和粉红色的盒子“触及”蓝盒子堵塞,弹出应该发生。

我不知道,如果使用kineticjs是实现这虽然最简单的方法?

任何想法,建议或帮助,我会非常感激。

Answer 1:

是的,因为KineticJS没有碰撞测试你必须做你自己。

这里是任何2个kineticJS矩形之间的碰撞试验:

function theyAreColliding(rect1, rect2) {
  return !(rect2.getX() > rect1.getX()+rect1.getWidth() || 
           rect2.getX()+rect2.getWidth() < rect1.getX() || 
           rect2.getY() > rect1.getY()+rect1.getHeight() ||
           rect2.getY()+rect2.getHeight() < rect1.getY());
}

这里是你将如何调用框和障碍物之间的碰撞试验:

if( theyAreColliding(box,obstacle){
      // obstacle is blocking box
      alert("You are being blocked!");
}

这里是你将如何调用框和目标之间的碰撞试验:

if( theyAreColliding(box,target){
      // box touched the goal
      alert("Goal!");
}

从右通过障碍停止拖动箱子,你必须给框像这样的自定义功能拖拽:

dragBoundFunc: function(pos){
    if(theyAreColliding(box,obstacle){
        // box is touching obstacle
        // don't let box move down
        return({ x:pos.x, y:obstacle.getY()-1 });
    } else{
        // box is not touching obstacle
        // let it move ahead
        return({ x:pos.x, y:pos.y });
    }
}

你可以看到这是如何在工作中演示: http://www.html5canvastutorials.com/kineticjs/html5-canvas-drag-and-drop-bounds-tutorial-with-kineticjs/

[编辑:指定每个码去]

我把碎片汇集成下文一个工作片段。 我没有找到一个不幸的事情。 这有可能为用户拖动粉色框速度不够快,笔直地穿过障碍物KineticJS无法作出反应,以阻止一个非常快的拖累。

而且 - 哎呀我。 我在上面的theyAreColliding功能纠正了一些缺少括号。

该dragBoundFunc去作为除盒构造(见下面的代码)。

您可以测试,如果用户可以通过在框中的“dragmove”处理程序测试,这样的目标:

  box.on('dragmove', function() {
    if (theyAreColliding(box, target)) {
        // box touched the goal
        alert("Goal!");
    }      
  });

这里是代码和一个小提琴: http://jsfiddle.net/uCAys/

<!DOCTYPE HTML>
<html>
  <head>
    <style>
body {
    margin: 0px;
    padding: 20px;
}
canvas {
    border: 1px solid #777;
}
    </style>
  </head>
  <body>
    <div id="container"></div>
    <script src="http://www.html5canvastutorials.com/libraries/kinetic-v4.3.1-beta2.js"></script>
    <script>
        var stage = new Kinetic.Stage({
            container: 'container',
            width: 300,
            height: 300
        });
        var layer = new Kinetic.Layer();

        //Dragable Pink box
        var box = new Kinetic.Rect({
            x: 100,
            y: 50,
            width: 100,
            height: 50,
            fill: 'pink',
            stroke: 'black',
            strokeWidth: 2,
            draggable: true,
            // this causes box to be stopped if contacting obstacle
            dragBoundFunc: function(pos){
                if(theyAreColliding(box,obstacle)){
                    // box is touching obstacle
                    // don't let box move down
                    return({ 
                        x: pos.x, 
                        y: Math.min( obstacle.getY()-box.getHeight()-1, pos.y)
                    });
                } else{
                    // box is not touching obstacle
                    // let it move ahead
                    return({ x:pos.x, y:pos.y });
                }
            } 
        });

      box.on('dragmove', function() {
        if (theyAreColliding(box, target)) {
            // box touched the goal
            box.setX(100);
            box.setY(50);
            alert("Goal!");
        }      
      });

        // End goal blue box
        var target = new Kinetic.Rect({
            x: 100,
            y: 200,
            width: 100,
            height: 50,
            fill: 'blue',
            stroke: 'black',
            strokeWidth: 2
        });

        // Obstacle/blocker orange box
        var obstacle = new Kinetic.Rect({
            x: 125,
            y: 145,
            width: 50,
            height: 30,
            fill: 'orange',
            stroke: 'black',
            strokeWidth: 2
        });

        function theyAreColliding(rect1, rect2) {
            return !(rect2.getX() > rect1.getX() + rect1.getWidth() ||  //
                     rect2.getX() + rect2.getWidth() < rect1.getX() ||  // 
                     rect2.getY() > rect1.getY() + rect1.getHeight() ||   //
                     rect2.getY() + rect2.getHeight() < rect1.getY());  //
        }

        layer.add(box);
        layer.add(obstacle);
        layer.add(target);
        stage.add(layer);

    </script>
  </body>
</html>


文章来源: Dragging collisions