Skip to content

Commit 8f57137

Browse files
authored
Merge pull request cocos2d#20265 from Mee-gu/fix3DBoundary
fix walking boundary of 3D test in lua
2 parents 487634b + 4f48652 commit 8f57137

File tree

3 files changed

+75
-27
lines changed

3 files changed

+75
-27
lines changed

cocos/scripting/lua-bindings/manual/3d/lua_cocos2dx_3d_manual.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,67 @@ int lua_cocos2dx_3d_Terrain_getHeight(lua_State* L)
468468
return 0;
469469
}
470470

471+
int lua_cocos2dx_3d_Terrain_getIntersectionPoint(lua_State* tolua_S)
472+
{
473+
int argc = 0;
474+
cocos2d::Terrain* cobj = nullptr;
475+
bool ok = true;
476+
#if COCOS2D_DEBUG >= 1
477+
tolua_Error tolua_err;
478+
#endif
479+
480+
#if COCOS2D_DEBUG >= 1
481+
if (!tolua_isusertype(tolua_S, 1, "cc.Terrain", 0, &tolua_err)) goto tolua_lerror;
482+
#endif
483+
cobj = (cocos2d::Terrain*)tolua_tousertype(tolua_S, 1, 0);
484+
#if COCOS2D_DEBUG >= 1
485+
if (!cobj)
486+
{
487+
tolua_error(tolua_S, "invalid 'cobj' in function 'lua_cocos2dx_3d_Terrain_getIntersectionPoint'", nullptr);
488+
return 0;
489+
}
490+
#endif
491+
argc = lua_gettop(tolua_S) - 1;
492+
do {
493+
if (argc == 2) {
494+
cocos2d::Ray* arg0 = nullptr;
495+
ok &= luaval_to_object<cocos2d::Ray>(tolua_S, 2, "cc.Ray", &arg0, "cc.Terrain:getIntersectionPoint");
496+
497+
if (!ok) { break; }
498+
cocos2d::Vec3 arg1;
499+
ok &= luaval_to_vec3(tolua_S, 3, &arg1, "cc.Terrain:getIntersectionPoint");
500+
501+
if (!ok) { break; }
502+
bool ret = cobj->getIntersectionPoint(*arg0, arg1);
503+
tolua_pushboolean(tolua_S, (bool)ret);
504+
vec3_to_luaval(tolua_S, arg1);
505+
return 2;
506+
}
507+
} while (0);
508+
ok = true;
509+
do {
510+
if (argc == 1) {
511+
cocos2d::Ray* arg0;
512+
ok &= luaval_to_object<cocos2d::Ray>(tolua_S, 2, "cc.Ray", &arg0, "cc.Terrain:getIntersectionPoint");
513+
514+
if (!ok) { break; }
515+
cocos2d::Vec3 ret = cobj->getIntersectionPoint(*arg0);
516+
vec3_to_luaval(tolua_S, ret);
517+
return 1;
518+
}
519+
} while (0);
520+
ok = true;
521+
luaL_error(tolua_S, "%s has wrong number of arguments: %d, was expecting %d \n", "cc.Terrain:getIntersectionPoint", argc, 1);
522+
return 0;
523+
524+
#if COCOS2D_DEBUG >= 1
525+
tolua_lerror:
526+
tolua_error(tolua_S, "#ferror in function 'lua_cocos2dx_3d_Terrain_getIntersectionPoint'.", &tolua_err);
527+
#endif
528+
529+
return 0;
530+
}
531+
471532
static void extendTerrain(lua_State* L)
472533
{
473534
lua_pushstring(L, "cc.Terrain");
@@ -476,6 +537,7 @@ static void extendTerrain(lua_State* L)
476537
{
477538
tolua_function(L, "create", lua_cocos2dx_3d_Terrain_create);
478539
tolua_function(L, "getHeight", lua_cocos2dx_3d_Terrain_getHeight);
540+
tolua_function(L, "getIntersectionPoint", lua_cocos2dx_3d_Terrain_getIntersectionPoint);
479541
}
480542
lua_pop(L, 1);
481543
}

tests/lua-tests/src/Camera3DTest/Camera3DTest.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,11 @@ function Camera3DTestDemo:onEnter()
236236
local ndo = nearP.x * 0 + nearP.y * 1 + nearP.z * 0
237237
dist= (0 - ndo) / ndd
238238
local p = cc.vec3add(nearP, cc.vec3mul(dir, dist))
239+
240+
if p.x > 100 then p.x = 100 end
241+
if p.x < -100 then p.x = -100 end
242+
if p.z > 100 then p.z = 100 end
243+
if p.z < -100 then p.z = -100 end
239244
self._targetPos = p
240245
end
241246
end

tests/lua-tests/src/Scene3DTest/Scene3DTest.lua

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -181,34 +181,16 @@ function TerrainWalkThru:init()
181181
local dir = cc.vec3sub(farP, nearP)
182182
dir = cc.vec3normalize(dir)
183183

184-
local rayStep = cc.vec3mul(dir, 15)
185-
local rayPos = nearP
186-
local rayStartPosition = nearP
187-
local lastRayPosition = rayPos
188-
rayPos = cc.vec3add(rayPos, rayStep)
189-
-- Linear search - Loop until find a point inside and outside the terrain Vector3
190-
local height = self._terrain:getHeight(rayPos.x, rayPos.z)
191-
192-
while rayPos.y > height do
193-
lastRayPosition = rayPos
194-
rayPos = cc.vec3add(rayPos, rayStep)
195-
height = self._terrain:getHeight(rayPos.x,rayPos.z)
184+
local collisionPoint = cc.vec3(-999,-999,-999)
185+
local ray = cc.Ray:new(nearP, dir)
186+
local isInTerrain = true;
187+
isInTerrain, collisionPoint = self._terrain:getIntersectionPoint(ray, collisionPoint)
188+
189+
if( not isInTerrain) then
190+
self._player:idle()
191+
return
196192
end
197193

198-
local startPosition = lastRayPosition
199-
local endPosition = rayPos
200-
201-
for i = 1, 32 do
202-
-- Binary search pass
203-
local middlePoint = cc.vec3mul(cc.vec3add(startPosition, endPosition), 0.5)
204-
if (middlePoint.y < height) then
205-
endPosition = middlePoint
206-
else
207-
startPosition = middlePoint
208-
end
209-
end
210-
211-
local collisionPoint = cc.vec3mul(cc.vec3add(startPosition, endPosition), 0.5)
212194
local playerPos = self._player:getPosition3D()
213195
dir = cc.vec3sub(collisionPoint, playerPos)
214196
dir.y = 0
@@ -217,7 +199,6 @@ function TerrainWalkThru:init()
217199

218200
self._player._headingAxis = vec3_cross(dir, cc.vec3(0, 0, -1), self._player._headingAxis)
219201
self._player._targetPos = collisionPoint
220-
-- self._player:forward()
221202
self._player._playerState = PLAER_STATE.FORWARD
222203
end
223204
end

0 commit comments

Comments
 (0)