]> git.lizzy.rs Git - dragonfireclient.git/commitdiff
CSM: Bugfixes to camera:get_pos() and camera:get_fov()
authorsfan5 <sfan5@live.de>
Thu, 14 May 2020 19:16:45 +0000 (21:16 +0200)
committerGitHub <noreply@github.com>
Thu, 14 May 2020 19:16:45 +0000 (21:16 +0200)
closes #9857

clientmods/preview/init.lua
doc/client_lua_api.txt
src/client/camera.cpp
src/script/lua_api/l_camera.cpp
src/script/lua_api/l_env.cpp

index d2440369a7f97325a51be0b7bb7a0a4df9eb312a..089955d2f94c496ab09bf7d6a7edc4a9ceaf015b 100644 (file)
@@ -79,7 +79,7 @@ core.register_on_item_use(function(itemstack, pointed_thing)
                return false
        end
 
-       local pos = vector.add(core.localplayer:get_pos(), core.camera:get_offset())
+       local pos = core.camera:get_pos()
        local pos2 = vector.add(pos, vector.multiply(core.camera:get_look_dir(), 100))
 
        local rc = core.raycast(pos, pos2)
index 53442d308bb0a625132c489a99bbc286f9ca65e5..c9cd8ac93d0c12a154be9eaa518b8580cb2796d0 100644 (file)
@@ -967,7 +967,7 @@ Please do not try to access the reference until the camera is initialized, other
 * `get_camera_mode()`
     * Returns 0, 1, or 2 as described above
 * `get_fov()`
-    * Returns:
+    * Returns a table with X, Y, maximum and actual FOV in degrees:
 
 ```lua
      {
index 1a5253db426b61e59b1249445532eb056286be07..9b311171af7118111e202a5b0446d3befbeece08 100644 (file)
@@ -540,7 +540,7 @@ void Camera::update(LocalPlayer* player, f32 frametime, f32 busytime, f32 tool_r
        m_aspect = (f32) window_size.X / (f32) window_size.Y;
        m_fov_y = m_curr_fov_degrees * M_PI / 180.0;
        // Increase vertical FOV on lower aspect ratios (<16:10)
-       m_fov_y *= MYMAX(1.0, MYMIN(1.4, sqrt(16./10. / m_aspect)));
+       m_fov_y *= core::clamp(sqrt(16./10. / m_aspect), 1.0, 1.4);
        m_fov_x = 2 * atan(m_aspect * tan(0.5 * m_fov_y));
        m_cameranode->setAspectRatio(m_aspect);
        m_cameranode->setFOV(m_fov_y);
index 9c1470284645c5811e8172f411d6588716be26ec..bfa60be67d364ba46ed63d09887c6da8495047f0 100644 (file)
@@ -51,6 +51,7 @@ void LuaCamera::create(lua_State *L, Camera *m)
        lua_setfield(L, objectstable, "camera");
 }
 
+// set_camera_mode(self, mode)
 int LuaCamera::l_set_camera_mode(lua_State *L)
 {
        Camera *camera = getobject(L, 1);
@@ -67,17 +68,19 @@ int LuaCamera::l_set_camera_mode(lua_State *L)
        return 0;
 }
 
+// get_camera_mode(self)
 int LuaCamera::l_get_camera_mode(lua_State *L)
 {
        Camera *camera = getobject(L, 1);
        if (!camera)
                return 0;
 
-       lua_pushnumber(L, (int)camera->getCameraMode());
+       lua_pushinteger(L, (int)camera->getCameraMode());
 
        return 1;
 }
 
+// get_fov(self)
 int LuaCamera::l_get_fov(lua_State *L)
 {
        Camera *camera = getobject(L, 1);
@@ -85,9 +88,9 @@ int LuaCamera::l_get_fov(lua_State *L)
                return 0;
 
        lua_newtable(L);
-       lua_pushnumber(L, camera->getFovX() * core::DEGTORAD);
+       lua_pushnumber(L, camera->getFovX() * core::RADTODEG);
        lua_setfield(L, -2, "x");
-       lua_pushnumber(L, camera->getFovY() * core::DEGTORAD);
+       lua_pushnumber(L, camera->getFovY() * core::RADTODEG);
        lua_setfield(L, -2, "y");
        lua_pushnumber(L, camera->getCameraNode()->getFOV() * core::RADTODEG);
        lua_setfield(L, -2, "actual");
@@ -96,16 +99,18 @@ int LuaCamera::l_get_fov(lua_State *L)
        return 1;
 }
 
+// get_pos(self)
 int LuaCamera::l_get_pos(lua_State *L)
 {
        Camera *camera = getobject(L, 1);
        if (!camera)
                return 0;
 
-       push_v3f(L, camera->getPosition());
+       push_v3f(L, camera->getPosition() / BS);
        return 1;
 }
 
+// get_offset(self)
 int LuaCamera::l_get_offset(lua_State *L)
 {
        LocalPlayer *player = getClient(L)->getEnv().getLocalPlayer();
@@ -115,38 +120,40 @@ int LuaCamera::l_get_offset(lua_State *L)
        return 1;
 }
 
+// get_look_dir(self)
 int LuaCamera::l_get_look_dir(lua_State *L)
 {
-       LocalPlayer *player = getClient(L)->getEnv().getLocalPlayer();
-       sanity_check(player);
-
-       float pitch = -1.0 * player->getPitch() * core::DEGTORAD;
-       float yaw = (player->getYaw() + 90.) * core::DEGTORAD;
-       v3f v(std::cos(pitch) * std::cos(yaw), std::sin(pitch),
-                       std::cos(pitch) * std::sin(yaw));
+       Camera *camera = getobject(L, 1);
+       if (!camera)
+               return 0;
 
-       push_v3f(L, v);
+       push_v3f(L, camera->getDirection());
        return 1;
 }
 
+// get_look_horizontal(self)
+// FIXME: wouldn't localplayer be a better place for this?
 int LuaCamera::l_get_look_horizontal(lua_State *L)
 {
        LocalPlayer *player = getClient(L)->getEnv().getLocalPlayer();
        sanity_check(player);
 
-       lua_pushnumber(L, (player->getYaw() + 90.) * core::DEGTORAD);
+       lua_pushnumber(L, (player->getYaw() + 90.f) * core::DEGTORAD);
        return 1;
 }
 
+// get_look_vertical(self)
+// FIXME: wouldn't localplayer be a better place for this?
 int LuaCamera::l_get_look_vertical(lua_State *L)
 {
        LocalPlayer *player = getClient(L)->getEnv().getLocalPlayer();
        sanity_check(player);
 
-       lua_pushnumber(L, -1.0 * player->getPitch() * core::DEGTORAD);
+       lua_pushnumber(L, -1.0f * player->getPitch() * core::DEGTORAD);
        return 1;
 }
 
+// get_aspect_ratio(self)
 int LuaCamera::l_get_aspect_ratio(lua_State *L)
 {
        Camera *camera = getobject(L, 1);
@@ -215,13 +222,19 @@ void LuaCamera::Register(lua_State *L)
        lua_pop(L, 1);
 }
 
+// clang-format off
 const char LuaCamera::className[] = "Camera";
-const luaL_Reg LuaCamera::methods[] = {luamethod(LuaCamera, set_camera_mode),
-               luamethod(LuaCamera, get_camera_mode), luamethod(LuaCamera, get_fov),
-               luamethod(LuaCamera, get_pos), luamethod(LuaCamera, get_offset),
-               luamethod(LuaCamera, get_look_dir),
-               luamethod(LuaCamera, get_look_vertical),
-               luamethod(LuaCamera, get_look_horizontal),
-               luamethod(LuaCamera, get_aspect_ratio),
-
-               {0, 0}};
+const luaL_Reg LuaCamera::methods[] = {
+       luamethod(LuaCamera, set_camera_mode),
+       luamethod(LuaCamera, get_camera_mode),
+       luamethod(LuaCamera, get_fov),
+       luamethod(LuaCamera, get_pos),
+       luamethod(LuaCamera, get_offset),
+       luamethod(LuaCamera, get_look_dir),
+       luamethod(LuaCamera, get_look_vertical),
+       luamethod(LuaCamera, get_look_horizontal),
+       luamethod(LuaCamera, get_aspect_ratio),
+
+       {0, 0}
+};
+// clang-format on
index cabca124d35bc70f31107d1e3af19458b6566aa8..b8a8a5ce1a390b29f442128cfc7750ca045bd779 100644 (file)
@@ -780,8 +780,8 @@ int ModApiEnvMod::l_find_node_near(lua_State *L)
 
 #ifndef SERVER
        // Client API limitations
-       if (getClient(L))
-               radius = getClient(L)->CSMClampRadius(pos, radius);
+       if (Client *client = getClient(L))
+               radius = client->CSMClampRadius(pos, radius);
 #endif
 
        for (int d = start_radius; d <= radius; d++) {
@@ -811,9 +811,9 @@ int ModApiEnvMod::l_find_nodes_in_area(lua_State *L)
        const NodeDefManager *ndef = env->getGameDef()->ndef();
 
 #ifndef SERVER
-       if (getClient(L)) {
-               minp = getClient(L)->CSMClampPos(minp);
-               maxp = getClient(L)->CSMClampPos(maxp);
+       if (Client *client = getClient(L)) {
+               minp = client->CSMClampPos(minp);
+               maxp = client->CSMClampPos(maxp);
        }
 #endif
 
@@ -887,9 +887,9 @@ int ModApiEnvMod::l_find_nodes_in_area_under_air(lua_State *L)
        const NodeDefManager *ndef = env->getGameDef()->ndef();
 
 #ifndef SERVER
-       if (getClient(L)) {
-               minp = getClient(L)->CSMClampPos(minp);
-               maxp = getClient(L)->CSMClampPos(maxp);
+       if (Client *client = getClient(L)) {
+               minp = client->CSMClampPos(minp);
+               maxp = client->CSMClampPos(maxp);
        }
 #endif