Given a 2D-grid which is defined like below and is used to draw a grid consisting of cells:
int[,] grid; //filled with 0 (blocked) and 1 (walkable); I also have a A*-algorithm implemented which returns a List<Vector2> with the grid cell positions of the waypoints of the path. The actual screen-positions of the cells can be calculated using the cell width. So for example a Vector2 waypoint = new Vector2(0,2); is a Cell in the first row and third column.
I also have a Player class which holds and displays a texture. When I let the Player follow the path, the texture is moving towards the top waypoint in the List. Once the waypoint is reached with position == currentgoal //comparing Vector2 here I delete it from the list and set the next waypoint as the current goal.
Here is how I update the Player position, in this case it is moving down as the direction Vector2 is set to Vector2(0,1);
float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds; position += new Vector2(0,1) * speed * elapsedTime; //direction * float * float First I was using an integer as speed and omiting the elapsedTime in the calculation. As this is not how it is supposed to be implemented I changed the speed to float and added elapsedTime. Also with an speed value of int speed = 1; the texture was moving too fast already.
Unfortunately with the float values the Vector2 comparison of position and waypoint vectors is almost never exactly reached and therefore the condition never fulfilled. This is because the goal values although being floats always hold values with a 0 after the point.
What I tried is to check for a (+/-)1 value so if the position is somewhere between currentgoal +/- 1. This does lead to a inaccuracy and other side effects.
Is my position calculation a common and right way to do it? If so, how could I modify my code which is checking whether the goal is reached or not?