diff --git a/cocos/3d/CCTerrain.cpp b/cocos/3d/CCTerrain.cpp index aba46b3eb0..adbbbdcb3f 100644 --- a/cocos/3d/CCTerrain.cpp +++ b/cocos/3d/CCTerrain.cpp @@ -541,11 +541,6 @@ bool Terrain::getIntersectionPoint(const Ray & ray, Vec3 & intersectionPoint) co { lastRayPosition = rayPos; rayPos += rayStep; - if (normal.isZero()) - { - intersectionPoint = Vec3(0, 0, 0); - return false; - } 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; 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) @@ -571,7 +573,7 @@ void Terrain::setMaxDetailMapAmount(int max_value) _maxDetailMapValue = max_value; } -cocos2d::Vec2 Terrain::convertToTerrainSpace(Vec2 worldSpaceXZ) +cocos2d::Vec2 Terrain::convertToTerrainSpace(Vec2 worldSpaceXZ) const { Vec2 pos(worldSpaceXZ.x,worldSpaceXZ.y); diff --git a/cocos/3d/CCTerrain.h b/cocos/3d/CCTerrain.h index 5f678b3795..7d9512a876 100644 --- a/cocos/3d/CCTerrain.h +++ b/cocos/3d/CCTerrain.h @@ -362,7 +362,7 @@ public: /** * 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. diff --git a/tests/cpp-tests/Classes/TerrainTest/TerrainTest.cpp b/tests/cpp-tests/Classes/TerrainTest/TerrainTest.cpp index 6205638f3a..b5d7faaeb8 100644 --- a/tests/cpp-tests/Classes/TerrainTest/TerrainTest.cpp +++ b/tests/cpp-tests/Classes/TerrainTest/TerrainTest.cpp @@ -161,34 +161,13 @@ void TerrainWalkThru::onTouchesEnd(const std::vector& touches, _camera->unproject(size, &farP, &farP); Vec3 dir = farP - nearP; dir.normalize(); - Vec3 rayStep = 15*dir; - Vec3 rayPos = nearP; - Vec3 rayStartPosition = nearP; - 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) + Vec3 collisionPoint(-999,-999,-999); + bool isInTerrain = _terrain->getIntersectionPoint(Ray(nearP, dir), collisionPoint); + if (!isInTerrain) { - lastRayPosition = rayPos; - rayPos += rayStep; - 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; + _player->idle(); + return; + } dir = collisionPoint - _player->getPosition3D(); dir.y = 0; dir.normalize();