繁体   English   中英

SAT的碰撞实现(画布,javascript)

[英]Collision realisation with SAT (canvas, javascript)

因此,我有一个简单的2d火车sim ,并且我已经做了一个相当不错的SAT实现,它可以正常工作(至少我没有偶然发现):

function calcCollision(self){
    var dist = self.distanceCheck();
    var possible = [], collision = [];
    var myBox, otherBox, myMin, myMax, otherMin, otherMax, myBoxRecalc = [], otherBoxRecalc = [];

    for (var i=0;i<trainCount;i++){
        if (dist[i]!="SELF"&&dist[i]<=(dTrain+10)){
            possible.push(i);
        }
    }

    if (possible.length!==0){

        myBox = self.box();

        self.hit = false;

        for (i=0;i<possible.length;i++){
            otherBox = window["train_"+possible[i]].box();

            //для self координат

            for (var j=0;j<4;j++){
                myBoxRecalc[j] = XYtoBoxCoordinates(self,myBox[j][0],myBox[j][1]);
                otherBoxRecalc[j] = XYtoBoxCoordinates(self,otherBox[j][0],otherBox[j][1]);
            }

            //для self координат, проекция на X

            myMin = myBoxRecalc[0][0];
            myMax = myBoxRecalc[0][0];

            otherMin = otherBoxRecalc[0][0];
            otherMax = otherBoxRecalc[0][0];

            for (j=0;j<4;j++){
                if (myBoxRecalc[j][0]<myMin) myMin=myBoxRecalc[j][0];
                if (myBoxRecalc[j][0]>myMax) myMax=myBoxRecalc[j][0];

                if (otherBoxRecalc[j][0]<otherMin) otherMin=otherBoxRecalc[j][0];
                if (otherBoxRecalc[j][0]>otherMax) otherMax=otherBoxRecalc[j][0];
            }

            //console.log(myMin + " " + myMax + " " + otherMin + " " + otherMax);

            if (otherMax<myMin||otherMin>myMax) break;

            //для self координат, проекция на Y

            myMin = myBoxRecalc[0][1];
            myMax = myBoxRecalc[0][1];

            otherMin = otherBoxRecalc[0][1];
            otherMax = otherBoxRecalc[0][1];

            for (j=0;j<4;j++){
                if (myBoxRecalc[j][1]<myMin) myMin=myBoxRecalc[j][1];
                if (myBoxRecalc[j][1]>myMax) myMax=myBoxRecalc[j][1];

                if (otherBoxRecalc[j][1]<otherMin) otherMin=otherBoxRecalc[j][1];
                if (otherBoxRecalc[j][1]>otherMax) otherMax=otherBoxRecalc[j][1];
            }

            //console.log(myMin + " " + myMax + " " + otherMin + " " + otherMax);

            if (otherMax<myMin||otherMin>myMax) break;

            //для other координат

            for (j=0;j<4;j++){
                myBoxRecalc[j] = XYtoBoxCoordinates(window["train_"+possible[i]],myBox[j][0],myBox[j][1]);
                otherBoxRecalc[j] = XYtoBoxCoordinates(window["train_"+possible[i]],otherBox[j][0],otherBox[j][1]);
            }

            //для other координат, проекция на X

            myMin = myBoxRecalc[0][0];
            myMax = myBoxRecalc[0][0];

            otherMin = otherBoxRecalc[0][0];
            otherMax = otherBoxRecalc[0][0];

            for (j=0;j<4;j++){
                if (myBoxRecalc[j][0]<myMin) myMin=myBoxRecalc[j][0];
                if (myBoxRecalc[j][0]>myMax) myMax=myBoxRecalc[j][0];

                if (otherBoxRecalc[j][0]<otherMin) otherMin=otherBoxRecalc[j][0];
                if (otherBoxRecalc[j][0]>otherMax) otherMax=otherBoxRecalc[j][0];
            }

            //console.log(myMin + " " + myMax + " " + otherMin + " " + otherMax);

            if (otherMax<myMin||otherMin>myMax) break;

            //для other координат, проекция на Y

            myMin = myBoxRecalc[0][1];
            myMax = myBoxRecalc[0][1];

            otherMin = otherBoxRecalc[0][1];
            otherMax = otherBoxRecalc[0][1];

            for (j=0;j<4;j++){
                if (myBoxRecalc[j][1]<myMin) myMin=myBoxRecalc[j][1];
                if (myBoxRecalc[j][1]>myMax) myMax=myBoxRecalc[j][1];

                if (otherBoxRecalc[j][1]<otherMin) otherMin=otherBoxRecalc[j][1];
                if (otherBoxRecalc[j][1]>otherMax) otherMax=otherBoxRecalc[j][1];
            }

            //console.log(myMin + " " + myMax + " " + otherMin + " " + otherMax);

            if (otherMax<myMin||otherMin>myMax) break;

            collision.push(possible[i]);

        }
    } else return false;

    if (collision.length!==0){
        self.hit = true;
        return collision;
    } else return false;
}

它会self检测可能的碰撞对象,并在发生碰撞时返回其ID。 正如我已经说过的,它工作正常。 之后,当我试图对碰撞产生反应时,问题就出现了。 我一直在为算法苦苦挣扎近一个星期,这是我想到的最好的解决方案:

function moveCollided(){

    for (var i = 0; i < trainCount; i++) {
        var banged = calcCollision(window["train_"+i]);
        //console.log(banged);
        if (window["train_"+i].hit){
            window["train_"+i].speed -= (window["train_"+i].speed/3);
            for (var j = 0; j < banged.length; j++) {
                window["train_"+banged[j]].speed += calcSpeedIncrement(window["train_"+i],window["train_"+banged[j]]);
            }
        }
    }

    setTimeout(moveCollided, 15);
}

此函数降低火车的速度,并向calcSpeedIncrement(self,other)的火车增加一些速度( calcSpeedIncrement(self,other) )。 我在直线轨道上得到了很好的碰撞效果,但是如果您继续向前推动一列火车在另一列上滑行。 同样的“滑倒”的另一个问题是其中一列火车在转弯处站立时发生碰撞。

是否有人对如何解决这些问题有任何想法?

我不会简单地尝试重新发明轮子并寻求现有解决方案。 您是否看过非常流行的2D物理引擎Box2D 这是 JavaScript的一个不错的实现

暂无
暂无

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

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