Diagonal Movement math

Hello, I got this movement function that was finished a few months back and it came to me that the “diagonal” movement is faster than the left,right,up,down. With my understanding it would be:

c^2 = a^2 + b^2

c^2 = (5)^2 + (5)^2

c = sqrt(25+25)

c = sqrt(50)

c = ~7

When I want them all to be 5 for example (while diagonal is actually moving 7!) when moving diagonal. Its registered to a circle “dpad” image on the screen. And I got a setLinearVelocity working with this function in my runtime and all that works fine np.

The movement part of the function:

if event.phase == "began" or event.phase == "moved" then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;local touchX = event.x - event.target.x &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;local touchY = event.y - event.target.y &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if touchX \< -15 then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;velX = -5 &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;sequence = "left" &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;elseif touchX \> 15 then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;velX = 5 &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;sequence = "right" &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;else &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;velX = 0 &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;end &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if touchY \< -15 then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;velY = -5 &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;sequence = "up" &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;elseif touchY \> 15 then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;velY = 5 &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;sequence = "down" &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;else &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;velY = 0 &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;end

How would I go about to make it properly register the proper movement on the diagonal parts?

EDIT: The character already moves diagonal and everything I just want the math to match up to the same speed on every movement, I’m aware of the sprite only registers 4 ways and that is all working as intended.

Thanks for your time as always.

Any type of advice or approach to take even just pseudo word theory is greatly appreciated, thanks.

Just off the top of my head I would think something like this might work?

local rad = math.atan2(event.x, event.y) local angle = rad \* 180 / math.pi if (angle \< 0) then angle = 360 + angle end

Then just check the angle like

if (angle \> 0 and angle \< 25) then sequence = "upperright" end --etc and so forth

didn’t check to even see if that would work but at first glance to your question it seems that since you are trying to build a dpad that you shouldn’t even really be looking at if it is left/right etc. but rather what the current angle (of your thumb) in coralation to the center of the dpad is?

never made a dpad so this is all just a guess :slight_smile:

Hey thanks for the response, I will try to calculate it using the angle. Yes I should not be looking at left/right etc but the movement is pretty much exactly the same as “Links Awakening”, where it uses diagonal movement and normal movement and just categorizes them into 4 sides, the reason I am following the only 4 sides is because of having only 4 different attack animations (left,right,up,down) that calls those sprites. It for sure would be amazing to figure out how to do attacks at any angle and proper physics, smoothness, animation of it but that is something I probably won’t get to.

Any type of advice or approach to take even just pseudo word theory is greatly appreciated, thanks.

Just off the top of my head I would think something like this might work?

local rad = math.atan2(event.x, event.y) local angle = rad \* 180 / math.pi if (angle \< 0) then angle = 360 + angle end

Then just check the angle like

if (angle \> 0 and angle \< 25) then sequence = "upperright" end --etc and so forth

didn’t check to even see if that would work but at first glance to your question it seems that since you are trying to build a dpad that you shouldn’t even really be looking at if it is left/right etc. but rather what the current angle (of your thumb) in coralation to the center of the dpad is?

never made a dpad so this is all just a guess :slight_smile:

Hey thanks for the response, I will try to calculate it using the angle. Yes I should not be looking at left/right etc but the movement is pretty much exactly the same as “Links Awakening”, where it uses diagonal movement and normal movement and just categorizes them into 4 sides, the reason I am following the only 4 sides is because of having only 4 different attack animations (left,right,up,down) that calls those sprites. It for sure would be amazing to figure out how to do attacks at any angle and proper physics, smoothness, animation of it but that is something I probably won’t get to.