完成碰撞器组件与碰撞检测

This commit is contained in:
Ziyu
2025-06-16 14:44:47 +08:00
parent b4399d2f41
commit ddb02723f5
12 changed files with 477 additions and 1 deletions
+24
View File
@@ -4,6 +4,7 @@
#include "../../engine/component/transform_component.h"
#include "../../engine/component/sprite_component.h"
#include "../../engine/component/physics_component.h"
#include "../../engine/component/collider_component.h"
#include "../../engine/physics/physics_engine.h"
#include "../../engine/scene/level_loader.h"
#include "../../engine/input/input_manager.h"
@@ -33,6 +34,7 @@ void GameScene::init() {
void GameScene::update(float delta_time) {
Scene::update(delta_time);
TestCollisionPairs();
}
void GameScene::render() {
@@ -59,8 +61,22 @@ void GameScene::createTestObject() {
test_object->addComponent<engine::component::TransformComponent>(glm::vec2(100.0f, 100.0f));
test_object->addComponent<engine::component::SpriteComponent>("assets/textures/Props/big-crate.png", context_.getResourceManager());
test_object->addComponent<engine::component::PhysicsComponent>(&context_.getPhysicsEngine());
test_object->addComponent<engine::component::ColliderComponent>(
std::make_unique<engine::physics::AABBCollider>(glm::vec2(32.0f, 32.0f))
);
// 将创建好的 GameObject 添加到场景中 (一定要用std::move,否则传递的是左值)
addGameObject(std::move(test_object));
// 添加第二个游戏对象(不受重力影响),用于测试碰撞
auto test_object2 = std::make_unique<engine::object::GameObject>("test_object2");
test_object2->addComponent<engine::component::TransformComponent>(glm::vec2(50.0f, 50.0f));
test_object2->addComponent<engine::component::SpriteComponent>("assets/textures/Props/big-crate.png", context_.getResourceManager());
test_object2->addComponent<engine::component::PhysicsComponent>(&context_.getPhysicsEngine(), false);
test_object2->addComponent<engine::component::ColliderComponent>(
std::make_unique<engine::physics::CircleCollider>(16.0f)
);
addGameObject(std::move(test_object2));
spdlog::trace("test_object 创建并添加到 GameScene 中。");
}
@@ -89,4 +105,12 @@ void GameScene::TestObject()
}
}
void GameScene::TestCollisionPairs()
{
auto collision_pairs = context_.getPhysicsEngine().getCollisionPairs();
for (auto& pair : collision_pairs) {
spdlog::info("碰撞对: {} 和 {}", pair.first->getName(), pair.second->getName());
}
}
} // namespace game::scene