namespace m1
{
class Tema1 : public gfxc::SimpleScene
{
public:
Tema1();
~Tema1();
code
void Init() override;
void InitTank();
float CalculateTankY(float tankX);
float getAngle(float tankX);
std::vector<glm::vec2> CalculTraiectorie(float vitezaInitiala, float unghi, float gravitatie, int numPuncte, float intervalTimp, glm::vec2 tipTurret);
private:
void FrameStart() override;
void Update(float deltaTimeSeconds) override;
void FrameEnd() override;
void OnInputUpdate(float deltaTime, int mods) override;
void OnKeyPress(int key, int mods) override;
void OnKeyRelease(int key, int mods) override;
void OnMouseMove(int mouseX, int mouseY, int deltaX, int deltaY) override;
void OnMouseBtnPress(int mouseX, int mouseY, int button, int mods) override;
void OnMouseBtnRelease(int mouseX, int mouseY, int button, int mods) override;
void OnMouseScroll(int mouseX, int mouseY, int offsetX, int offsetY) override;
void OnWindowResize(int width, int height) override;
protected:
float cx, cy;
glm::mat3 modelMatrix;
glm::mat3 modelMatrix1;
glm::mat3 modelMatrix2;
float translateX, translateY;
float scaleX, scaleY;
float angularStep;
float tankX; // Poziția tancului pe axa X
float tankAngle; // Unghiul de înclinare al tancului
float turretAngle; // Unghiul de rotație al turelei
float moveSpeed; // Viteza de deplasare a tancului
float rotationSpeed; // Viteza de rotație a turelei
float tankWidth, tankHeight;
std::vector<float> heightMap; // Harta de înălțime
std::vector<float> xPositions; // Pozițiile x pentru fiecare punct din teren
// TODO(student): If you need any other class variables, define them here.
float maxX, maxY;
float min;
int var, var2;
int numSquare;
struct Proiectil {
glm::vec2 position;
glm::vec2 velocity;
bool active;
};
std::vector<Proiectil> proiectile;
float gravitatie = 100.0f; // Ajustează în funcție de contextul jocului
float vitezaProiectil = 300.0f; // Magnitudinea vitezei inițiale
};
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);
code
glm::ivec2 resolution = window->GetResolution();
// Sets the screen area where to draw
glViewport(0, 0, resolution.x, resolution.y);
}
float Tema1::CalculateTankY(float tankX) {
// Căutăm punctele A și B din heightMap care încadrează x-ul tancului
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];
code
if (tankX >= x1 && tankX <= x2) {
// Calculăm t conform formulei
float t = (tankX - x1) / (x2 - x1);
// Calculăm coordonata y a tancului
return y1 + t * (y2 - y1);
}
}
return 0; // În cazul în care tankX este în afara intervalului de puncte (nu ar trebui să se întâmple)
// Start from the tip of the turret
glm::vec2 pozitieVarfTun = tipTurret;
// Generate trajectory points
for (int i = 0; i < numPuncte; ++i) {
float t = i * intervalTimp;
// Calculate the position on trajectory at time t
glm::vec2 pozitie;
pozitie.x = pozitieVarfTun.x + vitezaInitiala * cos(unghiTurret) * t;
pozitie.y = pozitieVarfTun.y + vitezaInitiala * sin(unghiTurret) * t - 0.5f * gravitatie * t * t;
// Add the point to trajectory
puncteTraiectorie.push_back(pozitie);
// Stop if the point goes below the terrain
if (pozitie.y < 0) break;
}
return puncteTraiectorie;
}
void Tema1::Update(float deltaTimeSeconds) {
// Codul existent pentru redarea terenului și a tancului
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];
void Tema1::OnMouseMove(int mouseX, int mouseY, int deltaX, int deltaY)
{
// Add mouse move event
}
void Tema1::OnMouseBtnPress(int mouseX, int mouseY, int button, int mods)
{
// Add mouse button press event
}
void Tema1::OnMouseBtnRelease(int mouseX, int mouseY, int button, int mods)
{
// Add mouse button release event
}
void Tema1::OnMouseScroll(int mouseX, int mouseY, int offsetX, int offsetY)
{
}
void Tema1::OnWindowResize(int width, int height)
{
}
lanseaza proiectile dupa traiectorie
To launch projectiles following the calculated trajectory, you need to make use of a data structure that will represent the projectiles in flight. This structure can be updated every frame to move the projectiles based on their velocity and the gravitational force acting on them.
Let's enhance the OnKeyPress function to add a projectile to the list when the player presses the space bar. Each projectile will initially be positioned at the turret's tip and will follow the trajectory calculated based on the turret's current angle.
First, ensure you have a data structure for projectiles, like the one you seem to have defined in your code (Proiectil):
cpp
struct Proiectil {
glm::vec2 position;
glm::vec2 velocity;
bool active;
};
// Member variable in Tema1 class
std::vector<Proiectil> proiectile;
Next, in OnKeyPress, add a projectile to the vector when the spacebar is pressed:
cpp
void Tema1::OnKeyPress(int key, int mods)
{
if (key == GLFW_KEY_SPACE) {
// Calculate position and velocity of the projectile
float angle = turretAngle + getAngle(tankX); // Adjust for the turret's and tank's angles
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(vitezaProiectil * cos(turretAngle + angle), vitezaProiectil * sin(turretAngle + angle));
Proiectil newProiectil = { tipTurret, initialVelocity, true };
proiectile.push_back(newProiectil);
}
}
In the Update function, update each active projectile's position and render it:
cpp
void Tema1::Update(float deltaTimeSeconds) {
// Existing terrain and tank rendering code...
// Update and render projectiles
for (auto& proj : proiectile) {
if (!proj.active) continue;
// Update projectile position
proj.position += proj.velocity * deltaTimeSeconds;
// Apply gravity
proj.velocity.y -= gravitatie * deltaTimeSeconds;
// Deactivate projectile if it goes below the terrain
if (proj.position.y < CalculateTankY(proj.position.x)) {
proj.active = false;
}
// Render active projectile
if (proj.active) {
modelMatrix = glm::mat3(1);
modelMatrix *= transform2D::Translate(proj.position.x, proj.position.y);
RenderMesh2D(meshes["projectile"], shaders["VertexColor"], modelMatrix);
}
}
}
Remember to update and deactivate any projectiles that go below the terrain’s height. This will keep your list of proiectile clean and avoid rendering inactive projectiles. Adjust vitezaProiectil, gravitatie, and other parameters as needed to suit the desired physics behavior in your game.