Skip to content

fix walking boundary of 3D test in lua #20265

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions cocos/scripting/lua-bindings/manual/3d/lua_cocos2dx_3d_manual.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,67 @@ int lua_cocos2dx_3d_Terrain_getHeight(lua_State* L)
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<cocos2d::Ray>(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<cocos2d::Ray>(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");
Expand All @@ -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);
}
Expand Down
5 changes: 5 additions & 0 deletions tests/lua-tests/src/Camera3DTest/Camera3DTest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 8 additions & 27 deletions tests/lua-tests/src/Scene3DTest/Scene3DTest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)

while rayPos.y > height do
lastRayPosition = rayPos
rayPos = cc.vec3add(rayPos, rayStep)
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)

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
Expand All @@ -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
Expand Down