Jump to content
  • Advertisement
Sign in to follow this  
wilsq

Walking in any direction

This topic is 4845 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, I'm making a crimsonland like top-town shooting game. In the game, I'm trying to make the hero move freely in any direction (0-360), instead of just 4 or 8. The direction is controled by mouse. When player holds the [w] key, the hero starts moving towards the mouse pointer. I can make the hero always face the mouse pointer by rotating the sprite, but I can't figure out how to make the sprite move by a certain angle (e.g. 25 degrees to the north). Thankyou.

Share this post


Link to post
Share on other sites
Advertisement
trigonometric functions, ie sinus and cosinus, are your friend.

look them up and you can probably figure out how to use them better than i could explain it.

Share this post


Link to post
Share on other sites
Let's take your example of 25 degrees to the north. That means character is also walking 90 - 25 = 65 degrees to the east or west. Use trigonometry to figure out how much to move character in both cardinal directions:

movement factor for north = sin 25 degrees (OR cos 65 degrees) = 0.42 (approx)
movement factor for east or west = cos 25 degrees (OR sin 65 degrees) = 0.91 (approx)

Share this post


Link to post
Share on other sites
I'm assuming since you can rotate your sprite, you are somewhat familiar with trigonometry and vector math.

One thing you could try is to use 2d vectors to "encode" your character's movement. Start with a 2d vector pointing towards your initial direction. The head of the vector should always face the direction of movement. The size of the vector depends on your walk speed. You can start with a unit vector (length of 1) and just scale it to whatever your walk speed is. This can give you an easier way to change your character's speed by just scaling the vector. When you rotate your sprite, rotate your vector also. Then when the user presses [w] or your move forward button, just add the vector's components to the character's location. This will move the character in the proper direction. If you want to go backwards, then just subtract the vector from the location.

This solution may be a little more complicated than what you were looking for but I think you will like the results.

Share this post


Link to post
Share on other sites
thank you guys,
That refreshed my maind, because I've been thinking about using "line-drawing" algorithms. 2D vectors will work just fine! Thank you again!

-wilsq

Share this post


Link to post
Share on other sites
Now, there's a new problem. In theory, the 2d vector gives a ideal path, but on the screen, the smallest unit is 1 (1 pixel). There is a problem with converting a real number into pixels: say the x-factor is 0.42 and the y-factor is 0.91. In that case we can't simply round 0.42 in to nothing and 0.91 into 1, because that way, the sprite will move horizontally. So how is this problem solved with the vector algorithms?

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!