Skip to content

Commit cd89a57

Browse files
committed
Add physics system with collision world, character controller and rigid bodies
- src/physics: collision shapes (sphere/box/capsule/plane), AABB/Ray/Raycast and closest-point math, ShapeCollision narrow phase including capsule vs rotated box, and PhysicsWorld with static registration, id-to-index lookup, a sweep broadphase shared by queries and stepping, raycast/overlap queries, collision layers/masks, fixed step with render interpolation and sleeping. - character: sub-stepped move-and-slide with slope limit and ground snapping; rigid bodies: sphere dynamic bodies solved with sequential impulses. - scene: PhysicsBodyComponent plus GameObject::setPhysicsBody/clearPhysicsBody, unregistering automatically when the object is removed. - examples/physics_demo: static terrain, playable character, dynamic balls, raycast placement, collider overlay. Camera-relative movement moved into examples/common/CameraRelativeInput.h (fixes inverted left/right). - tests: 9 new cpu tests (shapes, math, raycast, collision, world, component, character, rigidbody, camera-relative); cpu suite now 28 tests.
1 parent 5c749c2 commit cd89a57

36 files changed

Lines changed: 4613 additions & 1 deletion

‎CMakeLists.txt‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ find_package(glm CONFIG REQUIRED)
1414
find_path(STB_IMAGE_INCLUDE_DIR NAMES stb/stb_image.h REQUIRED)
1515

