mirror of
https://github.com/Kelsidavis/WoWee.git
synced 2026-05-03 00:03:50 +00:00
Fix swimming controls and enforce swim collision sweeps
This commit is contained in:
parent
8bc50818a9
commit
1951dbd9e6
1 changed files with 61 additions and 6 deletions
|
|
@ -157,8 +157,8 @@ void CameraController::update(float deltaTime) {
|
||||||
|
|
||||||
if (nowForward) movement += forward;
|
if (nowForward) movement += forward;
|
||||||
if (nowBackward) movement -= forward;
|
if (nowBackward) movement -= forward;
|
||||||
if (nowStrafeLeft) movement -= right;
|
if (nowStrafeLeft) movement += right;
|
||||||
if (nowStrafeRight) movement += right;
|
if (nowStrafeRight) movement -= right;
|
||||||
|
|
||||||
// Stand up if jumping while crouched
|
// Stand up if jumping while crouched
|
||||||
if (!uiWantsKeyboard && sitting && input.isKeyPressed(SDL_SCANCODE_SPACE)) {
|
if (!uiWantsKeyboard && sitting && input.isKeyPressed(SDL_SCANCODE_SPACE)) {
|
||||||
|
|
@ -186,12 +186,31 @@ void CameraController::update(float deltaTime) {
|
||||||
|
|
||||||
if (inWater) {
|
if (inWater) {
|
||||||
swimming = true;
|
swimming = true;
|
||||||
// Reduce horizontal speed while swimming
|
// Swim movement follows look pitch (forward/back), while strafe stays
|
||||||
|
// lateral for stable control.
|
||||||
float swimSpeed = speed * SWIM_SPEED_FACTOR;
|
float swimSpeed = speed * SWIM_SPEED_FACTOR;
|
||||||
|
|
||||||
if (glm::length(movement) > 0.001f) {
|
glm::vec3 swimForward = glm::normalize(forward3D);
|
||||||
movement = glm::normalize(movement);
|
if (glm::length(swimForward) < 1e-4f) {
|
||||||
targetPos += movement * swimSpeed * deltaTime;
|
swimForward = forward;
|
||||||
|
}
|
||||||
|
glm::vec3 swimRight = camera->getRight();
|
||||||
|
swimRight.z = 0.0f;
|
||||||
|
if (glm::length(swimRight) > 1e-4f) {
|
||||||
|
swimRight = glm::normalize(swimRight);
|
||||||
|
} else {
|
||||||
|
swimRight = right;
|
||||||
|
}
|
||||||
|
|
||||||
|
glm::vec3 swimMove(0.0f);
|
||||||
|
if (nowForward) swimMove += swimForward;
|
||||||
|
if (nowBackward) swimMove -= swimForward;
|
||||||
|
if (nowStrafeLeft) swimMove -= swimRight;
|
||||||
|
if (nowStrafeRight) swimMove += swimRight;
|
||||||
|
|
||||||
|
if (glm::length(swimMove) > 0.001f) {
|
||||||
|
swimMove = glm::normalize(swimMove);
|
||||||
|
targetPos += swimMove * swimSpeed * deltaTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Spacebar = swim up (continuous, not a jump)
|
// Spacebar = swim up (continuous, not a jump)
|
||||||
|
|
@ -234,6 +253,42 @@ void CameraController::update(float deltaTime) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Enforce collision while swimming too (horizontal only), so we don't
|
||||||
|
// pass through walls/props when underwater or at waterline.
|
||||||
|
{
|
||||||
|
glm::vec3 swimFrom = *followTarget;
|
||||||
|
glm::vec3 swimTo = targetPos;
|
||||||
|
float swimMoveDist = glm::length(swimTo - swimFrom);
|
||||||
|
int swimSteps = std::max(1, std::min(12, static_cast<int>(std::ceil(swimMoveDist / 0.22f))));
|
||||||
|
glm::vec3 stepPos = swimFrom;
|
||||||
|
glm::vec3 stepDelta = (swimTo - swimFrom) / static_cast<float>(swimSteps);
|
||||||
|
|
||||||
|
for (int i = 0; i < swimSteps; i++) {
|
||||||
|
glm::vec3 candidate = stepPos + stepDelta;
|
||||||
|
|
||||||
|
if (wmoRenderer) {
|
||||||
|
glm::vec3 adjusted;
|
||||||
|
if (wmoRenderer->checkWallCollision(stepPos, candidate, adjusted)) {
|
||||||
|
candidate.x = adjusted.x;
|
||||||
|
candidate.y = adjusted.y;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (m2Renderer) {
|
||||||
|
glm::vec3 adjusted;
|
||||||
|
if (m2Renderer->checkCollision(stepPos, candidate, adjusted)) {
|
||||||
|
candidate.x = adjusted.x;
|
||||||
|
candidate.y = adjusted.y;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
stepPos = candidate;
|
||||||
|
}
|
||||||
|
|
||||||
|
targetPos.x = stepPos.x;
|
||||||
|
targetPos.y = stepPos.y;
|
||||||
|
}
|
||||||
|
|
||||||
grounded = false;
|
grounded = false;
|
||||||
} else {
|
} else {
|
||||||
swimming = false;
|
swimming = false;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue