碰撞检测,计算到障碍物的距离

时间:2015-12-16 07:31:39

标签: javascript oop typescript collision-detection

所以我在使用2d碰撞检测工作时遇到了一些问题。检查两个物体是否相交并不是我的问题所在,而是让物体只能移动到障碍物而不再进一步。

我检测碰撞的尝试是这样的:

我试图计算物体在撞到障碍物之前可以移动的距离,然后才将它移动到那么远。 为此,我采用物体的速度并将其与矩形中的四个角耦合,产生四条线。 然后我检查这些线是否与障碍线相交。 如果有一个交叉点,我会发现这个交叉点最多缩短了哪条线,并用它来确定新的最高可能长度,然后才能到达交叉点。并用它替换物体的速度。

现在,物体陷入障碍物中,我还没有找到应该开始寻找问题的线索。

找到交集的代码来自:How do you detect where two line segments intersect? - 我知道我在这里有一些冗余。

api

我应该以某种方式简化我的对象类,但还没有想出如何。但现在他们看起来像这样:

static linesIntersect(l1: cLine, l2: cLine): boolean { //returns true if the lines intersect.
    var s1_x = l1.p2.x - l1.p1.x;
    var s1_y = l1.p2.y - l1.p1.y;
    var s2_x = l2.p2.x - l2.p1.x;
    var s2_y = l2.p2.y - l2.p1.y;
    var s = (-s1_y * (l1.p1.x - l2.p1.x) + s1_x * (l1.p1.y - l2.p1.y)) / (-s2_x * s1_y + s1_x * s2_y);
    var t = (s2_x * (l1.p1.y - l2.p1.y) - s2_y * (l1.p1.x - l2.p1.x)) / (-s2_x * s1_y + s1_x * s2_y);
    if ((s >= 0) && (s <= 1) && (t >= 0) && (t <= 1)) { return true; }
    else { return false; }
}
static linesIntersection(l1: cLine, l2: cLine): cPoint { //returns the point in which the lines intersect.
    var s1_x = l1.p2.x - l1.p1.x;
    var s1_y = l1.p2.y - l1.p1.y;
    var s2_x = l2.p2.x - l2.p1.x;
    var s2_y = l2.p2.y - l2.p1.y;
    var s = (-s1_y * (l1.p1.x - l2.p1.x) + s1_x * (l1.p1.y - l2.p1.y)) / (-s2_x * s1_y + s1_x * s2_y);
    var t = (s2_x * (l1.p1.y - l2.p1.y) - s2_y * (l1.p1.x - l2.p1.x)) / (-s2_x * s1_y + s1_x * s2_y);
    return new cPoint(l1.p1.x + (t * s1_x), l1.p1.y + (t * s1_y));
}
static possibleIntersectionLines(cElements: cElement[], player: cElement): cLine[] {
    var possibleLines: cLine[] = []; //All elements 4 lines (as I'm only working with rectangles at the moment
    for (var i = 0; i < cElements.length; i++) { //Run through all elements
        if (cElements[i] != player) { //but not itself
            var newPossibleLines = possibleLines.concat(cElements[i].sides); //And add these lines to an array.
            possibleLines = newPossibleLines;
        }
    }
    return possibleLines;
}
static ModifiedVelocityIfPlayerCollideInNextFrame(cElements: cElement[], player: cElement): Vector { //Return a new vector which indicate how long the player can move before hitting an object.
    var cLinesToCheck = Collission.possibleIntersectionLines(cElements, player); //Get all the lines which could be in the way.
    var playerLines = [player.lineAB, player.lineBC, player.lineCD, player.lineDA]; //Get the players lines.   
    var maxVelocity = player.velocity; //Get the current velocity of the player. 
    for (var i = 0; i < cLinesToCheck.length; i++) { //Check all the element's lines.  
        for (var j = 0; j < playerLines.length; j++) { //against the player's lines.
            if (Collission.linesIntersect(playerLines[j], cLinesToCheck[i])) { //If there is an collission
                var collissionPoint = Collission.linesIntersection(playerLines[j], cLinesToCheck[i]); //Get the Collission point
                var possibleMaxVelocity = new Vector(collissionPoint.x - playerLines[j].p1.x, collissionPoint.y - playerLines[j].p1.y); //Create a new vector out of it.
                if (maxVelocity.Length > possibleMaxVelocity.Length) maxVelocity = possibleMaxVelocity; //If it's shorter than the current velocity, set the current velocity to the new value.
            }
        }
    }
    return maxVelocity;
}

提前感谢您的时间。

0 个答案:

没有答案