πŸŽ‰ 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

he has been very helpful; I will look at his previous posts.

Advertisement

@Tom Sloper : Thanks Tom. Although I should say that there is no frustration on my part here. My computer doesn't understand anything. So, when I program, I have to break things down into the most simple steps, and detail everything. Because of that, I'm used to being patient about these things.

My real concern is that there is a difference between, giving someone a fish, and teaching them to fish.

In the long run, teaching someone to fish is much better than simply giving them a fish. Simply giving somebody the answers is more like giving them the fish. The goal should be to teach people how to find their own answers (if that is possible).

@pbivens67 : I'm always glad to help. Although, what I've given you so far should be enough to get you where you need to be.

thank you for teaching me to fish

pbivens67 said:

thank you for teaching me to fish

I want to see the fish.

πŸ™‚πŸ™‚πŸ™‚πŸ™‚πŸ™‚<←The tone posse, ready for action.

I have stubbed out some code to determine the distance between hexes that are flat topped, unfortunately my hex board is point topped, how do I convert my code to work with pointy topped hexes.

#include <iostream>
#include <math.h>

using namespace std;

class Point
{
public:
	float x = 0.0f;
	float y = 0.0f;
};

int ComputeDistanceHexGrid(const Point & A, const Point & B);

int main()
{
	Point pointA;
	Point pointB;
	
	pointA.x = 0;
	pointA.y = 0;
	pointB.x = 1;
	pointB.y = -1;

	cout << ComputeDistanceHexGrid(pointA,pointB);
	
	return 0;
}

int ComputeDistanceHexGrid(const Point & A, const Point & B)
{
	Point distance;
	distance.x = A.x - B.x;
	distance.y = A.y - B.y;
	Point diagonalMovement;
	int lesserCoord = abs(distance.x) < abs(distance.y) ? abs(distance.x) : abs(distance.y);
	diagonalMovement.x = (distance.x < 0) ? -lesserCoord : lesserCoord;
	diagonalMovement.y = (distance.y < 0) ? -lesserCoord : lesserCoord;
	Point straightMovement;
	straightMovement.x = distance.x - diagonalMovement.x;
	straightMovement.y = distance.y - diagonalMovement.y;
	size_t straightDistance = abs(straightMovement.x) + abs(straightMovement.y);
	size_t diagonalDistance = abs(diagonalMovement.x);
	if ((diagonalMovement.x < 0 && diagonalMovement.y>0) || (diagonalMovement.x > 0 && diagonalMovement.y < 0))
	{
		diagonalDistance *= 2;
	}
	return straightDistance + diagonalDistance;
} 

I found this on stack overflow. Check out the accepted answer, might make things easier.

https://stackoverflow.com/questions/5084801/manhattan-distance-between-tiles-in-a-hexagonal-grid

πŸ™‚πŸ™‚πŸ™‚πŸ™‚πŸ™‚<←The tone posse, ready for action.

thanks fleabay

well, I have got my code to work, I have initialized the hex grid and then checked to see where I am at and to find out what kind of hex I am moving to, thanks for all the help.

@pbivens67 : That's great to hear!

This topic is closed to new replies.

Advertisement