To change the color of tank2 without affecting tank1, you need to modify the RenderTank function call specifically for tank2. Here's how you can do that:
Inside the Update function, when rendering tank2, pass a different color to the RenderTank function.
Here's the specific change you need to make to the Update function in your code:
To ensure that tank2 has a separate and distinct color from tank1, you need to make sure that each RenderTank call uses a different color parameter. The RenderTank function already accepts a glm::vec3 color parameter, so you need to pass different colors when calling this function for each tank.
Let's check the RenderTank calls to confirm distinct colors are being passed:
User
but i think i need to change something in init too
Assistant
}
float CalculateSlopeAngle(glm::vec2 A, glm::vec2 B) {
glm::vec2 V = B - A;
return atan2(V.y, V.x);
}
float CalculateTankY(float tankX, glm::vec2 A, glm::vec2 B) {
float t = (tankX - A.x) / (B.x - A.x);
return A.y + t * (B.y - A.y);
}
bool CheckProjectileCollision(const glm::vec2& projectilePos, const std::vectorglm::vec2& terrain, float collisionThreshold) {
for (size_t i = 1; i < terrain.size(); ++i) {
glm::vec2 P1 = terrain[i - 1];
glm::vec2 P2 = terrain[i];
}
std::vectorglm::vec2 CalculateProjectilePath(glm::vec2 startPosition, float angle, float initialSpeed, float gravity, int numSegments) {
std::vectorglm::vec2 trajectory;
float timeStep = 0.1f; // Intervalul de timp dintre fiecare punct
for (int i = 0; i < numSegments; i++) {
float t = i * timeStep;
float x = startPosition.x + initialSpeed * cos(angle) * t;
float y = startPosition.y + initialSpeed * sin(angle) * t - 0.5f * gravity * t * t;
trajectory.push_back(glm::vec2(x, y));
if (y < 0) {
break;
}
}
return trajectory;
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)
{
}
With this:
if (tank2X >= prev.x && tank2X <= curr.x) {
glm::vec2 A = prev;
glm::vec2 B = curr;
tank2Y = CalculateTankY(tank2X, A, B);
rotationAngle2 = CalculateSlopeAngle(A, B);
glm::vec3 tank2Color = glm::vec3(1.0f, 0.4f, 0.7f); // New color for tank2
RenderTank(tank2X, tank2Y, rotationAngle2, turret2Angle, tank2Color);
turretTip2X = turretTipX;
turretTip2Y = turretTipY;
float angle = turret2Angle + rotationAngle2 - glm::radians(-45.0f);
RenderTrajectory(glm::vec2(turretTip2X, turretTip2Y), angle, 400.0f, gravity);
}
In this modification, I created a new glm::vec3 variable tank2Color for tank2 with the color (1.0f, 0.4f, 0.7f). This ensures that each tank retains its color independently.
// For Tank 1
if (tank1X > prev.x && tank1X <= curr.x) {
glm::vec2 A = prev;
glm::vec2 B = curr;
tank1Y = CalculateTankY(tank1X, A, B);
rotationAngle1 = CalculateSlopeAngle(A, B);
glm::vec3 tank1Color = glm::vec3(0.6f, 0.4f, 0.2f); // Color for tank1
RenderTank(tank1X, tank1Y, rotationAngle1, turret1Angle, tank1Color);
turretTip1X = turretTipX;
turretTip1Y = turretTipY;
float angle = turret1Angle + rotationAngle1 - glm::radians(-45.0f);
RenderTrajectory(glm::vec2(turretTip1X, turretTip1Y), angle, 400.0f, gravity);
}
// For Tank 2
if (tank2X >= prev.x && tank2X <= curr.x) {
glm::vec2 A = prev;
glm::vec2 B = curr;
tank2Y = CalculateTankY(tank2X, A, B);
rotationAngle2 = CalculateSlopeAngle(A, B);
glm::vec3 tank2Color = glm::vec3(1.0f, 0.4f, 0.7f); // Distinct color for tank2
RenderTank(tank2X, tank2Y, rotationAngle2, turret2Angle, tank2Color);
turretTip2X = turretTipX;
turretTip2Y = turretTipY;
float angle = turret2Angle + rotationAngle2 - glm::radians(-45.0f);
RenderTrajectory(glm::vec2(turretTip2X, turretTip2Y), angle, 400.0f, gravity);
}
In this example, ensure that you:
Define a color specifically for tank1 (e.g., tank1Color).
Define a different color specifically for tank2 (e.g., tank2Color).
Make sure these colors are different, as shown in the example above. If you follow this pattern, tank1 should render with one color, while tank2 will render with a distinct color. Adjust the RGB values in glm::vec3 as needed to achieve the desired colors for each tank.
If the issue persists and you believe changes are needed in the Init function as well, it might be related to the mesh creation for the tank components. In your Init function, each mesh is created with a color, and this color may need to be customized to distinguish between tank1 and tank2.
In your Init function, there are currently meshes for tank components created with specific colors:
These mesh names (baseTrapezoid1, baseTrapezoid2, etc.) seem to be shared. If these are used for both tanks without distinguishing them, updating the color in the RenderTank function won't affect the existing meshes significantly, unless correctly modified.
Here's what you could do to adjust the code by properly handling unique copies or instances specifically for each tank's color:
Create separate meshes for each tank's components if custom colors are needed:
You can have separate base trapezoid meshes for each tank specifically if needed.
Modify Init function to create distinct mesh colors for tank2 if necessary:
Consider modifying the Init function like this:
// For tank1
Mesh* baseTrapezoid1Tank1 = object2D::CreateTrapezoid("baseTrapezoid1Tank1", 110, 20, 100, glm::vec3(0.6f, 0.4f, 0.2f), true);
AddMeshToList(baseTrapezoid1Tank1);
// For tank2 - Use different color
Mesh* baseTrapezoid1Tank2 = object2D::CreateTrapezoid("baseTrapezoid1Tank2", 110, 20, 100, glm::vec3(1.0f, 0.4f, 0.7f), true);
AddMeshToList(baseTrapezoid1Tank2);
// Do similar setup for other parts if necessary...
Ensure the RenderTank function uses separate mesh names when rendering each tank.
Update the RenderTank calls to use the specific meshes you created for each tank:
// Example for tank1
RenderMesh2D(meshes["baseTrapezoid1Tank1"], shaders["VertexColor"], baseMatrix);
// Example for tank2 - using its specific mesh with a different color
RenderMesh2D(meshes["baseTrapezoid1Tank2"], shaders["VertexColor"], baseMatrix);
Make sure each mesh (like base, turret, etc.) is created with distinct names and colors as necessary to ensure they don't share the same mesh object across different tanks if separate colors are important. This approach provides direct control over the rendering color of each tank's geometry.