Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
#pragma once
#include <math.h>
#define WORLD_SIZE 64
struct SearchCell
{
pubic:
int m_xcoord, m_zcoord;
int m_id;
SearchCell *parent;
float G;
float H;
SearchCell() : parent(0) {}
SearchCell (int x, int z, SearchCell *_parent = 0: m_xcoord(x), m_zcoord(z),
parent(_parent), m_id(z * WORLD_SIZE + z), G(0), H(0) {};
float GetF() {
return G + H;
}
float ManHattanDistnce (SearchCell *nodeEnd)
{
float x = (float) (fabs(this->m_xcoord - nodeEnd->m_xcoord));
float x = (float) (fabs(this->m_zcoord - nodeEnd->m_zcoord));
return x+z;
}
};
=========================================
PathFinding.h
#pragma once
#include "Vector3.h"
#include "SearchCell.h"
#include <vector>
class PathFinding
{
public:
PathFinding (void);
~PathFinding(void);
private:
void SetStartAndGoal(SearchCell start, SearchCell goal);
void PathOpened (int x, int z, float newCost, SearchCell *parent);
SearchCell *GetNextCell ();
void ContinuePath();
Searchcell *m_startCell;
SearchCell *m_goalCell;
std::vector<SesrchCell*> m_openList;
std::vector<SesrchCell*> m_visitedList;
std::vector<Vecto3*> m_pathToGoal;
};
==================================
PathFinding.cpp
#include "PathFinding.h"
PathFinding::PathFinfing(void)
{
m_intializedStartGoal = false;
m_foundGoal = false;
}
PathFinding::~PathFinding(void)
{
}
void PathFinding::FindPath(Vector3 currentPos, Vector3 targetPos)
{
if (!m_initializedStartGoal)
{
for (int i= 0; i<m_openList.size(); i++)
{
delete m_openList[1];
}
m_openList.clear();
//inisialize start
SearchCell start;
start.m_xcoord = currentPos.x;
start.m_zcoord = currentPos.z;
//inisialize goal
SearchCell goal;
goal.m_xcoord = targetPos.x;
goal.m_zcoord = targetPos.z;
m_startCell->G = 0;
m_startCell->H = m_startCell->ManHattanDistnce (m_goalCell);
m_startCell->parent = 0;
m_opentList.push_back(m_startCell);
}
SearchCell* PathFindinf::GetNextCell()
{
float bestF = 999999.0f;
int cellIndex = 1;
searchCell* nextCell = NULL;
int id = z * WORD_SIZE + x;
for(int i = 0; i< m_visitedList.size(); i++)
{
if(id == m_visitedList[i]->m_id)
{
return;
}
}
void PathFinding::ContinuePath()
{
if(m_openList.empty())
{
return;
}
if(currenCell->m_id == m_goalCell->m_id)
{
m_goalCell->parent = currenCell->parent;
SearchCell*getPath;
m_foundGoal = true;
return;
}
else
{
//righSude
PathOpened(currentCell->m_xcoord + 1, currentCell->m_zcoord, currentCell->G + 1,
currentCell);
//leftSide
PathOpened(currentCell->m_xcoord - 1, currentCell->m_zcoord, currentCell->G + 1,
currentCell);
//Up
PathOpened(currentCell->m_xcoord, currentCell->m_zcoord + 1, currentCell->G + 1,
currentCell);
//Down
PathOpened(currentCell->m_xcoord, currentCell->m_zcoord - 1, currentCell->G + 1,
currentCell);
//left-up diagonal
PathOpened(currentCell->m_xcoord - 1, currentCell->m_zcoord + 1, currentCell->G +
1.414f, currentCell);
//right-up diagonal
PathOpened(currentCell->m_xcoord + 1, currentCell->m_zcoord + 1, currentCell->G +
1.414f, currentCell);
//left-down diagonal
PathOpened(currentCell->m_xcoord - 1, currentCell->m_zcoord - 1, currentCell->G +
1.414f, currentCell);
//right-down diagonal
PathOpened(currentCell->m_xcoord + 1, currentCell->m_zcoord + 1, currentCell->G +
1.414f, currentCell);
Vector3 nextPos;
nextPos.x = m_pathToGoal[m_pathTogoal.size() - index]->x;
nextPos.z = m_pathToGoal[m_pathTogoal.size() - index]->z;