Advanced RPG-style movement in XNA

I am making a top-down game like rpg - similar to Pokemon, but I'm stuck in the character’s movement. Basically, what I want to achieve is a smooth tile-based movement system for my player and other people on the map. Has anyone managed to do this effectively? If so, how?

+4
source share
2 answers

If you use a mesh system such as Pokémon, you can achieve this by setting the speed at which they can move between the plates.

For example, if you made it so that they could move one tile per second, you can use (float)gameTime.ElapsedGameTime.TotalSeconds in your Update function to determine how quickly the game updates. From this, you should be able to make the character move ((float)gameTime.ElapsedGameTime.TotalSeconds * TileSize) every update.

This would mean that if they have 4 updates per second (very slow, but, for example,), they will move 1/4 of the distance on each plate each update and reach the end point by the end of the movement period (1 second in this case ) In this situation, if you had 32 pixel tiles, they would move 0.25 * 32 every update or 8 pixels.

Hope this helps.

+8
source

The “technical” word for what you want is possibly “interpolation”.

I think the simplest thing you want to do is interpolate the position of the device between the start point (in the middle of the start tile) and its end point (middle of the finish tile) over the period of time that you want to give it movement.

The formula is something like this:

 float k = timeSinceStarted / durationOfMovement; pos.X = startPos.X + (endPos.X - startPos.X) * k; pos.Y = startPos.Y + (endPos.Y - startPos.Y) * k; 

In a game, it is probably best to pre-calculate the delta and add increments instead of maintaining the starting position. Something like this (Note: I have not double-checked this calculation).

 delta = endPos - startPos; position += delta * (timeSinceLastUpdate / durationOfMovement); 
+2
source

Source: https://habr.com/ru/post/1346804/


All Articles