Help with angles

Non-platform specific questions.

Help with angles

Postby Hblade » Mon Jun 27, 2011 9:39 pm

I've written a really complex code, btu I'll pinpoint the part I'm confused at. I'm trying to make it so that the AI will face the player when you collide with him/her. There are 4 directions, up down left and right. i'll explain which AI_DIR is which.

AI_DIR:
Code: Select all
0: Walk Up
1: Walk Down
2: Walk Right
3: Walk Left
4: Stand Down
5: Stand Left
6: Stand Right
7: Stand Up


Now heres the confusing part. I'm using angle=direction and stuff, it gets the angles correctly but finding the angle is what I'm confused on. For some reason, it works but it's all off... like if your at the center of the character and move a bit, she changes direction too soon. It's strange, but heres the code
Code: Select all
void AIToPlayer() {
    angle=direction(xscreen, yscreen, Player.xscreen, Player.yscreen);
    if (angle<60) {
        AI_DIR=7; }
    if (angle>60 && angle<180) {
        AI_DIR=4; }
    if (angle>180 && angle<240) {
        AI_DIR=6; }
    if (angle>240) {
        AI_DIR=5; }
                  }

angle=direction works, I even planted a text actor to test it. The issue is, I can't seem to get the angles right. I tried if (angle<90) and so on so fourth, but it would seem I need to divide 360 by 4, which is 90, but... it's so confusing because it needs to be 180° (Flat End) for each direction...


EDIT: I think I figured it out, maybe if I use if (angle<90 || angle>270) that'll create a flat 180° angle for top scanning.

Nope.. GAH I can't figure this out >.>

Edit 2:
i tried replacing with:
Code: Select all
void AIToPlayer() {
    angle=direction(xscreen, yscreen, Player.xscreen, Player.yscreen);
    if (angle<90 || angle>270) {
        if (x<Player.x) { AI_DIR=6; } if (x>Player.x) { AI_DIR=5; } }
    if (angle>0 && angle<180) {
        AI_DIR=7; }
    if (angle>=180 && angle<360) {
        AI_DIR=5; }
                  }


as to a follow up with this angle image I made after studing the angles of GE
Image

However I still can't get it right.
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: Help with angles

Postby lcl » Mon Jun 27, 2011 9:50 pm

You can see from your picture that it's not going to work, because there is always 2 statements true at the same time.

This is how you do it, with ranges of 90°s.
The ranges are:

45 - 135
135 - 225
225 - 315
315 - 45

Now there is no overlapping statements. :D
Try it, I'm quite sure it works! :)
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: Help with angles

Postby Hblade » Mon Jun 27, 2011 9:57 pm

Genius! Thank you! +1 point :D Man dude this boggled my mind so much and you however cought the bug in a matter of seconds ^^ thanks a bunch! :D
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: Help with angles

Postby skydereign » Tue Jun 28, 2011 1:12 am

When I'm handling angles I usually use this function to do that (no need for ifs this way).
Code: Select all
int
angle_dir (double ang)
{
    return ((int) (angle+45) % 360) / 90;
}

It returns 0 if it is within 315-45, 1 if within 45-135, and so on. That way you can use a switch statement instead of if/else if/else statement.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Help with angles

Postby Hblade » Tue Jun 28, 2011 2:19 am

Thank you very much sky, perhaps you can clean my code up after the template/example is done? :)
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: Help with angles

Postby lcl » Tue Jun 28, 2011 2:26 pm

Hblade wrote:Genius! Thank you! +1 point :D Man dude this boggled my mind so much and you however cought the bug in a matter of seconds ^^ thanks a bunch! :D

Your welcome! :)
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: Help with angles

Postby Hblade » Wed Jun 29, 2011 1:56 pm

^^
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