• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GML [SOLVED] Efficient Angle Check - 359 and 0 degrees

Qlak

Member
Hey guys!

I'm having hard time when it comes to checking angles.
I managed to achieve what I wanted, but I'm sure that there must be a better way, because my code is not clean for sure.

Maybe you could help me out :):
-For instance, imagine that you have traditional top-up 2d shooter, left stick is player movement and right stick is aiming.
-You can get angle of each joystick with code like:
GML:
var rightStickHorizontal = gamepad_axis_value(controllerNumber, gp_axisrh);
var rightStickVertical = gamepad_axis_value(controllerNumber, gp_axisrv);
rightStickAngle = point_direction(0, 0, rightStickHorizontal, rightStickVertical);

var leftStickHorizontal = gamepad_axis_value(controllerNumber, gp_axislh);
var leftStickVertical = gamepad_axis_value(controllerNumber, gp_axislv);
leftStickAngle = point_direction(0, 0, leftStickHorizontal, leftStickVertical);
-It gives you the angle of each joystick in degrees 0-359.

Lets say that you want character to be faster, when running the same way he looks at with +-20 degree margin (so when both sticks point more or less the same angle).
In theory you can achieve it this way:
GML:
if(leftStickAngle < (rightStickAngle + 20) && leftStickAngle > (rightStickAngle - 20)){ 
    maxMovementSpeed = runningSpeed; 
}
It works perfectly untill you get to 19 or 340 degrees, because after it reaches 360 it becomes 0.

I managed to do it in far too many lines of code, and I was hoping you can show me simpler solution to this problem?
I believe it's something rather common. How do you handle this 359-0 angle change?

Thanks for your help in advance! :D
 
Last edited:

curato

Member
why not do something like this
GML:
if(abs(leftStickAngle - rightStickAngle) < 20){
    maxMovementSpeed = runningSpeed;
}
 
Last edited:

Qlak

Member
why not do something like this
GML:
if(abs(leftStickAngle - rightStickAngle) < 20)){
    maxMovementSpeed = runningSpeed;
}
Cool approach, looks much better, but it still does not work properly.
When one of the sticks is below 359 (pointing right, example 356) and the other is above 0 (also pointing right, example: 10), then running stops working as well.
 

curato

Member
what you most likely want to do there is write a script that takes the difference in the angles and if it is more than 180 subtracts 360 from that and if it is less than 180 then add 360 to it other it would just be the difference then just take the abs on that number and if you have the actual difference. I can whip it up later if you need more help.
 

Qlak

Member
Have you considered using angle_difference?
Great tip mate! Cheers!
This is EXACTLY what I was looking for. Can't believe I did not find this function earlier...

I combined your answer with curato's one and it works perfectly, solves all problems and is in 3 lines instead of 15.

GML:
if(abs(angle_difference(leftStickAngle, rightStickAngle)) < 20){   
    maxMovementSpeed = 4;   
}
Thanks guys!
šŸ» :D
 
Top