From 4f486528cc9d43ad37f808adc070d81d1c8792aa Mon Sep 17 00:00:00 2001 From: coulsonwang Date: Tue, 29 Oct 2019 19:24:35 +0800 Subject: [PATCH] fix walking boundary of 3D test in lua --- .../manual/3d/lua_cocos2dx_3d_manual.cpp | 62 +++++++++++++++++++ .../src/Camera3DTest/Camera3DTest.lua | 5 ++ .../lua-tests/src/Scene3DTest/Scene3DTest.lua | 33 +++------- 3 files changed, 74 insertions(+), 26 deletions(-) diff --git a/cocos/scripting/lua-bindings/manual/3d/lua_cocos2dx_3d_manual.cpp b/cocos/scripting/lua-bindings/manual/3d/lua_cocos2dx_3d_manual.cpp index b73eee4ea3..bdee328444 100644 --- a/cocos/scripting/lua-bindings/manual/3d/lua_cocos2dx_3d_manual.cpp +++ b/cocos/scripting/lua-bindings/manual/3d/lua_cocos2dx_3d_manual.cpp @@ -468,6 +468,67 @@ tolua_lerror: return 0; } +int lua_cocos2dx_3d_Terrain_getIntersectionPoint(lua_State* tolua_S) +{ + int argc = 0; + cocos2d::Terrain* cobj = nullptr; + bool ok = true; +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; +#endif + +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertype(tolua_S, 1, "cc.Terrain", 0, &tolua_err)) goto tolua_lerror; +#endif + cobj = (cocos2d::Terrain*)tolua_tousertype(tolua_S, 1, 0); +#if COCOS2D_DEBUG >= 1 + if (!cobj) + { + tolua_error(tolua_S, "invalid 'cobj' in function 'lua_cocos2dx_3d_Terrain_getIntersectionPoint'", nullptr); + return 0; + } +#endif + argc = lua_gettop(tolua_S) - 1; + do { + if (argc == 2) { + cocos2d::Ray* arg0 = nullptr; + ok &= luaval_to_object(tolua_S, 2, "cc.Ray", &arg0, "cc.Terrain:getIntersectionPoint"); + + if (!ok) { break; } + cocos2d::Vec3 arg1; + ok &= luaval_to_vec3(tolua_S, 3, &arg1, "cc.Terrain:getIntersectionPoint"); + + if (!ok) { break; } + bool ret = cobj->getIntersectionPoint(*arg0, arg1); + tolua_pushboolean(tolua_S, (bool)ret); + vec3_to_luaval(tolua_S, arg1); + return 2; + } + } while (0); + ok = true; + do { + if (argc == 1) { + cocos2d::Ray* arg0; + ok &= luaval_to_object(tolua_S, 2, "cc.Ray", &arg0, "cc.Terrain:getIntersectionPoint"); + + if (!ok) { break; } + cocos2d::Vec3 ret = cobj->getIntersectionPoint(*arg0); + vec3_to_luaval(tolua_S, ret); + return 1; + } + } while (0); + ok = true; + luaL_error(tolua_S, "%s has wrong number of arguments: %d, was expecting %d \n", "cc.Terrain:getIntersectionPoint", argc, 1); + return 0; + +#if COCOS2D_DEBUG >= 1 + tolua_lerror: + tolua_error(tolua_S, "#ferror in function 'lua_cocos2dx_3d_Terrain_getIntersectionPoint'.", &tolua_err); +#endif + + return 0; +} + static void extendTerrain(lua_State* L) { lua_pushstring(L, "cc.Terrain"); @@ -476,6 +537,7 @@ static void extendTerrain(lua_State* L) { tolua_function(L, "create", lua_cocos2dx_3d_Terrain_create); tolua_function(L, "getHeight", lua_cocos2dx_3d_Terrain_getHeight); + tolua_function(L, "getIntersectionPoint", lua_cocos2dx_3d_Terrain_getIntersectionPoint); } lua_pop(L, 1); } diff --git a/tests/lua-tests/src/Camera3DTest/Camera3DTest.lua b/tests/lua-tests/src/Camera3DTest/Camera3DTest.lua index 9a304c195b..3a493f32a0 100644 --- a/tests/lua-tests/src/Camera3DTest/Camera3DTest.lua +++ b/tests/lua-tests/src/Camera3DTest/Camera3DTest.lua @@ -236,6 +236,11 @@ function Camera3DTestDemo:onEnter() local ndo = nearP.x * 0 + nearP.y * 1 + nearP.z * 0 dist= (0 - ndo) / ndd local p = cc.vec3add(nearP, cc.vec3mul(dir, dist)) + + if p.x > 100 then p.x = 100 end + if p.x < -100 then p.x = -100 end + if p.z > 100 then p.z = 100 end + if p.z < -100 then p.z = -100 end self._targetPos = p end end diff --git a/tests/lua-tests/src/Scene3DTest/Scene3DTest.lua b/tests/lua-tests/src/Scene3DTest/Scene3DTest.lua index 02259a63a0..d29a295d61 100644 --- a/tests/lua-tests/src/Scene3DTest/Scene3DTest.lua +++ b/tests/lua-tests/src/Scene3DTest/Scene3DTest.lua @@ -181,34 +181,16 @@ function TerrainWalkThru:init() local dir = cc.vec3sub(farP, nearP) dir = cc.vec3normalize(dir) - local rayStep = cc.vec3mul(dir, 15) - local rayPos = nearP - local rayStartPosition = nearP - local lastRayPosition = rayPos - rayPos = cc.vec3add(rayPos, rayStep) - -- Linear search - Loop until find a point inside and outside the terrain Vector3 - local height = self._terrain:getHeight(rayPos.x, rayPos.z) + local collisionPoint = cc.vec3(-999,-999,-999) + local ray = cc.Ray:new(nearP, dir) + local isInTerrain = true; + isInTerrain, collisionPoint = self._terrain:getIntersectionPoint(ray, collisionPoint) - while rayPos.y > height do - lastRayPosition = rayPos - rayPos = cc.vec3add(rayPos, rayStep) - height = self._terrain:getHeight(rayPos.x,rayPos.z) + if( not isInTerrain) then + self._player:idle() + return end - local startPosition = lastRayPosition - local endPosition = rayPos - - for i = 1, 32 do - -- Binary search pass - local middlePoint = cc.vec3mul(cc.vec3add(startPosition, endPosition), 0.5) - if (middlePoint.y < height) then - endPosition = middlePoint - else - startPosition = middlePoint - end - end - - local collisionPoint = cc.vec3mul(cc.vec3add(startPosition, endPosition), 0.5) local playerPos = self._player:getPosition3D() dir = cc.vec3sub(collisionPoint, playerPos) dir.y = 0 @@ -217,7 +199,6 @@ function TerrainWalkThru:init() self._player._headingAxis = vec3_cross(dir, cc.vec3(0, 0, -1), self._player._headingAxis) self._player._targetPos = collisionPoint - -- self._player:forward() self._player._playerState = PLAER_STATE.FORWARD end end