Skip to content

Commit 36d35f2

Browse files
authoredMay 14, 2020
CSM: Bugfixes to camera:get_pos() and camera:get_fov()
closes #9857
1 parent 6ef7ad0 commit 36d35f2

File tree

5 files changed

+47
-34
lines changed

5 files changed

+47
-34
lines changed
 

‎clientmods/preview/init.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ core.register_on_item_use(function(itemstack, pointed_thing)
7979
return false
8080
end
8181

82-
local pos = vector.add(core.localplayer:get_pos(), core.camera:get_offset())
82+
local pos = core.camera:get_pos()
8383
local pos2 = vector.add(pos, vector.multiply(core.camera:get_look_dir(), 100))
8484

8585
local rc = core.raycast(pos, pos2)

‎doc/client_lua_api.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -967,7 +967,7 @@ Please do not try to access the reference until the camera is initialized, other
967967
* `get_camera_mode()`
968968
* Returns 0, 1, or 2 as described above
969969
* `get_fov()`
970-
* Returns:
970+
* Returns a table with X, Y, maximum and actual FOV in degrees:
971971

972972
```lua
973973
{

‎src/client/camera.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ void Camera::update(LocalPlayer* player, f32 frametime, f32 busytime, f32 tool_r
540540
m_aspect = (f32) window_size.X / (f32) window_size.Y;
541541
m_fov_y = m_curr_fov_degrees * M_PI / 180.0;
542542
// Increase vertical FOV on lower aspect ratios (<16:10)
543-
m_fov_y *= MYMAX(1.0, MYMIN(1.4, sqrt(16./10. / m_aspect)));
543+
m_fov_y *= core::clamp(sqrt(16./10. / m_aspect), 1.0, 1.4);
544544
m_fov_x = 2 * atan(m_aspect * tan(0.5 * m_fov_y));
545545
m_cameranode->setAspectRatio(m_aspect);
546546
m_cameranode->setFOV(m_fov_y);

‎src/script/lua_api/l_camera.cpp

+36-23
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ void LuaCamera::create(lua_State *L, Camera *m)
5151
lua_setfield(L, objectstable, "camera");
5252
}
5353

54+
// set_camera_mode(self, mode)
5455
int LuaCamera::l_set_camera_mode(lua_State *L)
5556
{
5657
Camera *camera = getobject(L, 1);
@@ -67,27 +68,29 @@ int LuaCamera::l_set_camera_mode(lua_State *L)
6768
return 0;
6869
}
6970

71+
// get_camera_mode(self)
7072
int LuaCamera::l_get_camera_mode(lua_State *L)
7173
{
7274
Camera *camera = getobject(L, 1);
7375
if (!camera)
7476
return 0;
7577

76-
lua_pushnumber(L, (int)camera->getCameraMode());
78+
lua_pushinteger(L, (int)camera->getCameraMode());
7779

7880
return 1;
7981
}
8082

83+
// get_fov(self)
8184
int LuaCamera::l_get_fov(lua_State *L)
8285
{
8386
Camera *camera = getobject(L, 1);
8487
if (!camera)
8588
return 0;
8689

8790
lua_newtable(L);
88-
lua_pushnumber(L, camera->getFovX() * core::DEGTORAD);
91+
lua_pushnumber(L, camera->getFovX() * core::RADTODEG);
8992
lua_setfield(L, -2, "x");
90-
lua_pushnumber(L, camera->getFovY() * core::DEGTORAD);
93+
lua_pushnumber(L, camera->getFovY() * core::RADTODEG);
9194
lua_setfield(L, -2, "y");
9295
lua_pushnumber(L, camera->getCameraNode()->getFOV() * core::RADTODEG);
9396
lua_setfield(L, -2, "actual");
@@ -96,16 +99,18 @@ int LuaCamera::l_get_fov(lua_State *L)
9699
return 1;
97100
}
98101

102+
// get_pos(self)
99103
int LuaCamera::l_get_pos(lua_State *L)
100104
{
101105
Camera *camera = getobject(L, 1);
102106
if (!camera)
103107
return 0;
104108

105-
push_v3f(L, camera->getPosition());
109+
push_v3f(L, camera->getPosition() / BS);
106110
return 1;
107111
}
108112

113+
// get_offset(self)
109114
int LuaCamera::l_get_offset(lua_State *L)
110115
{
111116
LocalPlayer *player = getClient(L)->getEnv().getLocalPlayer();
@@ -115,38 +120,40 @@ int LuaCamera::l_get_offset(lua_State *L)
115120
return 1;
116121
}
117122

123+
// get_look_dir(self)
118124
int LuaCamera::l_get_look_dir(lua_State *L)
119125
{
120-
LocalPlayer *player = getClient(L)->getEnv().getLocalPlayer();
121-
sanity_check(player);
122-
123-
float pitch = -1.0 * player->getPitch() * core::DEGTORAD;
124-
float yaw = (player->getYaw() + 90.) * core::DEGTORAD;
125-
v3f v(std::cos(pitch) * std::cos(yaw), std::sin(pitch),
126-
std::cos(pitch) * std::sin(yaw));
126+
Camera *camera = getobject(L, 1);
127+
if (!camera)
128+
return 0;
127129

128-
push_v3f(L, v);
130+
push_v3f(L, camera->getDirection());
129131
return 1;
130132
}
131133

134+
// get_look_horizontal(self)
135+
// FIXME: wouldn't localplayer be a better place for this?
132136
int LuaCamera::l_get_look_horizontal(lua_State *L)
133137
{
134138
LocalPlayer *player = getClient(L)->getEnv().getLocalPlayer();
135139
sanity_check(player);
136140

137-
lua_pushnumber(L, (player->getYaw() + 90.) * core::DEGTORAD);
141+
lua_pushnumber(L, (player->getYaw() + 90.f) * core::DEGTORAD);
138142
return 1;
139143
}
140144

145+
// get_look_vertical(self)
146+
// FIXME: wouldn't localplayer be a better place for this?
141147
int LuaCamera::l_get_look_vertical(lua_State *L)
142148
{
143149
LocalPlayer *player = getClient(L)->getEnv().getLocalPlayer();
144150
sanity_check(player);
145151

146-
lua_pushnumber(L, -1.0 * player->getPitch() * core::DEGTORAD);
152+
lua_pushnumber(L, -1.0f * player->getPitch() * core::DEGTORAD);
147153
return 1;
148154
}
149155

156+
// get_aspect_ratio(self)
150157
int LuaCamera::l_get_aspect_ratio(lua_State *L)
151158
{
152159
Camera *camera = getobject(L, 1);
@@ -215,13 +222,19 @@ void LuaCamera::Register(lua_State *L)
215222
lua_pop(L, 1);
216223
}
217224

225+
// clang-format off
218226
const char LuaCamera::className[] = "Camera";
219-
const luaL_Reg LuaCamera::methods[] = {luamethod(LuaCamera, set_camera_mode),
220-
luamethod(LuaCamera, get_camera_mode), luamethod(LuaCamera, get_fov),
221-
luamethod(LuaCamera, get_pos), luamethod(LuaCamera, get_offset),
222-
luamethod(LuaCamera, get_look_dir),
223-
luamethod(LuaCamera, get_look_vertical),
224-
luamethod(LuaCamera, get_look_horizontal),
225-
luamethod(LuaCamera, get_aspect_ratio),
226-
227-
{0, 0}};
227+
const luaL_Reg LuaCamera::methods[] = {
228+
luamethod(LuaCamera, set_camera_mode),
229+
luamethod(LuaCamera, get_camera_mode),
230+
luamethod(LuaCamera, get_fov),
231+
luamethod(LuaCamera, get_pos),
232+
luamethod(LuaCamera, get_offset),
233+
luamethod(LuaCamera, get_look_dir),
234+
luamethod(LuaCamera, get_look_vertical),
235+
luamethod(LuaCamera, get_look_horizontal),
236+
luamethod(LuaCamera, get_aspect_ratio),
237+
238+
{0, 0}
239+
};
240+
// clang-format on

‎src/script/lua_api/l_env.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -780,8 +780,8 @@ int ModApiEnvMod::l_find_node_near(lua_State *L)
780780

781781
#ifndef SERVER
782782
// Client API limitations
783-
if (getClient(L))
784-
radius = getClient(L)->CSMClampRadius(pos, radius);
783+
if (Client *client = getClient(L))
784+
radius = client->CSMClampRadius(pos, radius);
785785
#endif
786786

787787
for (int d = start_radius; d <= radius; d++) {
@@ -811,9 +811,9 @@ int ModApiEnvMod::l_find_nodes_in_area(lua_State *L)
811811
const NodeDefManager *ndef = env->getGameDef()->ndef();
812812

813813
#ifndef SERVER
814-
if (getClient(L)) {
815-
minp = getClient(L)->CSMClampPos(minp);
816-
maxp = getClient(L)->CSMClampPos(maxp);
814+
if (Client *client = getClient(L)) {
815+
minp = client->CSMClampPos(minp);
816+
maxp = client->CSMClampPos(maxp);
817817
}
818818
#endif
819819

@@ -887,9 +887,9 @@ int ModApiEnvMod::l_find_nodes_in_area_under_air(lua_State *L)
887887
const NodeDefManager *ndef = env->getGameDef()->ndef();
888888

889889
#ifndef SERVER
890-
if (getClient(L)) {
891-
minp = getClient(L)->CSMClampPos(minp);
892-
maxp = getClient(L)->CSMClampPos(maxp);
890+
if (Client *client = getClient(L)) {
891+
minp = client->CSMClampPos(minp);
892+
maxp = client->CSMClampPos(maxp);
893893
}
894894
#endif
895895

0 commit comments

Comments
 (0)
Please sign in to comment.