Better way of doing this? Facing the mouse

Non-platform specific questions.

Better way of doing this? Facing the mouse

Postby Hblade » Wed Dec 19, 2012 6:49 am

Code: Select all
char *key=GetKeyState();
angle=direction(xscreen, yscreen, xmouse, ymouse);

switch(mov) {
    case 0: // not walking
        if(angle>=-145&&angle<45) {
            ChangeAnimation("Event Actor", "standRight", NO_CHANGE);
                              }
        if(angle>=45&&angle<145) {
            ChangeAnimation("Event Actor", "standUp", NO_CHANGE);
                              }
        if(angle>=145&&angle<245) {
            ChangeAnimation("Event Actor", "standLeft", NO_CHANGE);
                              }
        if(angle>=245&&angle<345) {
            ChangeAnimation("Event Actor", "standDown", NO_CHANGE);
                              }
    break;
    case 1: //moving
         if(angle>=-145&&angle<45) {
            ChangeAnimation("Event Actor", "walkRight", NO_CHANGE);
                              }
        if(angle>=45&&angle<145) {
            ChangeAnimation("Event Actor", "walkUp", NO_CHANGE);
                              }
        if(angle>=145&&angle<245) {
            ChangeAnimation("Event Actor", "walkLeft", NO_CHANGE);
                              }
        if(angle>=245&&angle<345) {
            ChangeAnimation("Event Actor", "walKDown", NO_CHANGE);
                              }
    break;
            }
if(key[KEY_d]==1 || key[KEY_a]==1 || key[KEY_s]==1 || key[KEY_w]==1) {
    mov=1;
                                                                     }
else { mov=0; }

if(key[KEY_d]==1) {
    x+=5;
                  }
if(key[KEY_a]==1) {
    x-=5;
                  }
if(key[KEY_s]==1) {
    y+=5;
                  }
if(key[KEY_w]==1) {
    y-=5;
                  }


Can someone clean this up? I'd like to know a shorter and more effective way of doing this :) I'm trying to make an MMO-styled game, but different. The player always faces the direction the mouse is pointing but he only has 4 actually facing directions



EDIT!
I have edited the code on my side and figured it out :) Using calculator. It's slightly different but its still large, I'd still like to know if there is an easier way of doing this :)
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Re: Better way of doing this? Facing the mouse

Postby skydereign » Wed Dec 19, 2012 7:16 am

I believe I've mentioned this to you before. Essentially, you want 360 degrees to map to 4 values.
0 (-45 - 45)
1 (45 - 135)
2 (135 - 225)
3 (225 - 315)
Now, dividing 360/90 is 4 (where 4%4 is equal to 0). So we take our angle and divide it by 90, and then mod it by 4. This will get almost what you want, with a slight problem. If the angle is 359, integer division will not get what we want.
(359/90)%4
3%4
3
Meaning the player will be facing south (as 3 is the same as 270). To fix this, we can add 45 (this is like adding 0.5 to an integer to make it round properly).
Code: Select all
int mov = ((direction(xscreen, yscreen, xmouse, ymouse)+45)/90)%4; // gets the direction you want
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Better way of doing this? Facing the mouse

Postby Hblade » Wed Dec 19, 2012 8:21 am

thanks sky.
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest

cron