Skip to content

Commit 45999b7

Browse files
authoredApr 16, 2020
Camera: Fix shooting line offsets (#9681)
Removes duplicated offset calculations from Game and use whatever the Camera class returns. This keeps the eye position nicely in sync, and gets rid of duplicated code.
1 parent 5cbe843 commit 45999b7

File tree

4 files changed

+31
-24
lines changed

4 files changed

+31
-24
lines changed
 

‎src/client/camera.cpp

+15-11
Original file line numberDiff line numberDiff line change
@@ -333,17 +333,21 @@ void Camera::update(LocalPlayer* player, f32 frametime, f32 busytime, f32 tool_r
333333
fall_bobbing *= m_cache_fall_bobbing_amount;
334334
}
335335

336-
// Calculate players eye offset for different camera modes
337-
v3f PlayerEyeOffset = player->getEyeOffset();
338-
if (m_camera_mode == CAMERA_MODE_FIRST)
339-
PlayerEyeOffset += player->eye_offset_first;
340-
else
341-
PlayerEyeOffset += player->eye_offset_third;
342-
343-
// Set head node transformation
344-
m_headnode->setPosition(PlayerEyeOffset+v3f(0,cameratilt*-player->hurt_tilt_strength+fall_bobbing,0));
345-
m_headnode->setRotation(v3f(player->getPitch(), 0, cameratilt*player->hurt_tilt_strength));
346-
m_headnode->updateAbsolutePosition();
336+
// Calculate and translate the head SceneNode offsets
337+
{
338+
v3f eye_offset = player->getEyeOffset();
339+
if (m_camera_mode == CAMERA_MODE_FIRST)
340+
eye_offset += player->eye_offset_first;
341+
else
342+
eye_offset += player->eye_offset_third;
343+
344+
// Set head node transformation
345+
eye_offset.Y += cameratilt * -player->hurt_tilt_strength + fall_bobbing;
346+
m_headnode->setPosition(eye_offset);
347+
m_headnode->setRotation(v3f(player->getPitch(), 0,
348+
cameratilt * player->hurt_tilt_strength));
349+
m_headnode->updateAbsolutePosition();
350+
}
347351

348352
// Compute relative camera position and target
349353
v3f rel_cam_pos = v3f(0,0,0);

‎src/client/camera.h

+6
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ class Camera
7575
return m_camera_position;
7676
}
7777

78+
// Returns the absolute position of the head SceneNode in the world
79+
inline v3f getHeadPosition() const
80+
{
81+
return m_headnode->getAbsolutePosition();
82+
}
83+
7884
// Get the camera direction (in absolute camera coordinates).
7985
// This has view bobbing applied.
8086
inline v3f getDirection() const

‎src/client/game.cpp

+7-13
Original file line numberDiff line numberDiff line change
@@ -3029,16 +3029,9 @@ void Game::processPlayerInteraction(f32 dtime, bool show_hud, bool show_debug)
30293029
{
30303030
LocalPlayer *player = client->getEnv().getLocalPlayer();
30313031

3032-
v3f player_position = player->getPosition();
3033-
v3f player_eye_position = player->getEyePosition();
3034-
v3f camera_position = camera->getPosition();
3035-
v3f camera_direction = camera->getDirection();
3036-
v3s16 camera_offset = camera->getOffset();
3037-
3038-
if (camera->getCameraMode() == CAMERA_MODE_FIRST)
3039-
player_eye_position += player->eye_offset_first;
3040-
else
3041-
player_eye_position += player->eye_offset_third;
3032+
const v3f head_position = camera->getHeadPosition();
3033+
const v3f camera_direction = camera->getDirection();
3034+
const v3s16 camera_offset = camera->getOffset();
30423035

30433036
/*
30443037
Calculate what block is the crosshair pointing to
@@ -3053,11 +3046,11 @@ void Game::processPlayerInteraction(f32 dtime, bool show_hud, bool show_debug)
30533046
core::line3d<f32> shootline;
30543047

30553048
if (camera->getCameraMode() != CAMERA_MODE_THIRD_FRONT) {
3056-
shootline = core::line3d<f32>(player_eye_position,
3057-
player_eye_position + camera_direction * BS * d);
3049+
shootline = core::line3d<f32>(head_position,
3050+
head_position + camera_direction * BS * d);
30583051
} else {
30593052
// prevent player pointing anything in front-view
3060-
shootline = core::line3d<f32>(camera_position, camera_position);
3053+
shootline = core::line3d<f32>(head_position, head_position);
30613054
}
30623055

30633056
#ifdef HAVE_TOUCHSCREENGUI
@@ -3145,6 +3138,7 @@ void Game::processPlayerInteraction(f32 dtime, bool show_hud, bool show_debug)
31453138
} else if (pointed.type == POINTEDTHING_NODE) {
31463139
handlePointingAtNode(pointed, selected_item, hand_item, dtime);
31473140
} else if (pointed.type == POINTEDTHING_OBJECT) {
3141+
v3f player_position = player->getPosition();
31483142
handlePointingAtObject(pointed, tool_item, player_position, show_debug);
31493143
} else if (input->getLeftState()) {
31503144
// When button is held down in air, show continuous animation

‎src/client/localplayer.h

+3
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ class LocalPlayer : public Player
135135
}
136136

137137
v3f getPosition() const { return m_position; }
138+
139+
// Non-transformed eye offset getters
140+
// For accurate positions, use the Camera functions
138141
v3f getEyePosition() const { return m_position + getEyeOffset(); }
139142
v3f getEyeOffset() const;
140143
void setEyeHeight(float eye_height) { m_eye_height = eye_height; }

0 commit comments

Comments
 (0)