Basic walking controls (for K+M and controller).

This commit is contained in:
Patrick 2024-09-26 15:40:27 +02:00
parent 9a5638670a
commit e92f414e02
4 changed files with 52 additions and 16 deletions

View File

@ -1,9 +1,12 @@
#include "./app.hpp" #include "./app.hpp"
#include <numbers>
#include <glm/mat4x4.hpp> #include <glm/mat4x4.hpp>
#include <glm/vec2.hpp> #include <glm/vec2.hpp>
#include <glm/vec4.hpp> #include <glm/vec4.hpp>
#include <glm/gtx/norm.hpp>
#include "../scene/mesh.hpp" #include "../scene/mesh.hpp"
#include "../sdlpp/keyboard.hpp" #include "../sdlpp/keyboard.hpp"
@ -14,8 +17,8 @@ namespace sdl_gpu_test
{ {
namespace namespace
{ {
inline constexpr float PITCH_MIN = -89.f; inline constexpr float PITCH_MIN = -0.49f * std::numbers::pi_v<float>;
inline constexpr float PITCH_MAX = 89.f; inline constexpr float PITCH_MAX = 0.49f * std::numbers::pi_v<float>;
inline constexpr Uint16 AXIS_DEADZONE = 5000; inline constexpr Uint16 AXIS_DEADZONE = 5000;
} }
@ -101,8 +104,8 @@ void ThreeDSceneApp::update(const AppUpdateArgs& args)
mSceneRenderer.render({ mSceneRenderer.render({
.camera = { .camera = {
.position = mPosition, .position = mPosition,
.pitch = glm::radians(mPitch), .pitch = mPitch,
.yaw = glm::radians(mYaw) .yaw = mYaw
}, },
.cmdBuffer = &cmdBuffer, .cmdBuffer = &cmdBuffer,
.targetTexture = &swapchainTexture, .targetTexture = &swapchainTexture,
@ -141,8 +144,8 @@ void ThreeDSceneApp::handleMouseMotionEvent(const sdlpp::MouseMotionEvent& event
{ {
mWidgetTree.notifyMouseMoved(event); mWidgetTree.notifyMouseMoved(event);
mYaw -= 0.5f * event.xrel; mYaw -= 0.002f * event.xrel;
mPitch -= 0.5f * event.yrel; mPitch -= 0.002f * event.yrel;
} }
void ThreeDSceneApp::handleMouseButtonEvent(const sdlpp::MouseButtonEvent& event) void ThreeDSceneApp::handleMouseButtonEvent(const sdlpp::MouseButtonEvent& event)
@ -152,30 +155,65 @@ void ThreeDSceneApp::handleMouseButtonEvent(const sdlpp::MouseButtonEvent& event
void ThreeDSceneApp::processInput(const AppUpdateArgs& args) void ThreeDSceneApp::processInput(const AppUpdateArgs& args)
{ {
glm::vec2 movement = {0.f, 0.f};
const std::span<const SDL_bool> keyboardState = sdlpp::getKeyboardState();
if (mGamepad) if (mGamepad)
{ {
Sint16 axisValue = mGamepad.getAxis(sdlpp::GamepadAxis::RIGHTX); Sint16 axisValue = mGamepad.getAxis(sdlpp::GamepadAxis::RIGHTX);
if (std::abs(axisValue) > AXIS_DEADZONE) if (std::abs(axisValue) > AXIS_DEADZONE)
{ {
mYaw -= 0.005f * args.tickSeconds * static_cast<float>(axisValue); mYaw -= 0.0001f * args.tickSeconds * static_cast<float>(axisValue);
} }
axisValue = mGamepad.getAxis(sdlpp::GamepadAxis::RIGHTY); axisValue = mGamepad.getAxis(sdlpp::GamepadAxis::RIGHTY);
if (std::abs(axisValue) > AXIS_DEADZONE) if (std::abs(axisValue) > AXIS_DEADZONE)
{ {
mPitch -= 0.005f * args.tickSeconds * static_cast<float>(axisValue); mPitch -= 0.0001f * args.tickSeconds * static_cast<float>(axisValue);
} }
axisValue = mGamepad.getAxis(sdlpp::GamepadAxis::LEFTY); axisValue = mGamepad.getAxis(sdlpp::GamepadAxis::LEFTY);
if (std::abs(axisValue) > AXIS_DEADZONE) if (std::abs(axisValue) > AXIS_DEADZONE)
{ {
mPosition.x += 0.0005f * args.tickSeconds * std::sin(mYaw) * static_cast<float>(axisValue); movement.y -= static_cast<float>(axisValue) / static_cast<float>(std::numeric_limits<Sint16>::max());
mPosition.z += 0.0005f * args.tickSeconds * std::cos(mYaw) * static_cast<float>(axisValue); }
axisValue = mGamepad.getAxis(sdlpp::GamepadAxis::LEFTX);
if (std::abs(axisValue) > AXIS_DEADZONE)
{
movement.x += static_cast<float>(axisValue) / static_cast<float>(std::numeric_limits<Sint16>::max());
} }
} }
while (mYaw >= 360.f) { mYaw -= 360.f; } if (keyboardState[SDL_SCANCODE_W])
while (mYaw < 0.f) { mYaw += 360.f; } {
movement.y += 1.f;
}
if (keyboardState[SDL_SCANCODE_S])
{
movement.y -= 1.f;
}
if (keyboardState[SDL_SCANCODE_A])
{
movement.x -= 1.f;
}
if (keyboardState[SDL_SCANCODE_D])
{
movement.x += 1.f;
}
if (glm::length2(movement) > 1.f)
{
movement = glm::normalize(movement);
}
mPosition.x -= 2.f * args.tickSeconds * std::sin(mYaw) * movement.y;
mPosition.z -= 2.f * args.tickSeconds * std::cos(mYaw) * movement.y;
mPosition.x += 2.f * args.tickSeconds * std::cos(mYaw) * movement.x;
mPosition.z -= 2.f * args.tickSeconds * std::sin(mYaw) * movement.x;
while (mYaw >= 2.f * std::numbers::pi) { mYaw -= 2.f * std::numbers::pi_v<float>; }
while (mYaw < 0.f) { mYaw += 2.f * std::numbers::pi_v<float>; }
mPitch = std::clamp(mPitch, PITCH_MIN, PITCH_MAX); mPitch = std::clamp(mPitch, PITCH_MIN, PITCH_MAX);
} }
} }

View File

@ -30,7 +30,7 @@ src_files = Split("""
shader_files = env.Glob("#assets/shaders/glsl/*.frag") \ shader_files = env.Glob("#assets/shaders/glsl/*.frag") \
+ env.Glob("#assets/shaders/glsl/*.vert") + env.Glob("#assets/shaders/glsl/*.vert")
env.Append(CPPDEFINES = ['GLM_FORCE_DEPTH_ZERO_TO_ONE', 'GLM_FORCE_RADIANS']) env.Append(CPPDEFINES = ['GLM_FORCE_DEPTH_ZERO_TO_ONE', 'GLM_FORCE_RADIANS', 'GLM_ENABLE_EXPERIMENTAL'])
prog_app = env.UnityProgram( prog_app = env.UnityProgram(
target = env['BIN_DIR'] + '/sdl_gpu_test', target = env['BIN_DIR'] + '/sdl_gpu_test',
source = src_files, source = src_files,

View File

@ -79,7 +79,7 @@ void SceneRenderer::render(const SceneRendererRenderArgs& args)
VertexShaderParameters vertexShaderParameters = { VertexShaderParameters vertexShaderParameters = {
// note that we are transforming the world, but are passed the camera transform, so invert it // note that we are transforming the world, but are passed the camera transform, so invert it
.worldToView = glm::translate( .worldToView = glm::translate(
glm::yawPitchRoll(-args.camera.yaw, -args.camera.pitch, -args.camera.roll), glm::transpose(glm::yawPitchRoll(args.camera.yaw, args.camera.pitch, args.camera.roll)),
-args.camera.position -args.camera.position
), ),
.viewToClip = glm::perspectiveFov( .viewToClip = glm::perspectiveFov(

View File

@ -4,13 +4,11 @@
#if !defined(SDL_GPU_TEST_PRIVATE_SDL_GPU_TEST_SCENE_TRANSFORM3D_HPP_INCLUDED) #if !defined(SDL_GPU_TEST_PRIVATE_SDL_GPU_TEST_SCENE_TRANSFORM3D_HPP_INCLUDED)
#define SDL_GPU_TEST_PRIVATE_SDL_GPU_TEST_SCENE_TRANSFORM3D_HPP_INCLUDED 1 #define SDL_GPU_TEST_PRIVATE_SDL_GPU_TEST_SCENE_TRANSFORM3D_HPP_INCLUDED 1
#define GLM_ENABLE_EXPERIMENTAL 1
#include <glm/mat4x4.hpp> #include <glm/mat4x4.hpp>
#include <glm/vec3.hpp> #include <glm/vec3.hpp>
#include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/euler_angles.hpp> #include <glm/gtx/euler_angles.hpp>
#include <glm/gtx/matrix_decompose.hpp> #include <glm/gtx/matrix_decompose.hpp>
#undef GLM_ENABLE_EXPERIMENTAL
namespace sdl_gpu_test namespace sdl_gpu_test
{ {