Page 1 of 1

Help with angles

PostPosted: Mon Jun 27, 2011 9:39 pm
by Hblade
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.

Re: Help with angles

PostPosted: Mon Jun 27, 2011 9:50 pm
by lcl
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! :)

Re: Help with angles

PostPosted: Mon Jun 27, 2011 9:57 pm
by Hblade
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

Re: Help with angles

PostPosted: Tue Jun 28, 2011 1:12 am
by skydereign
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.

Re: Help with angles

PostPosted: Tue Jun 28, 2011 2:19 am
by Hblade
Thank you very much sky, perhaps you can clean my code up after the template/example is done? :)

Re: Help with angles

PostPosted: Tue Jun 28, 2011 2:26 pm
by lcl
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! :)

Re: Help with angles

PostPosted: Wed Jun 29, 2011 1:56 pm
by Hblade
^^