@bballkiller wrote:
OK so I have a scenario where i can interact with NPCs, and send them to a position that I decided beforehand, the path that this NPC will take comes from the A* pathfinding algorythm in the form of a list of what i call "nodes" which are essentially Points to get to that final destination.
That being said, what I think is the correct way to do this is loop through all these points and move the character's position until it's reached this point's x and y. But it doesn't behave like that especially when the path has multiple turns or directional changes..
Anyway, my problem here is that the NPC's speed isn't constant during it's journey, maybe this is related to timers and stuff, i have troubles understanding this aspect.
Here's a gif of what's happening:
movement behaviour
the NPC is the one on the right, as you can see it starts fast and then slows down.now a bit of code:
move method in NPC class:
public const int millisecondsPerFrame = 200; ..... public void Move(GameTime gameTime) { foreach (var node in nodesToEnd) { timeSinceLastUpdate += (int)(gameTime.ElapsedGameTime.Milliseconds); if (timeSinceLastUpdate >= millisecondsPerFrame) { timeSinceLastUpdate = 0; if (Sprite.Hitbox.X < node.X)//right { Sprite.Direction = Direction.RIGHT; Sprite.MoveHitbox(gameTime, node.hitbox, Direction.RIGHT); } else if (Sprite.Hitbox.Y < node.Y)//down { Sprite.Direction = Direction.DOWN; Sprite.MoveHitbox(gameTime, node.hitbox, Direction.DOWN); } } } }
MoveHitbox method:
public void MoveHitbox(GameTime gameTime, Rectangle destination, Direction direction) { double delta = Globals.Speed * (gameTime.ElapsedGameTime.TotalMilliseconds / speedAdjustment); int newX = 0; int newY = 0; if (direction == Direction.RIGHT) { newX = (int)(_hitbox.X + delta); if (newX > destination.X) { _hitbox.X = destination.X; state = State.Idle; } else _hitbox.X += (int)(delta); } if (direction == Direction.LEFT) { newX = (int)(_hitbox.X - delta); if (newX < destination.X) { _hitbox.X = destination.X; state = State.Idle; } else _hitbox.X -= (int)(delta); } if (direction == Direction.DOWN) { newY = (int)(_hitbox.Y + delta); if (newY > destination.Y) { _hitbox.Y = destination.Y; State = State.Idle; } else _hitbox.Y += (int)(delta); } if (direction == Direction.UP) { newY = (int)(_hitbox.Y - delta); if (newY < destination.Y) { _hitbox.Y = destination.Y; State = State.Idle; } else _hitbox.Y -= (int)(delta); } }
Posts: 1
Participants: 1