x
Yes
No
Do you want to visit DriveHQ English website?
首页
产品服务
价格
免费试用
下载客户端
关于我们
云文件服务
|
云备份服务
|
FTP服务
|
企业邮箱服务
|
网站托管
|
客户端软件
云文件服务
云备份服务
FTP服务
企业级邮箱服务
网站托管
客户端软件
Map.cpp - Hosted on DriveHQ Cloud IT Platform
返回上层目录
上传
下载
共享
发布
新建文件夹
新建文件
复制
剪切
删除
粘贴
评论
升级服务
路径: \\game3dprogramming\materials\GameFactory\GameFactoryDemo\Map.cpp
旋转
特效
属性
历史版本
//#include "DarkPuzzle.h" #include "Map.h" #include "precompiled.h" #include "GameFactoryDemo.h" #include "Soldier.h" //#include "GameLevel.h" #define DEBUG_OUTPUT(x) {;} using namespace GameFactory; float Map::WalkableHeight = 1.0f; //should represent the smallest slope float Map::GridSize = 5.0f; //should be equal to the size of movable object float Map::NegativeHeight = -10.0f;//height of the map 'pitfall' float Map::FootHeight = 1.0f; //to compute connection between map cells int Map::InfiniteDistance = 30000; //the maximum distance in distance map int Map::MaximumSearchDistance = 50; //this parameter will affect searching performance Map* Map::theMap = NULL; namespace GameFactory{ extern FILE* fdebug; } Map::Map(float MapSizeX, float MapSizeY){ assert(theMap==NULL); theMap = this; this->mapSizeX = MapSizeX; this->mapSizeY = MapSizeY; this->sizeX = (int)ceil(MapSizeX/Map::GridSize); this->sizeY = (int)ceil(MapSizeY/Map::GridSize); this->totalSize = sizeX*sizeY; initHeight(); initCellConnection(); } void Map::initHeight(){ this->heightStatic = new Matrix
(sizeX,sizeY); for (int x=0;x
set(x,y,Map::NegativeHeight); } Vec3 from,to,hit,norm; for (int x=0;x
RayCast(from,to,&vhit,&vnorm)){ heightStatic->set(x,y,vhit.z); } } } } void Map::initCellConnection(){ //NOTES: Don't use body to check for accessible cell because this will not work on //non-planar ground (the body will collide with the ground) Vec3 from,to; bool hit; connectX = new Matrix
(sizeX,sizeY);//for easy indexing, ONLY (sizeX-1)*sizeY is used for (int x=0;x
10.0f) connectX->set(x,y,false); else{ if (IsAccessible(to)){ from = from - Vec3(GridSize/2,0,0);//inlcude the hero body to = to + Vec3(GridSize/2,0,0); hit = SkeletalApplication::GetSingleton()->RayCastTwoWay(from,to); //include the hero body, not allow him to pass narrow space if (!hit) hit = SkeletalApplication::GetSingleton()->RayCastTwoWay(from+Vec3(0,GridSize/2,0),to+Vec3(0,GridSize/2,0)); if (!hit) hit = SkeletalApplication::GetSingleton()->RayCastTwoWay(from-Vec3(0,GridSize/2,0),to-Vec3(0,GridSize/2,0)); connectX->set(x,y,!hit); }else connectX->set(x,y,false); } }else connectX->set(x,y,false); } } connectY = new Matrix
(sizeX,sizeY);//for easy indexing, ONLY sizeX*(sizeY-1) is used for (int x=0;x
10.0f) connectY->set(x,y,false); else{ if (IsAccessible(to)){ from = from - Vec3(0,GridSize/2,0); to = to + Vec3(0,GridSize/2,0); hit = SkeletalApplication::GetSingleton()->RayCastTwoWay(from,to); if (!hit) hit = SkeletalApplication::GetSingleton()->RayCastTwoWay(from+Vec3(GridSize/2,0,0),to+Vec3(GridSize/2,0,0)); if (!hit) hit = SkeletalApplication::GetSingleton()->RayCastTwoWay(from-Vec3(GridSize/2,0,0),to-Vec3(GridSize/2,0,0)); connectY->set(x,y,!hit); }else connectY->set(x,y,false); } }else connectY->set(x,y,false); } } } float Map::heuristicDistance(int x,int y,Vec3 to){ return (GetPosition(x,y)-to).Length(); } bool Map::MakePath(Vec3 from, Vec3 to, Vec2Queue &wayPoints, DistanceMap &distanceMap, OpenListPriority &openList, CloseList &closeList){ assert(&wayPoints!=NULL); assert(&distanceMap!=NULL); //GetCell(from,&fromX,&fromY); //GetCell(to,&toX,&toY); distanceMap.Fill(InfiniteDistance); closeList.Clean(); openList.Clean(); int x,y; GetCell(from,x,y); openList.Push(heuristicDistance(x,y,to),Vec2(x,y)); distanceMap.set(x,y,0); return ComputeDistanceMapStep(to,distanceMap,openList,closeList); } #define COMPUTE_DISTANCE_MAP_STEPS 10 bool Map::ComputeDistanceMapStep(Vec3 to, DistanceMap &distanceMap, OpenListPriority &openList, CloseList &closeList){ assert(&distanceMap!=NULL); assert(&openList!=NULL); assert(&closeList!=NULL); Vec2 curPos,nei; Vec2Queue neighbors; int toX,toY; GetCell(to,toX,toY); int processedStep = 0; int curDistance; while (!openList.IsEmpty()) { curPos = openList.Pop(); if (closeList.IsClosed(curPos.x,curPos.y)) continue; curDistance = distanceMap.get(curPos.x,curPos.y); if (curPos.x==toX && curPos.y==toY){//found the solution return true; } if (curDistance>Map::MaximumSearchDistance){ return true; } GetNeighbors(curPos.x,curPos.y,neighbors); while (!neighbors.IsEmpty()){ nei = neighbors.Pop(); if (!closeList.IsClosed(nei.x,nei.y)){ distanceMap.set(nei.x,nei.y,curDistance+1); openList.Push(curDistance+1+ heuristicDistance(nei.x,nei.y,to),nei); } } closeList.Close(curPos.x,curPos.y); processedStep++; if (processedStep>COMPUTE_DISTANCE_MAP_STEPS) return false; } return true; } #define MAKE_PATH_STEPS 20 bool Map::MakePathStep(Vec3 from, Vec2 &curPos, DistanceMap &distanceMap, Vec2Queue &wayPoints){ //construct the waypoint here //wayPoints.Clean(); int fromX,fromY; GetCell(from,fromX,fromY); Vec2 nei; int curDistance; Vec2Queue neighbors; int processedStep = 0; while (curPos.x != fromX || curPos.y != fromY){ curDistance = distanceMap.get(curPos.x,curPos.y); wayPoints.PushBack(curPos); GetNeighbors(curPos.x,curPos.y,neighbors); while (!neighbors.IsEmpty()){ nei = neighbors.Pop(); if (distanceMap.get(nei.x,nei.y)==curDistance-1){ curPos = nei; break; } } processedStep++; if (processedStep>MAKE_PATH_STEPS) return false; } assert(curDistance<=1); return true; } bool Map::ComputeDistanceMapStep(Vec3 from, int range, DistanceMap &distanceMap, OpenListPriority &openList, CloseList &closeList, Vec2Queue &visitedPos, Matrix
&isCovered){ assert(&distanceMap!=NULL); assert(&openList!=NULL); assert(&closeList!=NULL); assert(&visitedPos!=NULL); assert(&isCovered!=NULL); //initialization //distanceMap.Fill(InfiniteDistance); //openList.Clean(); //int fromX,fromY; //GetCell(from,fromX,fromY); //openList.Push(0,Vec2(fromX,fromY)); //distanceMap.set(fromX,fromY,0); //closeList.Clean(); Vec2 curPos,nei; Vec2Queue neighbors; int curDistance; int processedStep = 0; while (!openList.IsEmpty()) { curPos = openList.Pop(); if (closeList.IsClosed(curPos.x,curPos.y)) continue; curDistance = distanceMap.get(curPos.x,curPos.y); if (curDistance>range){ return true; } GetNeighbors(curPos.x,curPos.y,neighbors); while (!neighbors.IsEmpty()){ nei = neighbors.Pop(); if (!closeList.IsClosed(nei.x,nei.y)){ distanceMap.set(nei.x,nei.y,curDistance+1); openList.Push(curDistance+1,nei); } } closeList.Close(curPos.x,curPos.y); visitedPos.Push(curPos); //isCovered.set(curPos.x,curPos.y,GameLevel::RayCast(Character::GetHero()->GetPos()+Vec3(0,0,Character::FireHeight), // GetPosition(curPos.x,curPos.y)+Vec3(0,0,Character::FireHeight),NULL,NULL)); isCovered.set(curPos.x,curPos.y,Soldier::IsCovered(Soldier::GetThreadPosition(), GetPosition(curPos.x,curPos.y))); processedStep++; if (processedStep>COMPUTE_DISTANCE_MAP_STEPS) return false; } return true; } void Map::InitPathFindingParameters(Vec2Queue* wayPoints, Vec3Queue* smoothWP, DistanceMap* distanceMap, Vec3* from, OpenListPriority* openList, CloseList* closeList, Vec2Queue* visitedPos,Matrix
* isCovered){ if (wayPoints!=NULL) wayPoints->Clean(); if (smoothWP!=NULL) smoothWP->Clean(); if (distanceMap!=NULL) distanceMap->Fill(InfiniteDistance); if (closeList!=NULL) closeList->Clean(); if (visitedPos!=NULL) visitedPos->Clean(); if (isCovered!=NULL) isCovered->Fill(false); if (openList!=NULL){ assert(from!=NULL); openList->Clean(); int x,y; GetCell(*from,x,y); openList->Push(0,Vec2(x,y)); distanceMap->set(x,y,0); } } void Map::GetNeighbors(int x,int y,Vec2Queue &ret){ assert(&ret!=NULL); ret.Clean(); if (x
0 && IsConnectX(x-1,y)) ret.Push(Vec2(x-1,y)); if (y
0 && IsConnectY(x,y-1)) ret.Push(Vec2(x,y-1)); } void Map::InitMakeSmoothPath(Vec3 from, Vec3 &refPos, Vec3 &prePos){ refPos = from + Vec3(0,0,Map::FootHeight); prePos = refPos; } #define MAKE_SMOOTH_PATH_STEPS 3 bool Map::MakeSmoothPathStep(Vec3 &refPos, Vec3 &prePos, Vec3 to, Vec2Queue &wayPoints, Vec3Queue &smoothWps){ Vec2 wp; //smoothWps.Clean(); bool hit; Vec3 norm,curPos; int processedStep = 0; while (!wayPoints.IsEmpty()){ wp = wayPoints.Pop(); curPos = GetPosition(wp.x,wp.y); curPos.z += Map::FootHeight; hit = SkeletalApplication::GetSingleton()->RayCast(refPos,curPos,NULL,NULL); if (!hit){ norm = refPos - curPos; norm = Vec3(norm.y,-norm.x,norm.z); norm = norm / norm.Length() * GridSize/2; hit = SkeletalApplication::GetSingleton()->RayCast(refPos+norm,curPos+norm,NULL,NULL); } if (!hit){ hit = SkeletalApplication::GetSingleton()->RayCast(refPos-norm,curPos-norm,NULL,NULL); } if (hit){ if (smoothWps.Size()>Soldier::WayPointQueueLength) return false; smoothWps.Push(prePos); DEBUG_OUTPUT("Push wp\n"); refPos = prePos; }else{ prePos = curPos; } processedStep++; if (processedStep>MAKE_SMOOTH_PATH_STEPS) return false; } smoothWps.Push(to); DEBUG_OUTPUT("Push wp\n"); return true; } Vec3 Map::GetPosition(int x,int y){ return Vec3(x*GridSize+GridSize/2,y*GridSize+GridSize/2,heightStatic->get(x,y)); } Map::~Map(){ SAFE_DELETE(heightStatic); SAFE_DELETE(connectX); SAFE_DELETE(connectY); assert(theMap!=NULL); theMap = NULL; } bool Map::IsAccessible(int posX,int posY){ return (heightStatic->get(posX,posY)<=Map::WalkableHeight && heightStatic->get(posX,posY)>Map::NegativeHeight); } bool Map::IsAccessible(Vec3 pos){ int x,y; GetCell(pos,x,y); return IsAccessible(x,y); } void Map::GetCell(Vec3 pos, int &x, int &y){ assert(&x!=NULL); assert(&y!=NULL); x = (int)floor((pos.x-GridSize/2)/GridSize); y =(int)floor((pos.y-GridSize/2)/GridSize); }
Map.cpp
网页地址
文件地址
上一页
27/45
下一页
下载
( 10 KB )
Comments
Total ratings:
0
Average rating:
无评论
of 10
Would you like to comment?
Join now
, or
Logon
if you are already a member.