|
27 | 27 | #include "constants.hpp" |
28 | 28 |
|
29 | 29 | #include "input/raw/keyboard.hpp" |
| 30 | +#include "input/raw/mouse.hpp" |
30 | 31 | #include "resources/time.hpp" |
31 | 32 |
|
32 | 33 | static std::expected<void, std::string> startup(/* clang-format off */ |
@@ -84,6 +85,7 @@ static std::expected<void, std::string> startup(/* clang-format off */ |
84 | 85 | static std::expected<void, std::string> update(/* clang-format off */ |
85 | 86 | std::shared_ptr<entt::registry> registry, |
86 | 87 | std::shared_ptr<Renderer> renderer, |
| 88 | + std::shared_ptr<scenes::nfs::components::CameraState> cameraState, |
87 | 89 | resources::Time& time |
88 | 90 | ) { /* clang-format on */ |
89 | 91 |
|
@@ -178,28 +180,86 @@ static std::expected<void, std::string> update(/* clang-format off */ |
178 | 180 | angularVelocity.value.z = targetAngularVelocity; |
179 | 181 | } |
180 | 182 |
|
181 | | - // Third-person camera system - follow the car from behind |
182 | | - const float cameraDistance = 10.0f; // Distance behind the car |
183 | | - const float cameraHeight = 3.0f; // Height above the car |
| 183 | + // GTA 5-style camera system |
| 184 | + auto carView = registry->view<components::Position, components::Rotation>(); |
| 185 | + for (auto carEntity : carView) { |
| 186 | + auto& carPosition = carView.get<components::Position>(carEntity); |
| 187 | + auto& carRotation = carView.get<components::Rotation>(carEntity); |
184 | 188 |
|
185 | | - // Calculate camera position: behind the car + height offset |
186 | | - glm::vec3 cameraOffset = forward * cameraDistance + glm::vec3(0.0f, 0.0f, cameraHeight); |
187 | | - glm::vec3 cameraPos = position.value + cameraOffset; |
| 189 | + // Get car forward direction |
| 190 | + glm::vec3 carForward = carRotation.value * glm::vec3(0.0f, 1.0f, 0.0f); |
188 | 191 |
|
189 | | - // Camera looks at the car (slightly ahead of it) |
190 | | - glm::vec3 lookTarget = position.value - forward * 2.0f; |
191 | | - glm::vec3 cameraDir = glm::normalize(lookTarget - cameraPos); |
| 192 | + // Handle mouse input for camera rotation |
| 193 | + glm::vec2 mouseDelta = input::Mouse::getPositionDelta(); |
| 194 | + bool hasMouseInput = glm::length(mouseDelta) > 0.01f; |
192 | 195 |
|
193 | | - // Update renderer camera |
194 | | - renderer->setCameraPos(cameraPos); |
195 | | - renderer->setCameraDir(cameraDir); |
| 196 | + if (hasMouseInput) { |
| 197 | + cameraState->yaw -= mouseDelta.x * cameraState->mouseSensitivity; |
| 198 | + cameraState->pitch += mouseDelta.y * cameraState->mouseSensitivity; // Fixed inversion |
| 199 | + |
| 200 | + // Clamp pitch to prevent camera flipping |
| 201 | + cameraState->pitch = std::clamp(cameraState->pitch, -1.4f, 0.5f); // About -80° to +30° |
| 202 | + |
| 203 | + cameraState->isUserControlling = true; |
| 204 | + cameraState->timeSinceLastInput = 0.0f; |
| 205 | + } else { |
| 206 | + cameraState->timeSinceLastInput += time.deltaTime; |
| 207 | + |
| 208 | + // Start auto-centering after delay |
| 209 | + if (cameraState->timeSinceLastInput > cameraState->autoReturnDelay) { |
| 210 | + cameraState->isUserControlling = false; |
| 211 | + |
| 212 | + // Calculate target yaw (behind the car) |
| 213 | + glm::vec3 carForwardXY = glm::normalize(glm::vec3(carForward.x, carForward.y, 0.0f)); |
| 214 | + cameraState->targetYaw = atan2(carForwardXY.y, carForwardXY.x) + constants::PI; // Behind the car |
| 215 | + |
| 216 | + // Smoothly interpolate back to target position |
| 217 | + float returnSpeed = cameraState->autoReturnSpeed * time.deltaTime; |
| 218 | + |
| 219 | + // Handle yaw wrapping (shortest rotation path) |
| 220 | + float yawDiff = cameraState->targetYaw - cameraState->yaw; |
| 221 | + while (yawDiff > glm::pi<float>()) |
| 222 | + yawDiff -= 2.0f * glm::pi<float>(); |
| 223 | + while (yawDiff < -glm::pi<float>()) |
| 224 | + yawDiff += 2.0f * glm::pi<float>(); |
| 225 | + |
| 226 | + cameraState->yaw += yawDiff * returnSpeed; |
| 227 | + cameraState->pitch += (cameraState->targetPitch - cameraState->pitch) * returnSpeed; |
| 228 | + } |
| 229 | + } |
| 230 | + |
| 231 | + // Calculate camera position based on yaw, pitch, and distance |
| 232 | + float cosYaw = cos(cameraState->yaw); |
| 233 | + float sinYaw = sin(cameraState->yaw); |
| 234 | + float cosPitch = cos(cameraState->pitch); |
| 235 | + float sinPitch = sin(cameraState->pitch); |
| 236 | + |
| 237 | + // Camera offset in spherical coordinates (negative distance to position behind car) |
| 238 | + glm::vec3 cameraOffset; |
| 239 | + cameraOffset.x = -cameraState->distance * cosYaw * cosPitch; |
| 240 | + cameraOffset.y = -cameraState->distance * sinYaw * cosPitch; |
| 241 | + cameraOffset.z = cameraState->height + cameraState->distance * sinPitch; |
| 242 | + |
| 243 | + glm::vec3 cameraPos = carPosition.value + cameraOffset; |
| 244 | + |
| 245 | + // Camera looks at the car (with slight forward offset) |
| 246 | + glm::vec3 lookTarget = carPosition.value + carForward * 1.0f + glm::vec3(0.0f, 0.0f, 1.0f); |
| 247 | + glm::vec3 cameraDir = glm::normalize(lookTarget - cameraPos); |
| 248 | + |
| 249 | + // Update renderer camera |
| 250 | + renderer->setCameraPos(cameraPos); |
| 251 | + renderer->setCameraDir(cameraDir); |
| 252 | + |
| 253 | + // Only handle the first car entity for now |
| 254 | + break; |
| 255 | + } |
196 | 256 | } |
197 | 257 |
|
198 | 258 | return {}; |
199 | 259 | } |
200 | 260 |
|
201 | 261 | void scenes::nfs::NFS::build(Game& game) { |
202 | | - std::shared_ptr<scenes::nfs::components::CameraState> cameraState; |
| 262 | + auto cameraState = std::make_shared<scenes::nfs::components::CameraState>(); |
203 | 263 | game.addResource(cameraState); |
204 | 264 |
|
205 | 265 | game.addSystem(Schedule::Startup, startup); |
|
0 commit comments