To extend the code provided with a more complete implementation, let's focus on areas like trajectory visualization, terrain deformation rendering, code cleanup, and including missing parts such as rendering the trajectory of the projectile or any missing transformation functions.
Here’s a more complete approach, integrating these details:
#include "lab_m1/tema1/tema1.h"
#include <vector>
#include <iostream>
#include "lab_m1/tema1/transform2D.h"
#include "lab_m1/tema1/object2Dtema.h"
using namespace std;
using namespace m1;
Tema1::Tema1()
: tankX(100.0f), tankAngle(0.0f), turretAngle(glm::radians(0.0f)), moveSpeed(100.0f), rotationSpeed(100.0f),
vitezaInitiala(200.0f), gravitatie(9.81f), numPuncte(100), intervalTimp(0.1f), affectedRadius(50.0f)
{
}
Tema1::~Tema1()
{
}
float gen_y(float x) {
x /= 50;
return 1.5 * sin(0.5 * x) + 1.5 * sin(0.6 * x) + 0.4 * sin(1.5 * x);
}
void Tema1::Init()
{
glm::ivec2 resolution = window->GetResolution();
auto camera = GetSceneCamera();
camera->SetOrthographic(0, (float)resolution.x, 0, (float)resolution.y, 0.01f, 400);
camera->SetPosition(glm::vec3(0, 0, 50));
camera->SetRotation(glm::vec3(0, 0, 0));
camera->Update();
GetCameraInput()->SetActive(false);
glm::vec3 corner = glm::vec3(0, 0, 0);
float squareSide = 1;
// Set ground color to brown
Mesh* square = object2Dtema::CreateSquare("square", corner, squareSide, glm::vec3(0.7098f, 0.7412f, 0.5373f), true);
AddMeshToList(square);
numSquare = 1280; // Set the desired number of squares
int step = resolution.x / numSquare;
for (size_t i = 0; i < numSquare + 1; ++i) {
xPositions.push_back(i * step);
heightMap.push_back(30 * gen_y(i * step) + 300);
}
InitTank();
}
void Tema1::InitTank() {
// Build the tank's base with two trapezoids
tankWidth = 100.0f;
Mesh* base1 = object2Dtema::CreateTrapezoid("base1", glm::vec3(0, 10, 0), 80.0f, 100.0f, 20.0f, glm::vec3(0.5f, 0.5f, 0.5f), true);
Mesh* base2 = object2Dtema::CreateTrapezoid("base2", glm::vec3(20, 0, 0), 70.0f, 60.0f, 10.0f, glm::vec3(0.3f, 0.3f, 0.3f), true);
// Create the turret and the barrel
Mesh* turret = object2Dtema::CreateCircle("turret", glm::vec3(50, 30, 0), 17.5f, glm::vec3(0.5f, 0.5f, 0.5f), true);
Mesh* barrel = object2Dtema::CreateTrapezoid("barrel", glm::vec3(0, 0, 0), 40, 40, 5, glm::vec3(0.3f, 0.3f, 0.3f), true);
// Add meshes to list
AddMeshToList(base1);
AddMeshToList(base2);
AddMeshToList(turret);
AddMeshToList(barrel);
Mesh* linieTraiectorie = object2Dtema::CreateLine("line", glm::vec3(0, 0, 0), glm::vec3(1, 0, 0), glm::vec3(0.0f, 1.0f, 1.0f));
AddMeshToList(linieTraiectorie);
// Create a circle for the projectile
Mesh* projectile = object2Dtema::CreateCircle("projectile", glm::vec3(0, 0, 0), 5.0f, glm::vec3(1.0f, 0.0f, 0.0f), true);
AddMeshToList(projectile);
}
void Tema1::FrameStart()
{
// Set the background color to blue for the sky
glClearColor(0.529f, 0.808f, 0.922f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glm::ivec2 resolution = window->GetResolution();
// Sets the screen area where to draw
glViewport(0, 0, resolution.x, resolution.y);
}
float Tema1::CalculateTankY(float tankX) {
for (size_t i = 0; i < numSquare; ++i) {
float x1 = xPositions[i];
float y1 = heightMap[i];
float x2 = xPositions[i + 1];
float y2 = heightMap[i + 1];
if (tankX >= x1 && tankX <= x2) {
float t = (tankX - x1) / (x2 - x1);
return y1 + t * (y2 - y1);
}
}
return 0; // Should not happen if tankX is within the map
}
float Tema1::getAngle(float tankX) {
for (size_t i = 0; i < numSquare; ++i) {
float x1 = xPositions[i];
float y1 = heightMap[i];
float x2 = xPositions[i + 1];
float y2 = heightMap[i + 1];
if (tankX >= x1 && tankX <= x2)
return atan2(y2 - y1, (x2 - x1));
}
return 0;
}
std::vector<glm::vec2> Tema1::CalculTraiectorie(float vitezaInitiala, float unghiTurret, float gravitatie, int numPuncte, float intervalTimp, glm::vec2 tipTurret) {
std::vector<glm::vec2> puncteTraiectorie;
glm::vec2 pozitieVarfTun = tipTurret;
for (int i = 0; i < numPuncte; ++i) {
float t = i * intervalTimp;
glm::vec2 pozitie;
pozitie.x = pozitieVarfTun.x + vitezaInitiala * cos(unghiTurret) * t;
pozitie.y = pozitieVarfTun.y + vitezaInitiala * sin(unghiTurret) * t - 0.5f * gravitatie * t * t;
puncteTraiectorie.push_back(pozitie);
if (pozitie.y < 0) break;
}
return puncteTraiectorie;
}
void Tema1::DrawTerrain() {
glm::mat3 modelMatrix;
for (size_t i = 0; i < numSquare; ++i) {
float x1 = xPositions[i];
float y1 = heightMap[i];
float x2 = xPositions[i + 1];
float y2 = heightMap[i + 1];
modelMatrix = glm::mat3(1);
modelMatrix *= transform2D::Translate(x1, y1);
modelMatrix *= transform2D::Shear((y2 - y1) / (x2 - x1));
modelMatrix *= transform2D::Scale(x2 - x1, max(y1, y2));
modelMatrix *= transform2D::Translate(0, -1);
RenderMesh2D(meshes["square"], shaders["VertexColor"], modelMatrix);
}
}
void Tema1::DeformTerrain(glm::vec2 impactPoint, float radius) {
for (size_t i = 0; i < numSquare + 1; ++i) {
float dist = abs(xPositions[i] - impactPoint.x);
if (dist < radius) {
float normalizedDist = dist / radius;
float deformation = (1 - normalizedDist * normalizedDist) * (radius / 2.0f);
heightMap[i] = max(heightMap[i] - deformation, 0.0f);
}
}
}
void Tema1::Update(float deltaTimeSeconds) {
// Existing code for terrain rendering and tank updating
DrawTerrain();
tankY = CalculateTankY(tankX);
float angle = getAngle(tankX);
modelMatrix = glm::mat3(1);
modelMatrix *= transform2D::Translate(tankX, tankY);
modelMatrix *= transform2D::Rotate(angle);
modelMatrix *= transform2D::Translate(-tankWidth / 2, 0);
RenderMesh2D(meshes["base1"], shaders["VertexColor"], modelMatrix);
RenderMesh2D(meshes["base2"], shaders["VertexColor"], modelMatrix);
RenderMesh2D(meshes["turret"], shaders["VertexColor"], modelMatrix);
glm::mat3 barrelMatrix = modelMatrix;
barrelMatrix *= transform2D::Translate(50, 30);
barrelMatrix *= transform2D::Rotate(turretAngle);
RenderMesh2D(meshes["barrel"], shaders["VertexColor"], barrelMatrix);
glm::vec2 tipTurret(tankX + cos(angle) - 30 * sin(angle), tankY + sin(angle) + 30 * cos(angle));
tipTurret += glm::vec2(cos(turretAngle + angle) * 40, sin(turretAngle + angle) * 40);
std::vector<glm::vec2> puncteTraiectorie = CalculTraiectorie(vitezaInitiala, turretAngle + angle, gravitatie, numPuncte, intervalTimp, tipTurret);
for (size_t i = 0; i < puncteTraiectorie.size() - 1; ++i) {
glm::mat3 lineMatrix = glm::mat3(1);
lineMatrix *= transform2D::Translate(puncteTraiectorie[i].x, puncteTraiectorie[i].y);
lineMatrix *= transform2D::Scale(puncteTraiectorie[i+1].x - puncteTraiectorie[i].x, puncteTraiectorie[i+1].y - puncteTraiectorie[i].y);
RenderMesh2D(meshes["line"], shaders["VertexColor"], lineMatrix);
}
// Update and render projectiles
for (auto& proj : proiectile) {
if (!proj.active) continue;
proj.position += proj.velocity * deltaTimeSeconds;
proj.velocity.y -= gravitatie * deltaTimeSeconds;
float terrainHeight = CalculateTankY(proj.position.x);
if (proj.position.y <= terrainHeight) {
DeformTerrain(proj.position, affectedRadius);
proj.active = false;
}
if (proj.active) {
modelMatrix = glm::mat3(1);
modelMatrix *= transform2D::Translate(proj.position.x, proj.position.y);
RenderMesh2D(meshes["projectile"], shaders["VertexColor"], modelMatrix);
}
}
}
void Tema1::FrameEnd()
{
}
void Tema1::OnInputUpdate(float deltaTime, int mods) {
if (window->KeyHold(GLFW_KEY_D) || window->KeyHold(GLFW_KEY_RIGHT)) {
tankX += moveSpeed * deltaTime;
}
if (window->KeyHold(GLFW_KEY_A) || window->KeyHold(GLFW_KEY_LEFT)) {
tankX -= moveSpeed * deltaTime;
}
if (window->KeyHold(GLFW_KEY_W) || window->KeyHold(GLFW_KEY_UP)) {
turretAngle += glm::radians(rotationSpeed) * deltaTime;
}
if (window->KeyHold(GLFW_KEY_S) || window->KeyHold(GLFW_KEY_DOWN)) {
turretAngle -= glm::radians(rotationSpeed) * deltaTime;
}
}
void Tema1::OnKeyPress(int key, int mods)
{
if (key == GLFW_KEY_SPACE) {
float angle = getAngle(tankX);
glm::vec2 tipTurret(tankX + cos(angle) - 30 * sin(angle), tankY + sin(angle) + 30 * cos(angle));
tipTurret += glm::vec2(cos(turretAngle + angle) * 40, sin(turretAngle + angle) * 40);
glm::vec2 initialVelocity = glm::vec2(vitezaInitiala * cos(turretAngle + angle), vitezaInitiala * sin(turretAngle + angle));
Proiectil newProiectil = { tipTurret, initialVelocity, true };
proiectile.push_back(newProiectil);
}
}
void Tema1::OnKeyRelease(int key, int mods)
{
}
void Tema1::OnMouseMove(int mouseX, int mouseY, int deltaX, int deltaY)
{
}
void Tema1::OnMouseBtnPress(int mouseX, int mouseY, int button, int mods)
{
}
void Tema1::OnMouseBtnRelease(int mouseX, int mouseY, int button, int mods)
{
}
void Tema1::OnMouseScroll(int mouseX, int mouseY, int offsetX, int offsetY)
{
}
void Tema1::OnWindowResize(int width, int height)
{
}
Key Enhancements:
- Trajectory Rendering: For simplicity, I've added code to draw a basic line approximation of the projectile's trajectory.
- Terrain Deformation: When a projectile hits the terrain, it deforms the terrain based on a radius.
- Interpolation Calculations: Linear interpolation is used to determine projectile-terrain collision.
- Overall Game Flow: The game now effectively updates the terrain and projectiles, recalculating paths over time.
This code can be expanded with more intricate details like better collision detection, improved physics, and challenge levels increasing the complexity of the terrain or introducing enemies. Additionally, the visual representation of these elements could be enhanced using more sophisticated shaders and textures.
Please ensure that the supporting files (transform2D.h, object2Dtema.h) and implementation for transformations (Translate, Rotate, Scale, Shear) are correctly defined in your project for this to work seamlessly.