1616
set(ENGINE_SOURCES
17+
src/physics/math/Aabb.cpp
18+
src/physics/math/Ray.cpp
19+
src/physics/math/Raycast.cpp
20+
src/physics/math/ClosestPoint.cpp
21+
src/physics/shapes/CollisionShape.cpp
22+
src/physics/world/ShapeCollision.cpp
23+
src/physics/world/PhysicsWorld.cpp
24+
src/physics/character/CharacterController.cpp
1725
src/graphics/shaders/SkinShaderSource.cpp
1826
src/graphics/lighting/DirectionalShadowSettings.cpp
1927
src/graphics/rendering/shadows/DirectionalShadowMap.cpp
@@ -59,6 +67,7 @@ set(ENGINE_SOURCES
5967
src/scene/SceneSerializer.cpp
6068
src/scene/ModelInstantiator.cpp
6169
src/scene/components/Renderable.cpp
70+
src/scene/components/PhysicsBodyComponent.cpp
6271
src/platform/Window.cpp
6372
src/resources/ResourceManager.cpp
6473
src/resources/Model.cpp
@@ -310,6 +319,13 @@ target_compile_options(infinite_example_directional_shadow PRIVATE -Wall -Wextra
310319
target_link_libraries(infinite_example_directional_shadow PRIVATE infinite_engine)
311320
configure_engine_example(infinite_example_directional_shadow directional_shadow)
312321

322+
# 物理示例:静态地形、角色控制器、动态刚体、射线放置与碰撞体调试可视化。
323+
add_executable(infinite_example_physics_demo examples/physics_demo/main.cpp)
324+
target_include_directories(infinite_example_physics_demo PRIVATE examples)
325+
target_compile_options(infinite_example_physics_demo PRIVATE -Wall -Wextra)
326+
target_link_libraries(infinite_example_physics_demo PRIVATE infinite_engine)
327+
configure_engine_example(infinite_example_physics_demo physics_demo)
328+
313329
if(BUILD_TESTING)
314330
include(cmake/EngineTests.cmake)
315331
endif()

‎README.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
一个使用 C++、OpenGL 和 GLFW 编写的游戏引擎学习项目。
44

5+
**从零上手请先读[引擎入门教程](docs/引擎入门教程.md)**:环境准备(含 PATH 陷阱)→ 构建与测试 → 引擎心智模型与生命周期铁律 → 逐模块用法(渲染/光照/模型/动画/物理/输入/存档)→ 写测试 → 交付 → 常见问题排查。
6+
57
功能总览、对应示例及尚不支持的边界见[项目功能清单](docs/项目功能清单.md)。先从这份索引了解框架,再按需阅读专题说明。
68

79
本轮维护内容与测量记录见[维护优化记录](docs/维护优化记录.md),新增测试的方式见[测试维护约定](tests/README.md)。
@@ -24,6 +26,7 @@ src/
2426
├── platform/ # 窗口和OpenGL上下文
2527
├── scene/ # 游戏对象、组件、对象所有权、场景管理和场景序列化
2628
├── resources/ # Shader、Texture、Mesh、Material和Model缓存
29+
├── physics/ # 碰撞形状、AABB/射线、静态碰撞世界、角色控制器、刚体
2730
└── graphics/
2831
├── camera/ # Camera
2932
├── lighting/ # SceneLighting、方向光、点光源和聚光灯
@@ -50,9 +53,12 @@ runtime/ # 各示例的可执行文件、资源和运行库
5053
- `application_template`:可直接复制的空白应用模板,不创建任何默认物体。
5154
- `primitive_shapes`:使用引擎内置创建接口生成平面、立方体、圆盘、球体、圆柱和圆锥,不手写顶点数据。
5255
- `scene_lighting`:场景级环境光、多方向光、点光源和聚光灯,演示光照与材质职责分离。
56+
- `physics_demo`:静态地形(地面、斜坡、陡坡、台阶)上的角色控制器、动态刚体球、射线放置方块和碰撞体可视化。按键与边界见[物理系统说明](docs/物理系统说明.md)。
5357

5458
新增独立示例:`pbr_materials`(材质对照)、`gltf_pbr`(原版Avocado)、`node_animation`(刚体动画)、`skeletal_animation`(Rigged Figure双实例)、`directional_shadow`(主方向光投影)。
5559

60+
物理系统的接口、运行方式、生命周期规则与明确边界见[物理系统说明](docs/物理系统说明.md);阶段划分见[物理系统实施计划](docs/物理系统实施计划.md)。
61+
5662
场景光照的接口、限制和测试见[场景光照说明](docs/场景光照说明.md);阶段状态见[渲染与动画实施计划](docs/渲染与动画实施计划.md)。
5763

5864
立方体材质设为 `CullMode::Back`,约定屏幕上逆时针绕序为正面、剔除背面。

‎cmake/EngineTests.cmake‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ endforeach()
7171
# 实际启动全部示例及空白模板,隐藏窗口绘制三帧并检查像素。
7272
# 使用已有示例目标,不调用测试helper重新配置示例;运行目录特意不同于资源源码目录。
7373
foreach(EXAMPLE_NAME IN ITEMS color_triangle textured_quad diffuse_lighting player_controller textured_cube
74-
alpha_blending scene_objects asset_scene gltf_model application_template material_showcase primitive_shapes scene_lighting pbr_materials gltf_pbr node_animation skeletal_animation directional_shadow)
74+
alpha_blending scene_objects asset_scene gltf_model application_template material_showcase primitive_shapes scene_lighting pbr_materials gltf_pbr node_animation skeletal_animation directional_shadow physics_demo)
7575
add_test(NAME infinite_example_${EXAMPLE_NAME}_smoke
7676
COMMAND $<TARGET_FILE:infinite_example_${EXAMPLE_NAME}> --smoke-test)
7777
set_tests_properties(infinite_example_${EXAMPLE_NAME}_smoke PROPERTIES
@@ -111,6 +111,27 @@ add_executable(infinite_log_test tests/log_test.cpp)
111111
configure_engine_test(infinite_log_test
112112
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}" ARGS "${CMAKE_CURRENT_BINARY_DIR}/tests/logging")
113113

114+
# 物理基础:形状/射线/AABB数学全部为无窗口纯CPU测试,不创建OpenGL上下文。
115+
foreach(PHYSICS_TEST IN ITEMS physics_shapes physics_math physics_raycast)
116+
add_executable(infinite_${PHYSICS_TEST}_test tests/${PHYSICS_TEST}_test.cpp)
117+
configure_engine_test(infinite_${PHYSICS_TEST}_test)
118+
endforeach()
119+
# 碰撞世界与narrowphase同样不依赖窗口;场景侧组件集成也在无窗口下验证。
120+
foreach(PHYSICS_WORLD_TEST IN ITEMS physics_collision physics_world physics_body_component)
121+
add_executable(infinite_${PHYSICS_WORLD_TEST}_test tests/${PHYSICS_WORLD_TEST}_test.cpp)
122+
configure_engine_test(infinite_${PHYSICS_WORLD_TEST}_test)
123+
endforeach()
124+
# 角色控制器与刚体动力学:纯CPU模拟,不创建窗口。
125+
foreach(PHYSICS_SIMULATION_TEST IN ITEMS physics_character physics_rigidbody)
126+
add_executable(infinite_${PHYSICS_SIMULATION_TEST}_test tests/${PHYSICS_SIMULATION_TEST}_test.cpp)
127+
configure_engine_test(infinite_${PHYSICS_SIMULATION_TEST}_test)
128+
endforeach()
129+
130+
# 示例层的摄像机相对输入:纯方向数学,不需要窗口;防止出现左右颠倒之类的回归。
131+
add_executable(infinite_camera_relative_input_test tests/camera_relative_input_test.cpp)
132+
target_include_directories(infinite_camera_relative_input_test PRIVATE examples)
133+
configure_engine_test(infinite_camera_relative_input_test)
134+
114135
# 同时覆盖启用/禁用诊断宏;显式checkErrors在两种编译方式中都可用。
115136
foreach(DIAGNOSTIC_MODE IN ITEMS debug release)
116137
set(DIAGNOSTIC_TARGET "infinite_opengl_${DIAGNOSTIC_MODE}_test")
@@ -156,4 +177,8 @@ set_tests_properties(
156177
infinite_scene_update_test infinite_input_state_test infinite_camera_test infinite_camera_controller_test
157178
infinite_input_action_test infinite_mesh_loader_test infinite_log_test infinite_image_loader_test
158179
infinite_frame_timing_test infinite_scene_serialization_test infinite_animation_sampler_test
180+
infinite_physics_shapes_test infinite_physics_math_test infinite_physics_raycast_test
181+
infinite_physics_collision_test infinite_physics_world_test infinite_physics_body_component_test
182+
infinite_physics_character_test infinite_physics_rigidbody_test
183+
infinite_camera_relative_input_test
159184
PROPERTIES LABELS "cpu")
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#pragma once
2+
3+
#include <glm/geometric.hpp>
4+
#include <glm/vec3.hpp>
5+
6+
#include <cmath>
7+
#include <stdexcept>
8+
9+
// 摄像机相对输入 → 世界空间水平移动方向(示例层共用,不属于引擎库)。
10+
//
11+
// 方向约定:viewDirection是"从摄像机指向观察目标"的视线方向;
12+
// 屏幕右方 = cross(viewDirection, up)。这与FreeCameraController一致:
13+
// 右手系 +Y向上时,视线朝-Z,屏幕右方是+X(朝+Z时右方是-X)。
14+
// 写成cross(up, viewDirection)会得到完全相反的结果(左右颠倒)。
15+
//
16+
// forwardInput对应W/S,rightInput对应D/A;返回未归一化的合成向量,
17+
// 由调用方决定是否归一化(保留"输入强度小于1时按强度移动"的语义)。
18+
// 视线投影到水平面后长度为零(完全垂直俯视)或输入非有限时抛std::invalid_argument。
19+
inline glm::vec3 cameraRelativeDirection(const glm::vec3 &viewDirection, float forwardInput, float rightInput)
20+
{
21+
if (!std::isfinite(forwardInput) || !std::isfinite(rightInput))
22+
{
23+
throw std::invalid_argument("Camera-relative input must be finite");
24+
}
25+
const glm::vec3 flat(viewDirection.x, 0.0f, viewDirection.z);
26+
const float length = glm::length(flat);
27+
if (!std::isfinite(length) || length <= 0.0f)
28+
{
29+
throw std::invalid_argument("Camera-relative movement needs a non-vertical view direction");
30+
}
31+
const glm::vec3 forward = flat / length;
32+
const glm::vec3 right = glm::normalize(glm::cross(forward, glm::vec3(0.0f, 1.0f, 0.0f)));
33+
return forward * forwardInput + right * rightInput;
34+
}

0 commit comments

Comments
 (0)