commit
5221b3a689
@ -147,6 +147,11 @@ void GameObjectManager::handleEvent(const SDL_Event& event) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (event.type == SDL_MOUSEMOTION) {
|
||||
// Сохраняем позицию мыши для последующей проверки
|
||||
lastMouseX = event.motion.x;
|
||||
lastMouseY = event.motion.y;
|
||||
}
|
||||
}
|
||||
|
||||
void GameObjectManager::updateScene(size_t ms) {
|
||||
@ -178,4 +183,46 @@ void GameObjectManager::updateScene(size_t ms) {
|
||||
}
|
||||
}
|
||||
|
||||
bool GameObjectManager::isPointInObject(int screenX, int screenY, int objectScreenX, int objectScreenY) const {
|
||||
// Простая проверка попадания точки в квадрат 64x64 вокруг центра объекта
|
||||
const int objectSize = 32; // Половина размера области выделения
|
||||
return (screenX >= objectScreenX - objectSize &&
|
||||
screenX <= objectScreenX + objectSize &&
|
||||
screenY >= objectScreenY - objectSize &&
|
||||
screenY <= objectScreenY + objectSize);
|
||||
}
|
||||
|
||||
void GameObjectManager::checkMouseIntersection(int mouseX, int mouseY, const Matrix4f& projectionModelView) {
|
||||
for (auto& ao : activeObjects) {
|
||||
int screenX, screenY;
|
||||
worldToScreenCoordinates(ao.objectPos, projectionModelView,
|
||||
Environment::width, Environment::height, screenX, screenY);
|
||||
|
||||
if (isPointInObject(mouseX, mouseY, screenX, screenY)) {
|
||||
std::cout << "Mouse over object at screen coordinates: "
|
||||
<< screenX << ", " << screenY
|
||||
<< " (world pos: "
|
||||
<< ao.objectPos.v[0] << ", "
|
||||
<< ao.objectPos.v[1] << ", "
|
||||
<< ao.objectPos.v[2] << ")"
|
||||
<< std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GameObjectManager::worldToScreenCoordinates(Vector3f objectPos,
|
||||
Matrix4f projectionModelView,
|
||||
int screenWidth, int screenHeight,
|
||||
int& screenX, int& screenY) {
|
||||
|
||||
Vector4f inx = { objectPos.v[0], objectPos.v[1], objectPos.v[2], 1.0f};
|
||||
Vector4f clipCoords = MultMatrixVector(projectionModelView, inx);
|
||||
|
||||
float ndcX = clipCoords.v[0] / clipCoords.v[3];
|
||||
float ndcY = clipCoords.v[1] / clipCoords.v[3];
|
||||
|
||||
screenX = (int)((ndcX + 1.0f) * 0.5f * screenWidth);
|
||||
screenY = (int)((1.0f + ndcY) * 0.5f * screenHeight);
|
||||
}
|
||||
|
||||
} // namespace ZL
|
||||
|
||||
@ -15,6 +15,7 @@ public:
|
||||
void update();
|
||||
void handleEvent(const SDL_Event& event);
|
||||
void updateScene(size_t ms);
|
||||
void checkMouseIntersection(int mouseX, int mouseY, const Matrix4f& projectionModelView); // Добавляем новый метод
|
||||
|
||||
std::shared_ptr<ZL::Texture> testObjTexturePtr;
|
||||
std::shared_ptr<ZL::Texture> roomTexturePtr;
|
||||
@ -46,6 +47,13 @@ public:
|
||||
|
||||
private:
|
||||
int animationCounter = 0;
|
||||
int lastMouseX = 0; // Добавляем переменные для хранения позиции мыши
|
||||
int lastMouseY = 0;
|
||||
bool isPointInObject(int screenX, int screenY, int objectScreenX, int objectScreenY) const;
|
||||
void worldToScreenCoordinates(Vector3f objectPos, // Добавляем метод
|
||||
Matrix4f projectionModelView,
|
||||
int screenWidth, int screenHeight,
|
||||
int& screenX, int& screenY);
|
||||
};
|
||||
|
||||
} // namespace ZL
|
||||
|
||||
@ -71,6 +71,10 @@ void RenderSystem::drawWorld(const GameObjectManager& gameObjects) {
|
||||
renderer.DrawVertexRenderStruct(gameObjects.textMeshMutable);
|
||||
|
||||
Matrix4f latestProjectionModelView = renderer.GetProjectionModelViewMatrix();
|
||||
|
||||
// Проверяем пересечение с мышью после расчета всех матриц
|
||||
const_cast<GameObjectManager&>(gameObjects).checkMouseIntersection(
|
||||
lastMouseX, lastMouseY, latestProjectionModelView);
|
||||
|
||||
renderer.PopMatrix();
|
||||
renderer.PopProjectionMatrix();
|
||||
|
||||
@ -24,6 +24,8 @@ private:
|
||||
Renderer renderer;
|
||||
ShaderManager shaderManager;
|
||||
Matrix4f currentProjectionModelView; // Добавлено для хранения матрицы между drawWorld и drawUI
|
||||
int lastMouseX = 0;
|
||||
int lastMouseY = 0;
|
||||
};
|
||||
|
||||
} // namespace ZL
|
||||
|
||||
Loading…
Reference in New Issue
Block a user