-->

HTML5 / kineticJS getIntersection功能的实现(HTML5 / kin

2019-07-19 21:33发布

我学习kineticjs通过在提供的教程http://www.html5canvastutorials.com ,东西也不错,容易理解,但是,我有了解的问题getIntersection ,我想在不同的对象之间使用功能dragging来检测collision / overlapping的对象。

据我明白的例子中的getIntersection函数期望的位置,并检查其是否与任何其他物体或不相交..

虽然我得到了他们但也有一些问题。

我无法做到这一点..

下面是我现在已经尝试到代码..

<script>

var stage = new Kinetic.Stage({
    container: 'container',
    width: 1000,
    height: 500,
    opacity: 0.5
});

var layer = new Kinetic.Layer();
var previous_position;
var new_position;
var collision = false;
var colors = ['red', 'orange', 'yellow', 'green', 'blue', 'purple'];
var yellowBox = null;
for(var n = 0; n < 6; n++) {
    // anonymous function to induce scope
    (function() {
        var i = n;
        if(n < 3){
            y = 50;
            x = i * 100 + i * 10;
        }else{
            y = 150;
            x = (i - 3) * 100 + (i - 3) * 10 ;
            if(n == 3){
                x = 0;
            }

        }
        var box = new Kinetic.Rect({
            x: x,
            y: y,
            width: 100,
            height: 50,
            fill: colors[i],
            stroke: 'black',
            strokeWidth: 4,
            draggable: true,
            name: colors[i]
        });

        box.on('dragstart', function() {
            previous_position =     {
                                        x: this.attrs.x,
                                        y: this.attrs.y 
                                    }; 
        });

        box.on('dragend', function() {
            if(collision){
                //this.setPosition(previous_position);
                layer.draw();
                collision = false;
            }else{
                //this.setPosition(new_position);
                layer.draw();
            }
        });

        box.on("dragmove", function(evt) {
            console.log(layer.children.length);
            if(layer.children.length > 1){
                console.log('dragging');
                new_position = {x: this.attrs.x,
                            y: this.attrs.y};
        //              var posBL = {x: this.attrs.x,
        //                          y: this.attrs.height + this.attrs.y};
        //              var posTR = {x: this.attrs.x    + this.attrs.width,
        //                          y: this.attrs.y};
                var posBR = {x: this.attrs.x    + this.attrs.width,
                            y: this.attrs.y + this.attrs.height };
                var collisionTL = this.getStage().getIntersections(new_position);
        //              var collisionBL = this.getStage().getIntersections(posBL);
        //              var collisionTR = this.getStage().getIntersections(posTR);
        //              var collisionBR = this.getStage().getIntersections(posBR);

                console.log(collisionTL);
                console.log(collisionTL.shapes);

        //              if(collisionTL.length > 1 || collisionBL.length > 0 || collisionTR.length > 0 || collisionBR.length > 0){
                if(collisionTL.length > 1){
                    console.log(collisionTL.shapes);
                    collision = true;
                }else{ //if(collisionBR.length > 0){
                    collision = true;
                }
        //              for(i=0; i < collision.length; i++){
        //              //  console.log(collision[i]._id);
        //              }
            }
        });


        if(colors[i] === 'yellow') {
            yellowBox = box;
        }

        layer.add(box);
    })();
}

stage.add(layer);

</script>

dragmove事件中,你guyz可以看到我拿到拖箱{现在评论}的四个角落位置,并与这我能够检测重叠/冲突,但它有2个问题:

1.在我的测试只有3个对象很慢

2.如果角点的非交叉它不火的碰撞东西{这一个盒子可以更大,因此它可以覆盖其他完全}

我会非常apreciate如果任何人都可以请帮我完成这个东西..

[A]任何通过任何手段,如果拖曳对象重叠任何其他对象我想让它显示碰撞。

[B]如果可能化妆getIntersection在一个特定的层组取其可以[C]旁任何其他的解决方法工作kineticJS实现上述任务

问候

Answer 1:

好吧,KineticJS的开发商正在改善.getIntersections()函数......至少,他说他是。 但是,直到功能得到改善,你必须使自己的碰撞检测功能。 假设你的对象是矩形或可以分解成一系列你应该像这样去点:

创建其确定的点是在一个形状(如果是矩形的角是另一矩形内),像这样的函数:

 function checkCollide(pointX, pointY, objectx, objecty, objectw, objecth) { // pointX, pointY belong to one rectangle, while the object variables belong to another rectangle
      var oTop = objecty;
      var oLeft = objectx; 
      var oRight = objectx+objectw;
      var oBottom = objecty+objecth; 

      if(pointX > oLeft && pointX < oRight){
           if(pointY > oTop && pointY < oBottom ){
                return 1;
           }
      }
      else
           return 0;
 };

那么你可以做一个大循环,通过该层中的所有对象遍历检查碰撞,就像这样:

 var children = layer.getChildren();
 for( var i=0; i<children.length; i++){  // for each single shape
     for( var j=0; j<children.length; j++){ //check each other shape
         if(i != j){ //skip if shape is the same
            if(checkCollide(children[i].getX(), children[i].getY(), children[j].getX(), children[j].getY(), children[j].getWidth(), children[j].getHeight()))
                alert('top left corner collided');
         }
     }
 }

我公司提供的checkCollide函数只检查每个形状左上角的碰撞,所以你必须修改函数来检查各个角落,这不是很长的重写,而即使在这里也有很多教程上的计算器,其处理“边界矩形碰撞检测”

这看起来似乎是一个非常沉重的功能,但令人惊讶仍比.getIntersections()更快。 此外,你应该在额外抛出if语句,这样函数不会通过所有的检查运行所有的时间。

我创建了一个游戏自己并使用.intersects(),并有很多的放缓。 我切换到这种类型的“简单”碰撞检测的,现在我的游戏周边60FPS运行。 http://cs.neiu.edu/~tsam/physics/index.phtml (测试/测试),如果你想看看。 您可以查看网页源代码,看看我是如何构造的碰撞检测更有效率(如checkIntersectsGoal()函数。



文章来源: HTML5 / kineticJS getIntersection function implementation