So, in the little game I’m developing the character moves around with his front facing the mouse. The character is in the center of the screen and the tiled background updates behind him according to how he moves.
If he moves forwards or backwards, he moves toward the mouse or away from it. That part works fine. The problem is I can’t get the left/right or a/d strafing movement to work right. Here are the methods I’m using:
public int move_directional_x_2(int quadrant, int r, double gamma, boolean left) {
int xa = 0;
if (quadrant == 1) {
gamma = gamma + Math.PI / 2;
if (left) xa -= r * Math.cos(gamma);
else xa += r * Math.cos(gamma);
return xa;
//ya -= r * Math.sin(gamma);
}
if (quadrant == 2) {
gamma = gamma - Math.PI / 2;
if (left) xa -= r * Math.cos(gamma);
else xa += r * Math.cos(gamma);
return xa;
//ya -= r * Math.sin(gamma);
}
if (quadrant == 3) {
gamma = gamma - Math.PI;
if (left) xa += r * Math.cos(gamma);
else xa -= r * Math.cos(gamma);
return xa;
//ya += r * Math.sin(gamma);
}
if (quadrant == 4) {
gamma = gamma - Math.PI * 1.5;
if (left) xa += r * Math.cos(gamma);
else xa -= r * Math.cos(gamma);
return xa;
//ya += r * Math.sin(gamma);
}
return xa;
}
public int move_directional_y_2(int quadrant, int r, double gamma, boolean left) {
int ya = 0;
if (quadrant == 1) {
gamma = gamma + Math.PI / 2;
if (left) ya -= r * Math.sin(gamma);
else ya += r * Math.sin(gamma);
return ya;
}
if (quadrant == 2) {
gamma = gamma - Math.PI / 2;
//xa -= r * Math.cos(gamma);
if (left) ya += r * Math.sin(gamma);
else ya -= r * Math.sin(gamma);
return ya;
}
if (quadrant == 3) {
gamma = gamma - Math.PI;
//xa -= r * Math.cos(gamma);
if (left) ya += r * Math.sin(gamma);
else ya -= r * Math.sin(gamma);
return ya;
}
if (quadrant == 4) {
gamma = gamma - Math.PI * 1.5;
//xa += r * Math.cos(gamma);
if (left) ya -= r * Math.sin(gamma);
else ya += r * Math.sin(gamma);
return ya;
}
return ya;
}
xa and ya are the amounts of pixels the character moves by each frame. As I said forward/backwards works right but although I have tried a lot of different math things I can’t get the side to side movement to work right. I posted on here about low fps a week or two ago and the responses I got were helpful so I thought I’d try again. I just spent a little while working out the math on paper (not for the first time) and I was surprised when the movement did not work at all how I expected.