Nathan Laing
Member
Hello all,
Straight up, I know this topic has been done, and done again, but perhaps my take on it is different, perhaps not. I have tried some different things, and have one or two ideas (which may or may not work), but I'd like to see a beautiful or elegant way. But ultimately, Any help greatly appreciated.
I'm moving an object forwards, backwards, sideways(left & right), in relation to the object's pointing direction--(for example's sake, the mouse). I am not using any built-in variables, (and do not wish to).
All works appropriately, except when both a forward|backward key is pressed together with a left|right key.
What happens is the object moves faster than spd == 2, because obviously the x +=, and y-= code is run twice.
How can I write or re-arrange the code to limit the final adjustments to x and y, to spd?
Straight up, I know this topic has been done, and done again, but perhaps my take on it is different, perhaps not. I have tried some different things, and have one or two ideas (which may or may not work), but I'd like to see a beautiful or elegant way. But ultimately, Any help greatly appreciated.
I'm moving an object forwards, backwards, sideways(left & right), in relation to the object's pointing direction--(for example's sake, the mouse). I am not using any built-in variables, (and do not wish to).
All works appropriately, except when both a forward|backward key is pressed together with a left|right key.
What happens is the object moves faster than spd == 2, because obviously the x +=, and y-= code is run twice.
How can I write or re-arrange the code to limit the final adjustments to x and y, to spd?
Code:
// where spd == 2
// where radians is a degree value of the direction to point/move in, converted to radians
// FORWARD KEY PRESS
xx = cos(radians) * spd;
yy = sin(radians) * spd;
x += xx;
y -= yy;
// BACKWARD KEY PRESS
xx = cos(radians) * spd;
yy = sin(radians) * spd;
x -= xx;
y += yy;
// SIDEWAYS LEFT KEY PRESS
xx = cos(radians) * spd;
yy = sin(radians) * spd;
x -= xx;
y += yy;
// SIDEWAYS RIGHT KEY PRESS
xx = cos(radians) * spd;
yy = sin(radians) * spd;
x -= xx;
y += yy;
Last edited: