Diagonal movement issue

59 views
Skip to first unread message

JustGabe

unread,
Nov 10, 2015, 10:26:27 PM11/10/15
to HaxeFlixel
The problem is simple, if I press UP the char moves 2 pixels up, while LEFT will make it movev 2 pixels left. However when I press both UP and LEFT (or any diagonal movement), the char seems to move faster in the diagonal direction. Any practical way to fix it so that it seems that the char moves at the same speed in any direction?

MegaLeon

unread,
Nov 11, 2015, 5:14:46 AM11/11/15
to HaxeFlixel
You could just get the direction and apply a uniform speed, or clamp the player speed directly.

Dlean Jeans

unread,
Nov 11, 2015, 6:44:32 AM11/11/15
to HaxeFlixel
Maybe this tutorial will help: http://haxeflixel.com/documentation/part-iv-sprites-and-animation/
It uses the function FlxAngle.rotatePoint(speed, 0, 0, 0, angle, velocity)
See the function here: http://api.haxeflixel.com/flixel/util/FlxAngle.html?#rotatePoint

Eric Grice

unread,
Nov 11, 2015, 10:16:51 AM11/11/15
to haxef...@googlegroups.com

You need to use a little algebra to determine the actual amount of pixels the player should traverse when moving diagonally.  As you've described, your player is actually moving FOUR pixels worth of distance when moving diagonally as opposed to just two when moving up down left or right.

There should be built-in method for handling this, I can't remember of the top of my head.  I think somebody mentioned the tutorial, which is a great place to start and definitely the way to go.  However if you want to understand the actual solution, read on.

What you want to use for calculating diagonal movement is the Pythagoras theorem.  In your case, you know you want to move 2px in a diagonal direction.  What you need to figure out is how many pixels left/right and up/down that equates to.  So 2px is your hypotenuse and the other sides are equal.

Let's call the unknown sides "a" since we know they're equal.

a^2 + a^2 = 2^2
2a^2 = 4
a^2 = 2
a = √2
a = 1.4142...

So you can round that to whatever precision you want, but essentially when you move diagonally that's how many pixels your player should go.  Hope that explains things for you!  Feel free to ask questions :-)

On Nov 10, 2015 10:26 PM, "JustGabe" <gabrie...@gmail.com> wrote:
The problem is simple, if I press UP the char moves 2 pixels up, while LEFT will make it movev 2 pixels left. However when I press both UP and LEFT (or any diagonal movement), the char seems to move faster in the diagonal direction. Any practical way to fix it so that it seems that the char moves at the same speed in any direction?

--
HaxeFlixel Development Community
See our github https://github.com/haxeflixel/ and our documentation http://haxeflixel.com/documentation/
---
You received this message because you are subscribed to the Google Groups "HaxeFlixel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to haxeflixel+...@googlegroups.com.
Visit this group at http://groups.google.com/group/haxeflixel.
To view this discussion on the web visit https://groups.google.com/d/msgid/haxeflixel/609102d8-7b32-4951-bfa9-425312087ae1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages