- Code: Select all
if (ymouse<Soldier_1.y)
ChangeAnimation("Soldier_1", "Soldier_No_Gun_90_deg", FORWARD);
else if (ymouse>Soldier_1.y)
ChangeAnimation("Soldier_1", "Soldier_No_Gun_270_deg", FORWARD);
In the above code, "Soldier_1" is the actor being moved.
However, I noticed that the animations reacted differently in the sense that the actor was walking backwards while moving up unless I clicked at least half a screen away. My game uses a 1360 x 768 resolution, so I divided the height of the screen by 2 and modified the formula as follows.
- Code: Select all
if (ymouse-384<Soldier_1.y)
ChangeAnimation("Soldier_1", "Soldier_No_Gun_90_deg", FORWARD);
else if (ymouse-384>Soldier_1.y)
ChangeAnimation("Soldier_1", "Soldier_No_Gun_270_deg", FORWARD);
This time around, it worked as expected down to the pixel. I'd like to understand why this occurred. Is there a different frame of reference used for actors and the mouse position?
Is there any way I can make this dynamic so that players using a different screen resolution will not be limited by the value I hard coded in the formula?
For now, I used the above mechanism to compensate for the error (subtracting half the x and y screen resolutions from the x and y mouse values respectively) and drew up the following code. Unfortunately, my animations are consistently 90 degrees apart (except for the initial if statement without the slope). The slope corresponds to the tan of the angle formed by the line joining the mouse position and the the actor position. Since my angles are all 22.5 degrees apart, I have used ranges of -11.25 to +11.25 degrees for all the animations. So the if condition values for the zero degrees animation would lie between tan(-11.25) and tan(11.25).
The angle of the animation is indicated in the animation name (decimal values have an underscore instead of a decimal point). Please could someone let me know why this approach results in an angle offset of 90 degrees?
- Code: Select all
void moveDirection() {
if (xmouse-680==Soldier_1.x) { // Preventing a divide by zero error
if (ymouse-384<Soldier_1.y)
ChangeAnimation("Soldier_1", "Soldier_No_Gun_90_deg", FORWARD);
else if (ymouse-384>Soldier_1.y)
ChangeAnimation("Soldier_1", "Soldier_No_Gun_270_deg", FORWARD);
}
else {
// Calculating the slope
float slope=(ymouse-384-Soldier_1.y)/(xmouse-680-Soldier_1.x);
if (xmouse-680>=Soldier_1.x) { // For moving right
if (slope<=-5.0273&&slope>5.0273)
ChangeAnimation("Soldier_1", "Soldier_No_Gun_90_deg", FORWARD);
else if (slope<=5.0273&&slope>1.4966)
ChangeAnimation("Soldier_1", "Soldier_No_Gun_67_5_deg", FORWARD);
else if (slope<=1.4966&&slope>0.6682)
ChangeAnimation("Soldier_1", "Soldier_No_Gun_45_deg", FORWARD);
else if (slope<=0.6682&&slope>0.1989)
ChangeAnimation("Soldier_1", "Soldier_No_Gun_22_5_deg", FORWARD);
else if (slope<=0.1989&&slope>-0.1989)
ChangeAnimation("Soldier_1", "Soldier_No_Gun_0_deg", FORWARD);
else if (slope<=-0.1989&&slope>-0.6682)
ChangeAnimation("Soldier_1", "Soldier_No_Gun_337_5_deg", FORWARD);
else if (slope<=-0.6682&&slope>-1.4966)
ChangeAnimation("Soldier_1", "Soldier_No_Gun_315_deg", FORWARD);
else if (slope<=-1.4966&&slope>-5.0273)
ChangeAnimation("Soldier_1", "Soldier_No_Gun_292_5_deg", FORWARD);
else if (slope<=-5.0273&&slope>5.0273)
ChangeAnimation("Soldier_1", "Soldier_No_Gun_270_deg", FORWARD);
}
else { // For moving left
if (slope<=-5.0273&&slope>5.0273)
ChangeAnimation("Soldier_1", "Soldier_No_Gun_270_deg", FORWARD);
else if (slope<=5.0273&&slope>1.4966)
ChangeAnimation("Soldier_1", "Soldier_No_Gun_247_5_deg", FORWARD);
else if (slope<=1.4966&&slope>0.6682)
ChangeAnimation("Soldier_1", "Soldier_No_Gun_225_deg", FORWARD);
else if (slope<=0.6682&&slope>0.1989)
ChangeAnimation("Soldier_1", "Soldier_No_Gun_202_5_deg", FORWARD);
else if (slope<=0.1989&&slope>-0.1989)
ChangeAnimation("Soldier_1", "Soldier_No_Gun_180_deg", FORWARD);
else if (slope<=-0.1989&&slope>-0.6682)
ChangeAnimation("Soldier_1", "Soldier_No_Gun_157_5_deg", FORWARD);
else if (slope<=-0.6682&&slope>-1.4966)
ChangeAnimation("Soldier_1", "Soldier_No_Gun_135_deg", FORWARD);
else if (slope<=-1.4966&&slope>-5.0273)
ChangeAnimation("Soldier_1", "Soldier_No_Gun_112_5_deg", FORWARD);
else if (slope<=-5.0273&&slope>5.0273)
ChangeAnimation("Soldier_1", "Soldier_No_Gun_90_deg", FORWARD);
}
}
}