2d圆矩形碰撞和反射不起作用

时间:2018-05-26 13:27:33

标签: algorithm math reflection 2d collision

我有通过矩形构建的地图的游戏,较暗的矩形(命名为“封闭”)意味着球应该能够移动的地方,球应该从较亮的矩形(命名为“开放”)边界反射。将来我会添加更多球,它们会相互反射。

问题在于碰撞后的新Vector。

我强制函数circleRectGetCollisionNormal()返回向量(-1,0)我认为这种情况的正常情况(球向正方向移动)。 球以度数开始并将其简单地改为矢量,这种反射工作45度,但当我将角度改为10度时,球移动到较亮的矩形(命名为“打开”)。

Here is how it looks like (Picture)

我这样做:

1 - 检查球是否与较浅的矩形相撞, 2 - 如果碰撞,我想改变方向,所以我返回矢量,例如球的右侧与矩形返回[-1,0]碰撞(我认为它是垂直线的法线,它指向左方向)。 / p>

从这个等式计算新球移动矢量:newMoveVector = oldMoveVector − (2 * dotProduct(oldMoveVector, normalVector) * normalVector)

以下是每个步骤的代码:

1

   circleRect(circlePos, circleSize, rectPos, rectSize) { 
//its rectRect collision but it doesnt matter because reflection surface is always horizontal or vertical
        let r1 = {
            left: circlePos.x - circleSize.x/2,
            right: circlePos.x + circleSize.x/2,
            top: circlePos.y - circleSize.y/2,
            bottom: circlePos.y + circleSize.y/2
        };
        let r2 = {
            left: rectPos.x,
            right: rectPos.x + rectSize.x,
            top: rectPos.y,
            bottom: rectPos.y + rectSize.y
        };  
    return !(r2.left > r1.right || 
        r2.right < r1.left || 
        r2.top > r1.bottom ||
        r2.bottom < r1.top);
    }

    isOnOpenTile(pos: Vector, size: Vector) {
        let openTiles = this.getTiles('open');
        let result = false;
        openTiles.forEach(element => {
            if( this.circleRect(pos,size,element.pos,element.size) ){
                 result = element;
                return;
            }
        });
        return result;
    }

2

    circleRectGetCollisionNormal(c, r) {
        if(c.pos.y <= r.pos.y - (r.size.y/2)) return new Vector(0,-1);
        //Hit was from below the brick
        if(c.pos.y >= r.pos.y + (r.size.y/2)) return new Vector(0,1);
        //Hit was from above the brick
        if(c.pos.x < r.pos.x) return new Vector(1,0);
        //Hit was on left
        if(c.pos.x > r.pos.x) return new Vector(-1,0);
        //Hit was on right
        return false;
    }

3

    getNewMoveVector(moveVector, normalVector) {
        normalVector = this.normalize(normalVector);
        let dot = (moveVector.x * moveVector.y) + (normalVector.x * normalVector.y);
        let dotProduct = new Vector(dot, dot);
        return moveVector.sub(dotProduct.mult(normalVector).mult(new Vector(2,2)));
    }

    normalize(v) {
        let length = Math.sqrt((v.x*v.x) + (v.y*v.y));
        return new Vector(v.x/length,v.y/length);
    }

这是

的主要功能
        getMoveVectorOnCollision(circle) {
        let coll = this.isOnOpenTile( circle.pos, circle.size );
        if( coll != false) {
            let vector = this.circleRectGetCollisionNormal(circle, coll);
            return this.getNewMoveVector(circle.moveVector, vector);
        } else return false;
    }

对象向量总是包含2个值,所有函数(mult,sub,div,add)都像这里一样工作。

sub(vector: Vector) {
    return new Vector(this.x - vector.x, this.y - vector.y);
}

请给我建议,实际解决方案或讲述做这种反思的不同方式。我浪费了3天以上的时间试图解决这个问题,我必须继续前进。

1 个答案:

答案 0 :(得分:0)

Yor dot产品计算错误。改变这些行:

<?php

function ifNumberExists($vid, $vidDB) {
    $status = false;
    $vidDBItemArray = array();
    foreach($vid as $vidItem) {
        foreach($vidDB as $vidDBItem) {
            $vidDBItemArray = str_split($vidDBItem);
            foreach($vidDBItemArray as $vidDBItemArrayValue) {
                if($vidItem == $vidDBItemArrayValue) {
                    $status = true;
                }
            }
        }
    }
    return $status;
}

?>

这一行:

let dot = (moveVector.x * moveVector.y) + (normalVector.x * normalVector.y);
let dotProduct = new Vector(dot, dot);

请注意,let dotProduct = (moveVector.x * normalVector.x + moveVector.y * normalVector.y); 标量值,而不是矢量,因此您必须为减法制作矢量

dotProduct

subvec.x = 2 * dotProduct * normalVector.x
subvec.y = 2 * dotProduct * normalVector.y