Advertisement

move tank sprite

Started by April 18, 2022 10:01 PM
5 comments, last by LorenzoGatti 2 years, 7 months ago

I have got a tank sprite to rotate left and right but I want it to move up and down and left and right. here is the code I am working on.

void tankmove()
{
	glEnable(GL_TEXTURE_2D);

	glBindTexture(GL_TEXTURE_2D, texture[0]);

	if (direction[1] == 1)
	{
	glBindTexture(GL_TEXTURE_2D, texture[1]);
	direction[1] = 0;
	}

	if (direction[2] == 1)
	{
	glBindTexture(GL_TEXTURE_2D, texture[2]);
	direction[2] = 0;
	}

	if (direction[3] == 1)
	{
	glBindTexture(GL_TEXTURE_2D, texture[3]);
	direction[3] = 0;
	}

	if (direction[4] == 1)
	{
	glBindTexture(GL_TEXTURE_2D, texture[0]);
	direction[4] = 0;
	}

	if (direction_two[1] == 1)
	{
		glBindTexture(GL_TEXTURE_2D, texture[3]);
		direction_two[1] = 0;
	}

	if (direction_two[2] == 1)
	{
		glBindTexture(GL_TEXTURE_2D, texture[2]);
		direction_two[2] = 0;
	}

	if (direction_two[3] == 1)
	{
		glBindTexture(GL_TEXTURE_2D, texture[1]);
		direction_two[3] = 0;
	}

	if (direction_two[4] == 1)
	{
		glBindTexture(GL_TEXTURE_2D, texture[0]);
		direction_two[4] = 0;
	}

	glBegin(GL_QUADS);
	glTexCoord2f(0.0f, 0.0f);
	glVertex2f(5.0f, 5.0f);
	glTexCoord2f(1.0f, 0.0f);
	glVertex2f(-5.0f, 5.0f);
	glTexCoord2f(1.0f, 1.0f);
	glVertex2f(-5.0f, -5.0f);
	glTexCoord2f(0.0f, 1.0f);
	glVertex2f(5.0f, -5.0f);
	glEnd();
	glDisable(GL_TEXTURE_2D);
}

What is the problem? What is working versus not working?

What's the bit you're unsure about? What have you tried, and why didn't that work?

enum Bool { True, False, FileNotFound };
Advertisement

Change the coordinates where you paint the tank.

Well I have got the tank to move north and south but when I rotate the tank and try to move it west and east it snaps back to move north and south.

here is my updated code

void tankmove()
{
	glEnable(GL_TEXTURE_2D);

	glBindTexture(GL_TEXTURE_2D, texture[0]);

	if (direction[1] == 1)
	{
	glBindTexture(GL_TEXTURE_2D, texture[1]);
	direction[1] = 0;
	}

	if (direction[2] == 1)
	{
	glBindTexture(GL_TEXTURE_2D, texture[2]);
	direction[2] = 0;
	}

	if (direction[3] == 1)
	{
	glBindTexture(GL_TEXTURE_2D, texture[3]);
	direction[3] = 0;
	}

	if (direction[4] == 1)
	{
	glBindTexture(GL_TEXTURE_2D, texture[0]);
	direction[4] = 0;
	}

	if (direction_two[1] == 1)
	{
		glBindTexture(GL_TEXTURE_2D, texture[3]);
		direction_two[1] = 0;
	}

	if (direction_two[2] == 1)
	{
		glBindTexture(GL_TEXTURE_2D, texture[2]);
		direction_two[2] = 0;
	}

	if (direction_two[3] == 1)
	{
		glBindTexture(GL_TEXTURE_2D, texture[1]);
		direction_two[3] = 0;
	}

	if (direction_two[4] == 1)
	{
		glBindTexture(GL_TEXTURE_2D, texture[0]);
		direction_two[4] = 0;
	}

	glBegin(GL_QUADS);
	glTexCoord2f(0.0f, 0.0f);
	glVertex2f(5.0f, 5.0f + move_tank);
	glTexCoord2f(1.0f, 0.0f);
	glVertex2f(-5.0f, 5.0f + move_tank);
	glTexCoord2f(1.0f, 1.0f);
	glVertex2f(-5.0f, -5.0f + move_tank);
	glTexCoord2f(0.0f, 1.0f);
	glVertex2f(5.0f, -5.0f + move_tank);
	glEnd();
	glDisable(GL_TEXTURE_2D);
}
   

here is a screenshot

https://imgur.com/a/RIO6xYt

  • Your screenshot seems fine: you are rendering your tank sprite correctly with a “tank facing up” texture and coherent vertex and texture coordinates. So what's the problem?
  • The cluster of if blocks at the beginning is unacceptably verbose. Reorganize your control logic to call glBindTexture only once rather than up to 9 times in 9 places. The arrays seem related to user input or AI logic, none of the business of a function that does OpenGL calls: it would be better to pass the computed facing as a parameter.
  • The tank position is fixed, so how can you call this function “tankMove”? It should be named something like “renderTank”, and probably generalized to arbitrary sprites with arbitrary facing without using global variables for textures and directions.
  • How does your tank actually move on the screen? A translation transform somewhere else in the code? And how do you track tank state (position and facing)?

Omae Wa Mou Shindeiru

This topic is closed to new replies.

Advertisement