Actor Movement Anomaly

Questions, comments and discussion about the Game Editor development.

Actor Movement Anomaly

Postby bamby1983 » Mon Dec 31, 2012 12:56 am

I wrote the following code to determine which animation is selected when an actor is moved on the screen. The user clicks a point on the ground and theactor moves to that point. There are multiple animation sequences based on the angle of movement (the game is played using a top view). I tried to achieve this for a simple up-down (north-south) movement for my unit using the following code:

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);
             }
         }
                     }
bamby1983
 
Posts: 112
Joined: Tue Jul 31, 2012 11:36 pm
Score: 8 Give a positive score

Re: Actor Movement Anomaly

Postby skydereign » Mon Dec 31, 2012 12:30 pm

Actors have an xscreen and yscreen variable which correspond with their screen coordinates (relative position from the top left pixel of the view). The xmouse/ymouse variables use the same coordinate system. Therefore using xmouse/ymouse with x and y won't work unless you use view.x and view.y as well. Another helpful thing, since you were wondering about not having to hard code in values, is you can use the view's width/height variables to access the resolution of the game. Therefore you can use view.width/2 or similar for your equation.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Actor Movement Anomaly

Postby bamby1983 » Mon Dec 31, 2012 8:17 pm

Thanks! It worked after I used yscreen and xscreen.

I also figured out why the animations were offset by 90 degrees. My formula was based on a standard graph where y increases upwards, whereas in the game screen in GE, y increases downwards.
bamby1983
 
Posts: 112
Joined: Tue Jul 31, 2012 11:36 pm
Score: 8 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest