与SAT碰撞实现(canvas,javascript) [英] Collision realisation with SAT (canvas, javascript)

查看:201
本文介绍了与SAT碰撞实现(canvas,javascript)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有这个简单的2d列车sim ,我已经一个漂亮的SAT实现,工作没有错误(至少我没有偶然发现任何):

So, I have this simple 2d train sim, and I already made a pretty nice SAT realisation, that works without errors (at least I have not stumbled upon any):

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,如果有冲突。正如我已经说过,它的工作正常。问题出现之后,当我试图创建一个反应的碰撞。我一直在努力的算法,几乎一个星期,这是我想出的最佳解决方案:

It detects possible collision objects for self and returns their ID's if there is a collision. As I already said, it works just fine. The problem appears after that, when i'm trying to create a reaction on the collision. I've been struggling over the algorithm for almost a week, and that's the best solution that I came up with:

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);
}

此功能会降低列车的速度, c $ c> calcSpeedIncrement(self,other))到它崩溃的火车。我在直线轨道上得到了一个很好的碰撞效果,但如果你继续前进一列火车滑过另一个。同样的滑过的另一个问题是当其中一列火车站在转弯时的碰撞。

This function decreases the speed of the train, and adds some speed (calcSpeedIncrement(self,other)) to the train it crashed into. I'm getting a nice collision effect in the straight tracks, but if you keep pushing forward one train slides over the other. And another problem with the same "sliding over" is the collision when one of the trains is standing on the turn.

有没有人有任何想法如何解决这些问题?

Does anyone have any ideas on how to solve these problems?

推荐答案

我不会试图重新发明车轮,去寻找现有的解决方案。你看过非常流行的2D物理引擎 Box2D 吗?在JavaScript中这是一个很好的实现

I would simple not try to re-invent the wheel and go for an existing solution. Have you looked at the very popular 2D-physics engine Box2D? Here is a nice implementation of it in JavaScript.

这篇关于与SAT碰撞实现(canvas,javascript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