Jump to content
  • Advertisement
Sign in to follow this  

Movement animation in hexagon map

This topic is 2793 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys!

I'm trying to create a hexmap based game, like Panzer General, well actually I'm trying to animate unit movement.
And this is where I have problem. I am using A* algorithm to determine the path, so that is not problem.
My problem is that image I'm animating is always a couple of pixels on X-axis late.

This is how I calculate the movement:

int SPEED = 1;
int SOUTH_EAST = 300;

double newX = Math.cos(Math.toRadians(SOUTH_EAST));
double newY = Math.sin(Math.toRadians(SOUTH_EAST));
this.speedX = SPEED * newX;
this.speedY = -SPEED * newY;
// later in code inside each frame
this.x += this.speedX;
this.y += this.speedY

// drawing of image inside each frame
g.drawImage(this.tileImage, (int) this.x, (int) this.y, (int) this.x + TILE_SIZE, (int) this.y + TILE_SIZE, frameX, frameY, frameX + TILE_SIZE, frameY + TILE_SIZE, observer);

The problem is that those 300 degrees is from 0,0 to 39,39 (My tile size is 40) which is diagonal of the square inside which the hexagon is, and I need a little sharper angle since I'm not working with squares but hexagons. Ok, I know I need to provide correction for x and y, but that is not the point, point is that my movement by X-axis is late and if I put the angle of 305 that is too much.

Well my question is, how am I suppose to animate the unit movement from one hextile to another?
Is my approach good and I just need to do a better math calculation (I'm terrible in math, BTW) or is there some other approach?

Share this post

Link to post
Share on other sites
I'm trying to visualise your problem, but I'm failing at it smile.png
How is your hex grid oriented? Do the hexes form straight rows (x axis) or columns (y axis)? How do you measure the angle 300?
A little simple picture would help a lot.


Share this post

Link to post
Share on other sites
Hi Tom, thanks for the reply.
Here's the picture you asked.

I imagine there is a circle inside the hexagon. and since all the sides are the same I just divide 360 / 6. and I get 60 degrees for each direction.

Share this post

Link to post
Share on other sites
The angle -60 (or 300) should be absolutely fine and accurate.

You say the movement is behind only on the x axis? It reaches a point located left of the desired target point? I really cannot imagine how would that happen. Is the grid drawn properly, isn't the problem actually with how the grid looks and not with the movement?

Or, is it possible to make some screenshot of the exact problem you are getting?

Share this post

Link to post
Share on other sites
The hexes definitely aren't rendered properly, they are stretched - they are too wide. You cannot draw an inscribed or circumscribed circle in/around the hex.

I think the tank moves properly (it seems so but it's hard to tell), but the displayed hex grid doesn't correspond to it, the tiles are too short vertically, thus the tank ends far away below.

When I'm examining your grid more, I think I know what you did wrong. It seems that you misunderstood how a hex should look like.
Check the picture, left side shows how it should look, right side is your case.
Drawn in red is an inscribed circle and in green a square - you managed to fit your hex into a square, but that's not right ;)


Share this post

Link to post
Share on other sites
Yes, Tom you are correct, I did it the way showed on the right side.
When I was reading following articles:

I got the impression that's how it's suppose to be. Especially the second one.

So, in order to fix my problem, I need to draw smaller tank or draw different hexes? Because my hexes are 40x40 px.
I thought that when I draw a hex into square, the hex will have all sides the same. In your case certain sides of the hex are bigger than others.

Share this post

Link to post
Share on other sites
I'm glad I could help. Please let me know when you get it working right, or feel free to ask should you have any further questions/problems.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!