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) {
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));
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));
this->lastShootTime = time;
}
};

View File

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

View File

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

View File

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