fix terraintest and Terrain::getInstersectionPoint()

This commit is contained in:
tangziwen 2015-06-18 12:20:44 +08:00
parent 4187ea2241
commit 1989de7a04
3 changed files with 16 additions and 35 deletions

View File

@ -541,11 +541,6 @@ bool Terrain::getIntersectionPoint(const Ray & ray, Vec3 & intersectionPoint) co
{ {
lastRayPosition = rayPos; lastRayPosition = rayPos;
rayPos += rayStep; rayPos += rayStep;
if (normal.isZero())
{
intersectionPoint = Vec3(0, 0, 0);
return false;
}
height = getHeight(rayPos.x, rayPos.z); height = getHeight(rayPos.x, rayPos.z);
} }
@ -563,7 +558,14 @@ bool Terrain::getIntersectionPoint(const Ray & ray, Vec3 & intersectionPoint) co
} }
Vec3 collisionPoint = (startPosition + endPosition) * 0.5f; Vec3 collisionPoint = (startPosition + endPosition) * 0.5f;
intersectionPoint = collisionPoint; intersectionPoint = collisionPoint;
return true; Vec2 terrainSpacePos = convertToTerrainSpace(Vec2(intersectionPoint.x,intersectionPoint.z));
if (terrainSpacePos.x<0 || terrainSpacePos.x >_imageWidth || terrainSpacePos.y<0 || terrainSpacePos.y >_imageHeight)
{
return false;
}else
{
return true;
}
} }
void Terrain::setMaxDetailMapAmount(int max_value) void Terrain::setMaxDetailMapAmount(int max_value)
@ -571,7 +573,7 @@ void Terrain::setMaxDetailMapAmount(int max_value)
_maxDetailMapValue = max_value; _maxDetailMapValue = max_value;
} }
cocos2d::Vec2 Terrain::convertToTerrainSpace(Vec2 worldSpaceXZ) cocos2d::Vec2 Terrain::convertToTerrainSpace(Vec2 worldSpaceXZ) const
{ {
Vec2 pos(worldSpaceXZ.x,worldSpaceXZ.y); Vec2 pos(worldSpaceXZ.x,worldSpaceXZ.y);

View File

@ -362,7 +362,7 @@ public:
/** /**
* Convert a world Space position (X,Z) to terrain space position (X,Z) * Convert a world Space position (X,Z) to terrain space position (X,Z)
*/ */
Vec2 convertToTerrainSpace(Vec2 worldSpace); Vec2 convertToTerrainSpace(Vec2 worldSpace) const;
/** /**
* reset the heightmap data. * reset the heightmap data.

View File

@ -161,34 +161,13 @@ void TerrainWalkThru::onTouchesEnd(const std::vector<cocos2d::Touch*>& touches,
_camera->unproject(size, &farP, &farP); _camera->unproject(size, &farP, &farP);
Vec3 dir = farP - nearP; Vec3 dir = farP - nearP;
dir.normalize(); dir.normalize();
Vec3 rayStep = 15*dir; Vec3 collisionPoint(-999,-999,-999);
Vec3 rayPos = nearP; bool isInTerrain = _terrain->getIntersectionPoint(Ray(nearP, dir), collisionPoint);
Vec3 rayStartPosition = nearP; if (!isInTerrain)
Vec3 lastRayPosition =rayPos;
rayPos += rayStep;
// Linear search - Loop until find a point inside and outside the terrain Vector3
float height = _terrain->getHeight(rayPos.x,rayPos.z);
while (rayPos.y > height)
{ {
lastRayPosition = rayPos; _player->idle();
rayPos += rayStep; return;
height = _terrain->getHeight(rayPos.x,rayPos.z); }
}
Vec3 startPosition = lastRayPosition;
Vec3 endPosition = rayPos;
for (int i= 0; i< 32; i++)
{
// Binary search pass
Vec3 middlePoint = (startPosition + endPosition) * 0.5f;
if (middlePoint.y < height)
endPosition = middlePoint;
else
startPosition = middlePoint;
}
Vec3 collisionPoint = (startPosition + endPosition) * 0.5f;
dir = collisionPoint - _player->getPosition3D(); dir = collisionPoint - _player->getPosition3D();
dir.y = 0; dir.y = 0;
dir.normalize(); dir.normalize();