繁体   English   中英

圆形LineSegment碰撞C ++(UE4)

[英]Circle LineSegment Collision C++ (UE4)

您好,我正在尝试计算Circle-LineSegment碰撞(在2D坐标空间中)。

我想在代理移动时检测到碰撞,所以我要逐步移动-> agentPosition是代理现在的位置, agentPosition + agentDelta是此步骤要移动的位置。

该行定义为FVertex (是的,我知道testEdge名字不好) testEdge (它有2点AB )。

该圆定义为(agentPosition + agentDelta)为中心, agentRadius为半径。

我试图找到一个碰撞点(这是2个可能的相交点的平均值)并计算所需的参数:

碰撞时间({0.0f,1.0f})CollisionPoint(碰撞时圆的位置)ImpactPoint(碰撞的平均点)

这是我尝试过的方法,但是我没有运气:(我得到负数或大于1.0f时间,有时有时候该碰撞点位于线段的另一侧,还有其他有趣的工件。

如果您能帮助我在这里找到我在做什么错,我将不胜感激。

static bool CheckAgentEdgeCollision(FVertex testEdge, FVector agentPosition, FVector agentDelta, float agentRadius, FCollisionResult2Dplease& outCollisionResult, UWorld* world = nullptr)
{
    agentPosition.Z = 0.0f;
    agentDelta.Z = 0.0f;
    testEdge.B.Z = 0.0f; testEdge.A.Z = 0.0f;

    FVector D = testEdge.B - testEdge.A;
    FVector d = testEdge.A - (agentPosition + agentDelta);

    float a = D | D;            // Operator | is Dot Product
    float b = (d | D) * 2.0f;   
    float c = (d | d) - FMath::Square(agentRadius);

    float disc = b * b - 4.0f * a * c;
    if (disc < KINDA_SMALL_NUMBER) 
    {
        return false;
    }

    float sqrtDisc = FastSQRoot(disc);

    float invA = 1.0f / ( a * 2.0f );

    float t0 = (-b - sqrtDisc) * invA;
    float t1 = (-b + sqrtDisc) * invA;

    FVector poin1 = FVector::ZeroVector;
    FVector poin2 = FVector::ZeroVector;

    poin1 = testEdge.A + t0 * D;
    poin2 = testEdge.A + t1 * D;

    bool p1 = true;
    bool p2 = true;

    if(t0 > 1.0f || t0 < 0.0f)
    {
        //disregard
        p1 = false;
    }

    if (t1 > 1.0f || t1 < 0.0f)
    {
        p2 = false;
    }

    if(!p1 && !p2)
    {
        return false;
    }
    else if(!p1)
    {
        poin1 = poin2;
    }
    else if (!p2)
    {
        poin2 = poin1;
    }

    float invRadius = 1.0f / agentRadius;

    agentRadius += 5.0f; 

    //Average the points:
    FVector impactPoint = (poin1 + poin2) / 2.0f;


    FVector directionToCircle = agentPosition - impactPoint;  
    FastNormalize(directionToCircle);

    FVector collisionPoint = directionToCircle * agentRadius + impactPoint;

    float distToCollision = FastSQRoot(agentPosition.DistSquared2D(agentPosition, collisionPoint));
    float speed = FastSQRoot(agentDelta.SizeSquared2D());

    float outTime = 0.0f;
    if (speed != 0.0f)
    {
        outTime = distToCollision / speed;
    }

    outCollisionResult.m_bIsPawn = false;
    outCollisionResult.m_edge = testEdge;
    outCollisionResult.m_normal = directionToCircle;
    outCollisionResult.m_collisionPoint = collisionPoint;
    outCollisionResult.m_time = outTime;
    outCollisionResult.m_bHit = true;
    outCollisionResult.m_impactPoint = impactPoint;

    outCollisionResult.m_binPenetration = outTime < 0.0f;
    return true;
}

暗示:

如果线段是固定的,则可以通过在使线段膨胀的同时“缩小”圆来重现问题,这将为您提供一个移动点而不是一个“胶囊”。 如您所见,碰撞发生在中心轨迹与胶囊轮廓之间的交点处,该交点可能沿着直线或圆形边缘发生。

在此处输入图片说明

通过旋转场景使该段更容易到达X轴,从而使计算变得更加容易。

暂无
暂无

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

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