简体   繁体   English

碰撞检测性能差

[英]collision detection poor performance

I am building a game using KineticJs and it is using drag and drop to move elements on screen and I have big performance problems because I check on dragmove event if element that you are dragging is colliding with possible snap positions and then the dragging element gets all laggy. 我正在使用KineticJs构建游戏,并且正在使用拖放操作在屏幕上移动元素,所以我遇到了很大的性能问题,因为我检查了dragmove事件,如果您拖动的元素与可能的捕捉位置发生碰撞,然后拖动元素得到了全部ggy。 I am using kineticJs built in functions but i have a feeling that they are not optimized well. 我使用的是内置函数的dynamicJ,但是我感觉它们的优化效果不佳。 It actually works pretty decent in chrome(you can see the lag) but it is not good in firefox. 它实际上在chrome中效果不错(您可以看到滞后),但是在Firefox中效果不好。 Any ideas on how to optimize my code 关于如何优化我的代码的任何想法

element.on('dragmove',function(evt){
    var position = stage.getUserPosition(evt);
    var snap = snapGroup.getIntersections(position.x,position.y);
    if(snap.length > 0) snapElement(snap[0]);
}

Find the bottleneck. 找到瓶颈。 I'm guess it's this line: 我猜是这行:

var snap = snapGroup.getIntersections(position.x,position.y);

If so, set up a counter and only update every few ticks of the update. 如果是这样,请设置一个计数器,并仅每隔几个更新更新一次。 Make sure to do a final update on dragend so you end up with correct final position. 确保对dragend进行最终更新,以便最终获得正确的最终位置。

EDIT: 编辑:

Perhaps something like this may work: 也许这样的事情可能会起作用:

var dragUpdateCount = 0;
var dragUpdateRate = 5;
element.on('dragmove',function(evt){
    dragUpdateCount++;
    if (dragUpdateCount >= dragUpdateRate) {
        var position = stage.getUserPosition(evt);
        var snap = snapGroup.getIntersections(position.x,position.y);
        if (snap.length > 0) {
            snapElement(snap[0]);
        }
        dragUpdateCount = 0;
    }
}

As @Aram pointed out, the bottleneck is with the .getIntersections() function. 正如@Aram指出的那样,瓶颈在于.getIntersections()函数。

The .getIntersections() function is very memory/processing intensive, so what you need is a preliminary check which would limit the processing to certain locations. .getIntersections()函数占用大量内存/处理资源,因此您需要进行初步检查,以将处理限制在某些位置。 I had a similar problem with a little game I made and the collision detection of .getIntersections() was killing my speed, I also tried .intersects() but it has similar results. 我制作的一款小游戏也遇到了类似的问题,.getIntersections()的碰撞检测正在破坏我的速度,我也尝试了.intersects(),但结果相似。 What I did was something like this: (don't know if this works ok with rotations) 我所做的是这样的:(不知道旋转是否可以正常工作)

 element.on('dragmove',function(){ //dont think you need the 'evt' passed here
     var position = stage.getUserPosition();
     var snapChildren = snapGroup.getChildren();  //get each snapGroup child
     for(var i=0; i<snapChildren.length; i++){
          if(position.x > snapChild[i].getX() && position.x < snapChild[i].getX()+snapChild[i].getWidth()){ // check horizontal bounding rectangle
                if(position.y > snapChild[i].getY() && position.y < snapChild[i].getY()+snapChild[i].getHeight(){ // check vertical bounding rectangle
                    var snap = snapGroup.getIntersections(position.x,position.y);
                    if(snap.length > 0) snapElement(snap[0]);
                }
           }
     }
 }

So this is a 'bounding rectangles' collision detection approach, and should minimize the processing you have to do. 因此,这是一种“边界矩形”碰撞检测方法,应将您必须执行的处理最小化。

Even though you are getting all the children and processing them in a for loop, it's still faster than .getIntersections() 即使您获得所有子代并在for循环中对其进行处理,它仍然比.getIntersections()更快

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM