mirror of https://github.com/axmolengine/axmol.git
fix walking boundary of 3D test in lua
This commit is contained in:
parent
487634b5b7
commit
4f486528cc
|
@ -468,6 +468,67 @@ tolua_lerror:
|
||||||
return 0;
|
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)
|
static void extendTerrain(lua_State* L)
|
||||||
{
|
{
|
||||||
lua_pushstring(L, "cc.Terrain");
|
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, "create", lua_cocos2dx_3d_Terrain_create);
|
||||||
tolua_function(L, "getHeight", lua_cocos2dx_3d_Terrain_getHeight);
|
tolua_function(L, "getHeight", lua_cocos2dx_3d_Terrain_getHeight);
|
||||||
|
tolua_function(L, "getIntersectionPoint", lua_cocos2dx_3d_Terrain_getIntersectionPoint);
|
||||||
}
|
}
|
||||||
lua_pop(L, 1);
|
lua_pop(L, 1);
|
||||||
}
|
}
|
||||||
|
|
|
@ -236,6 +236,11 @@ function Camera3DTestDemo:onEnter()
|
||||||
local ndo = nearP.x * 0 + nearP.y * 1 + nearP.z * 0
|
local ndo = nearP.x * 0 + nearP.y * 1 + nearP.z * 0
|
||||||
dist= (0 - ndo) / ndd
|
dist= (0 - ndo) / ndd
|
||||||
local p = cc.vec3add(nearP, cc.vec3mul(dir, dist))
|
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
|
self._targetPos = p
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -181,34 +181,16 @@ function TerrainWalkThru:init()
|
||||||
local dir = cc.vec3sub(farP, nearP)
|
local dir = cc.vec3sub(farP, nearP)
|
||||||
dir = cc.vec3normalize(dir)
|
dir = cc.vec3normalize(dir)
|
||||||
|
|
||||||
local rayStep = cc.vec3mul(dir, 15)
|
local collisionPoint = cc.vec3(-999,-999,-999)
|
||||||
local rayPos = nearP
|
local ray = cc.Ray:new(nearP, dir)
|
||||||
local rayStartPosition = nearP
|
local isInTerrain = true;
|
||||||
local lastRayPosition = rayPos
|
isInTerrain, collisionPoint = self._terrain:getIntersectionPoint(ray, collisionPoint)
|
||||||
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
|
if( not isInTerrain) then
|
||||||
lastRayPosition = rayPos
|
self._player:idle()
|
||||||
rayPos = cc.vec3add(rayPos, rayStep)
|
return
|
||||||
height = self._terrain:getHeight(rayPos.x,rayPos.z)
|
|
||||||
end
|
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()
|
local playerPos = self._player:getPosition3D()
|
||||||
dir = cc.vec3sub(collisionPoint, playerPos)
|
dir = cc.vec3sub(collisionPoint, playerPos)
|
||||||
dir.y = 0
|
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._headingAxis = vec3_cross(dir, cc.vec3(0, 0, -1), self._player._headingAxis)
|
||||||
self._player._targetPos = collisionPoint
|
self._player._targetPos = collisionPoint
|
||||||
-- self._player:forward()
|
|
||||||
self._player._playerState = PLAER_STATE.FORWARD
|
self._player._playerState = PLAER_STATE.FORWARD
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue