🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

moving sprite on a hex grid

Started by
73 comments, last by Tom Sloper 1 year, 11 months ago

@pbivens67 :

if(rotate_tank==3) 
{
		drawTank_one = false;
		Xloc++;
		HexType = HexGrid[Yloc][Xloc];
		if (HexType == 1)
		{
		      move_x += 15.0f;
		}
		cout << Yloc << " " << Xloc << " " << HexType << endl;
}

One problem with the above code is that you increment the Xloc whether the new location is acceptable or not. You do not want to increment Xloc unless you actually want to move the tank. But to find out if you want to move the tank you have to test the new grid location first. . .

So, I would think you'd want something more like this instead :

if(rotate_tank==3) 
{
		drawTank_one = false;
		
		// we want to test a new location to see if it is good for a move.
		
		
// get the x value for the proposed new location
		int TestXLoc = Xloc + 1;
		
		// find out what HexType is at that new location. 
		HexType = HexGrid[Yloc][TestXLoc];
		
		// we can output what we've got that the new location
		cout << Yloc << " " << TestXLoc << " " << HexType << endl;		

		
		if (HexType == 1) // this means the new location is good to move to
		{
			 // now we can actually update the tank position
			 Xloc = TestXLoc; 
			 
		     move_x += 15.0f; // what is move_x doing?
		}

}

Advertisement

I am trying to move the tank diagonally, but it does not move in that direction.

		if (rotate_tank == 5)
		{
		drawTank_five = false;
		int TestXloc = Xloc - 1;
		int TestYloc = Yloc - 1;
		HexType = HexGrid[TestYloc][TestXloc];
		cout << Yloc << " " << TestXloc << " " << HexType << endl;
		if (HexType == 1)
		{
		Xloc = TestXloc;
		Yloc = TestYloc;
		move_x -= 8.0f;
		move_y -= 13.5f;
		}
		}
 

@pbivens67 : Hmm. That code looks good to me.

So we try to figure out what is wrong with it. . . This is really a process of elimination.

My first question : does the tank not move at all? Or is it moving, but just not diagonally ?

If it is not moving at all, then is that part of the code even executing? You start out with “if(rotate_tank == 5)” maybe it is never equal to 5?

Or maybe when you have “HexType = HexGrid[TestYloc][TestXloc];” HexType is never equal to 1, so it might not be moving for that reason. To test this stuff, I'd add some new output messages in your code, as follows below (this way you'll see what the code is doing) : (you could also do this same type of stuff in the debugger)

if (rotate_tank == 5) 
{ 
      // this is so we know we've actually successfully entered this part of the code
	  cout << "rotate tank == 5, testing diagonal move" << endl;
	  
      drawTank_five = false; 
      int TestXloc = Xloc - 1; 
      int TestYloc = Yloc - 1; 
      HexType = HexGrid[TestYloc][TestXloc]; 
      cout << TestYloc << " " << TestXloc << " " << HexType << endl; 
      if (HexType == 1) 
      { 
           cout << "HexType == 1, executing move" << endl;
           Xloc = TestXloc; 
           Yloc = TestYloc; 
           move_x -= 8.0f; 
           move_y -= 13.5f; 
      } 
      else
      {
           cout << "HexType != 1, not doing move" << endl;
      }
}

Add the new messages, run the program, and get back to me with the results, And also answer my question about whether the tank is moving at all. I'll know more about the situation then, and can give you some better advice.

it does not move diagonally

@pbivens67 : so those particular lines of code, that go off “if(rotate_tank == 5)” do move the tank, but not in a diagonal direction.

So how does the tank move when you try to move it diagonally?

I'm guessing that it still moves on the x axis, but not on the y axis. Am I correct?

it does not move on y-axis

@pbivens67 : So it is moving on the x-axis when you try to move diagonally. And according to the code you alter both x and y values.

An obvious possibility is that you might not be using the y values correctly when you are drawing the tank. Thus, even though these values change, the tank's position doesn't reflect that alteration.

Can you show me the code that you use when drawing the tank sprite?

here is my code

void drawTankSouthWest()
{
	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, texture[0]);
	glBegin(GL_QUADS);

	glTexCoord2f(0.0f, 0.0f);
	glVertex2f(0.0f + move_x, 5.0f + move_y);

	glTexCoord2f(1.0f, 0.0f);
	glVertex2f(5.0f + move_x, 0.0f + move_y);

	glTexCoord2f(1.0f, 1.0f);
	glVertex2f(0.0f + move_x, -5.0f + move_y);

	glTexCoord2f(0.0f, 1.0f);
	glVertex2f(-5.0f + move_x, 0.0f + move_y);

	glEnd();
	glDisable(GL_TEXTURE_2D);
}

@pbivens67 : that all looks good to me. However, there has to be an error in there somewhere, otherwise your tank would be moving in the y direction. . .

I'd like to see the values of move y before and after you do your case GLUT_KEY_UP code. And then find what the value of move y is when you call your draw function.

Two possibilities for errors occur to me. . .

(A) Are you sure that the drawTankSouthWest() function is getting called?

(B) I assume that “move y” is a global variable, is it possible that you've got a local version of it in your key_up function? You can have different variables of the same name. And, if the move y you're changing there is actually a different variable, then altering its value will not have an effect in the other function.

As an important side question, is there a specific reason why you have a drawTankSouthWest() function? I'd suggest that maybe it just be DrawTank() instead. Then you would draw the tank at whatever location it is currently at.

I assume that, if you have a drawTankSouthWest() function, you probably also have functions for the other 5 possible directions. Having 6 different draw functions is unnecessary to draw one tank. If there are errors, you might have to fix 6 different draw functions instead of one function. It also opens the possibility that you are somehow calling the wrong function.

thanks, warp9 I have got my tank to move along a path YEAH!!!!

This topic is closed to new replies.

Advertisement