응애맘마조

230725 강의 본문

공부/3D강의

230725 강의

TH.Wert 2023. 7. 25. 19:08

어제에 이어서 다익스트라에 대해 강의했습니다. 오늘은 완성이 되었습니다. 동시에 과제였습니다.

bool            IntersectCollider(Ray WRay);
int             GetDiNodeIdx(Vector3 pos);

터레인 헤더 파일에서 장애물을 놓았을 때 피해서 가는 길을 만들기 위해 충돌 함수와 경유하는 노드를 찾는 함수를 만들었습니다.

int  Terrain::GetDiNodeIdx(Vector3 pos)
{
    int idx = 0;
    float min = FLT_MAX;
    for (auto it = di.list.begin();  it != di.list.end(); it++)
    {
        Vector3 minus = it->second->pos - pos;

        if (min > minus.Length())
        {
            min = minus.Length();
            idx = it->first;
        }
    }
    return idx;
}

bool Terrain::IntersectCollider(Ray WRay)
{
    bool result = false;
    float len = WRay.direction.Length();

    for (auto it = obList.begin(); it != obList.end(); it++)
    {
        Vector3 hit;
     
        if (it->second->Intersect(WRay, hit))
        {
            Vector3 dis = WRay.position - hit;
            float len2 = dis.Length();
            if (len > len2)
            {
                result = true;
            }
        }
    }
    return result;
}

cpp 파일입니다. 레이를 사용해서 길이 값을 이용했습니다. 2개 이상의 장애물이 있을 경우에 대해 예외처리를 하였습니다.

int				src, dest;
Vector3			srcPos, destPos;

list<Vector3>	way;
Vector3 		moveDir;
float			moveSpeed;
float			timer;
if (INPUT->KeyDown(VK_MBUTTON))
{
    Vector3 HitPoint;
    if (map->ComPutePicking(Util::MouseToRay(), HitPoint))
    {
        srcPos = dead->root->GetWorldPos();
        destPos = HitPoint;
        Ray srcToDest;
        srcToDest.position = srcPos;
        srcToDest.direction = destPos - srcPos;
        //중간에 컬라이더랑 충돌이라면
        if (map->IntersectCollider(srcToDest))
        {
            src = map->GetDiNodeIdx(srcPos);
            dest = map->GetDiNodeIdx(destPos);
            if (map->PathFinding(src, dest, way))
            {
                way.push_front(destPos);
                Vector3 dir = way.back() - dead->root->GetWorldPos();
                timer = dir.Length() / moveSpeed;
                dir.Normalize();
                moveDir = dir;
            }
        }
        //아니라면
        else
        {
            moveDir = srcToDest.direction;
            moveDir.Normalize();
            timer = srcToDest.direction.Length() / moveSpeed;
        }
    }
}

if (timer > 0.0f)
{
    dead->root->MoveWorldPos(moveDir * moveSpeed * DELTA);
    timer -= DELTA;
    if (not way.empty() and timer <= 0.0f)
    {
        way.pop_back();
        if (not way.empty())
        {
            Vector3 dir = way.back() - dead->root->GetWorldPos();
            timer = dir.Length() / moveSpeed;
            dir.Normalize();
            moveDir = dir;
        }
    }
}

어제는 에디터에서 겨우 어디로 이동하는지 위치만 보였지만 이젠 메인에서 확인할 수 있게 되었습니다.

장애물 있을 시 인근에 있는 노드로 이동해 경유해서 이동

읽어주셔서 감사합니다.

'공부 > 3D강의' 카테고리의 다른 글

230727 강의  (0) 2023.07.27
230726 강의  (0) 2023.07.26
230724 강의  (0) 2023.07.24
230721 강의  (0) 2023.07.23
230720 강의  (0) 2023.07.20
Comments