• 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!

SOLVED Smooth Rotation using keys

Can anyone help with smooth rotation using the keyboard? so far I have:

GML:
moveUp = keyboard_check(ord("W"));
moveDown = keyboard_check(ord("S"));
moveRight = keyboard_check(ord("D"));
moveLeft = keyboard_check(ord("A"));

var _horMove = moveRight - moveLeft;
var _verMove = moveDown - moveUp;


if ((_horMove != 0) || (_verMove != 0)) {
     var pdir = point_direction(0, 0, _horMove, _verMove);
     facing += sin(degtorad(pdir - image_angle)) * tankRotSpeed;;
}


hspd = (moveRight - moveLeft) * moveSpeed;
vspd = (moveDown - moveUp) * moveSpeed;

x += hspd;
y += vspd;
With this, if push left or right, nothing happens but if i push up or down the player instance spins in circles.

Looking for the instance to smoothly rotate from its current angle to 90 if going up, 180 if going left etc etc...

Many thanks.
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Use the angle_difference function...

GML:
if ((_horMove != 0) || (_verMove != 0))
{
var pdir = point_direction(0, 0, _horMove, _verMove);
var _diff = angle_difference(facing, pdir); // The arguments here might need to be reversed... I always forget the order!
facing += median(-tankRotSpeed, _diff, tankRotSpeed);
}
 
Use the angle_difference function...

GML:
if ((_horMove != 0) || (_verMove != 0))
{
var pdir = point_direction(0, 0, _horMove, _verMove);
var _diff = angle_difference(facing, pdir); // The arguments here might need to be reversed... I always forget the order!
facing += median(-tankRotSpeed, _diff, tankRotSpeed);
}
Yes, you were right, I had to change them around, however, it's perfect, much appreciated...
 
Last edited:
Top