Fix billboarding of enemies and their shooting positions

This commit is contained in:
s473577 2024-01-24 00:35:11 +01:00 committed by Qumpell
parent f1c3a1b57b
commit 4e3264ad2a
4 changed files with 16 additions and 34 deletions

View File

@ -72,23 +72,9 @@ private:
} }
void shoot(float time) { void shoot(float time) {
Spaceship* spaceship = Spaceship::getInstance();
glm::vec3 bulletDirection = glm::normalize(spaceship->spaceshipPos - glm::vec3(modelMatrix[3]));
this->bullets.push_back(Bullet::createSimpleBullet(bulletDirection, this->modelMatrix[3], time));
glm::vec3 spaceshipPos = Spaceship::getInstance()->spaceshipPos;
// Przekształć pozycję statku do przestrzeni lokalnej obiektu Enemy
glm::vec3 localSpaceshipPos = glm::inverse(glm::mat3(this->modelMatrix)) * (spaceshipPos - glm::vec3(this->modelMatrix[3]));
// Uzyskaj kierunek od obecnej pozycji obiektu Enemy do pozycji statku
glm::vec3 localBulletDirection = glm::normalize(localSpaceshipPos);
// Przekształć kierunek strzału do przestrzeni świata, biorąc pod uwagę macierz modelu
glm::vec3 worldBulletDirection = glm::mat3(this->modelMatrix) * localBulletDirection;
// Dodaj nowy pocisk do listy pocisków
this->bullets.push_back(Bullet::createSimpleBullet(worldBulletDirection, this->modelMatrix[3], time));
this->lastShootTime = time; this->lastShootTime = time;
} }
}; };

View File

@ -1,4 +1,5 @@
#include "SpriteRenderer.h" #include "SpriteRenderer.h"
#include <iostream>
Core::SpriteRenderer::SpriteRenderer() Core::SpriteRenderer::SpriteRenderer()
{ {
@ -18,31 +19,25 @@ void Core::SpriteRenderer::DrawSprite(GLuint spriteTexture, const glm::mat4 mode
// Pobierz pozycjê kamery // Pobierz pozycjê kamery
glm::vec3 cameraPosition = spaceship->cameraPos; glm::vec3 cameraPosition = spaceship->cameraPos;
// Twórz macierz lookAt miêdzy kwadratem a kamer¹ glm::vec3 spritePosition = glm::vec3(modelMatrix[3]);
glm::mat4 billboardModelMatrix = glm::lookAt(glm::vec3(modelMatrix[3]), cameraPosition, glm::vec3(0.0f, 1.0f, 0.0f));
// Reset the rotation part of the matrix to make it face the camera // Oblicz wektor skierowany od sprita do kamery
//billboardModelMatrix[0][0] = 1.0f; glm::vec3 spriteToCamera = glm::normalize(cameraPosition - spritePosition);
//billboardModelMatrix[0][1] = 0.0f;
//billboardModelMatrix[0][2] = 0.0f;
billboardModelMatrix[1][0] = 0.0f; // Oblicz k¹t rotacji w stopniach wokó³ osi Y
billboardModelMatrix[1][1] = 1.0f; float angle = glm::degrees(atan2(spriteToCamera.x, spriteToCamera.z));
billboardModelMatrix[1][2] = 0.0f;
billboardModelMatrix[2][0] = 0.0f;
billboardModelMatrix[2][1] = 0.0f;
billboardModelMatrix[2][2] = 1.0f;
// Utwórz macierz rotacji wok³ osi Y
glm::mat4 rotationMatrix = glm::rotate(glm::mat4(1.0f), glm::radians(angle), glm::vec3(0.0f, 1.0f, 0.0f));
// Utwórz macierz widoku-projekcji // Utwórz macierz widoku-projekcji
glm::mat4 viewProjectionMatrix = Core::createPerspectiveMatrix() * spaceship->createCameraMatrix(); glm::mat4 viewProjectionMatrix = Core::createPerspectiveMatrix() * spaceship->createCameraMatrix();
// Kombinuj macierze transformacji // Kombinuj macierze transformacji
glm::mat4 transformation = viewProjectionMatrix * billboardModelMatrix; glm::mat4 transformation = viewProjectionMatrix * modelMatrix * rotationMatrix;
// Przeka¿ macierze do shadera // Przeka¿ macierze do shadera
glUniformMatrix4fv(glGetUniformLocation(program, "transformation"), 1, GL_FALSE, glm::value_ptr(transformation)); glUniformMatrix4fv(glGetUniformLocation(program, "transformation"), 1, GL_FALSE, glm::value_ptr(transformation));
glUniformMatrix4fv(glGetUniformLocation(program, "modelMatrix"), 1, GL_FALSE, glm::value_ptr(billboardModelMatrix)); glUniformMatrix4fv(glGetUniformLocation(program, "modelMatrix"), 1, GL_FALSE, glm::value_ptr(modelMatrix));
Core::SetActiveTexture(spriteTexture, "colorTexture", program, 0); Core::SetActiveTexture(spriteTexture, "colorTexture", program, 0);
glBindVertexArray(this->VAO); glBindVertexArray(this->VAO);

View File

@ -14,7 +14,7 @@ out vec3 color;
void main() void main()
{ {
worldPos = (modelMatrix* vec4(vertexPosition,1)).xyz; worldPos = (modelMatrix* vec4(vertexPosition,1)).xyz;
gl_Position = transformation * vec4(worldPos, 1.0); gl_Position = transformation * vec4(vertexPosition, 1.0);
texCoord = vertexTexCoord; texCoord = vertexTexCoord;
texCoord.y = 1.0 - texCoord.y; // so that it is turned correctly texCoord.y = 1.0 - texCoord.y; // so that it is turned correctly
color = aColor; color = aColor;

View File

@ -125,6 +125,7 @@ void renderScene(GLFWwindow* window)
spaceship->color, spaceship->color,
spaceship->roughness, spaceship->metallic, program spaceship->roughness, spaceship->metallic, program
); );
glUseProgram(programSprite); glUseProgram(programSprite);
for (const auto& enemy : enemies) { for (const auto& enemy : enemies) {
if (enemy->isAlive()) { if (enemy->isAlive()) {