Thread: Roaming AI

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Cool Roaming AI

    Hi, I want to cause an object to roam.

    I was thinking in a figure of 8, but with straight edges.

    _
    |_|
    |_|


    How could I go about mplementing such a roaming pattern?

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    What are some of your ideas and then we can discuss how they may or may not work?

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    Hi,

    I basically have an character that moves only in one direction at a time. Basically, I want it to have random movements within the game screen but perhaps some pattern to it. I thought a figure of 8 would be nicely suited.

    I just dont know where to start in coding this one

  4. #4
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Code:
    enum dir { up, down, left, right };
    extern void object::move(dir which_way);
    
    void object::make_move( . . .)
    {
       //if nothing interesting happens...
       dir direction = (std::rand() >> 2) % 4;  //random mod 4, but first shifted 
                                                //to use higher-order bits. They're
                                                //sometimes "more random."
       move(direction);
    }
    *edit* How does a figure eight go? right, down, left, down, right, up, left, up? + -' - -' + +' - +' ?
    Last edited by CodeMonkey; 03-24-2009 at 12:06 AM.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    Thanks.

    P.S. Nice quote

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    Code:
        int[][] zigzag = {{1, 1}, {-1, 1}};
        aX += zigzag[patStep][0];
        aY += zigzag[patStep][1];
    This code shows how to implement a very simple vertical zigzag pattern. The int array zigzag contains pairs of XY offsets used to apply the pattern to the alien. The patStep variable is an integer representing the current step in the pattern. When this pattern is applied, the alien moves in a vertical direction while zigzagging back and forth horizontally.

    I found this on the net (Internet Games Tutorial - Softlookup.com)

    It seems a novel idea. How would I implement patStep without immediately having a call to draw afterwards?

  7. #7
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    You probably don't want to generate a new random direction every time make_move() is called though. Have some kind of counter or timer that allows some time to pass before a new random direction is generated. That way he is not flipping directions every frame of motion....for if he did he would probably go nowhere.
    My Website

    "Circular logic is good because it is."

  8. #8
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Random movement usually looks pretty odd as DavidP points out. You may also consider hard-coding a bunch of different patterns (like the figure 8 using a method like strokebow has shown), then randomly choose a pattern from the set every x steps.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You could just store movements as paths. Stick them in strings, and just run through the string until you get to the end. Then make something parse the current character.
    Code:
    you.path = "enwseswn";
    
    switch( you.path[ you.here++ ] )
    {
        case 'n': you.y--; break;
        case 'e': you.x++; break;
        case 's': you.y++; break;
        case 'w': you.x--; break;
        case '*':
        {
            switch( rand() % 4 )
            {
                case 0: you.y--; break;
                case 1: you.x++; break;
                case 2: you.y++; break;
                case 3: you.x--; break;
            }
        }
        break;
    }
    
    randomsoutheastroamer.path = "s*e*";
    Then just throw in any other neat exceptions you want, as I did with *. Naturally you will want to do some boundary checks.
    Last edited by quzah; 03-30-2009 at 09:00 PM. Reason: randomsoutheastroamer added
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple space combat AI
    By VirtualAce in forum Game Programming
    Replies: 5
    Last Post: 01-06-2009, 12:54 AM
  2. chess ai contest
    By Raven Arkadon in forum Contests Board
    Replies: 7
    Last Post: 07-09-2005, 06:38 AM
  3. AI Contest Proposal
    By MadCow257 in forum Contests Board
    Replies: 4
    Last Post: 03-13-2005, 03:27 PM
  4. Game Design Topic #1 - AI Behavior
    By TechWins in forum Game Programming
    Replies: 13
    Last Post: 10-11-2002, 10:35 AM
  5. Technique of all board-like games?
    By Nutshell in forum Game Programming
    Replies: 28
    Last Post: 04-24-2002, 08:19 AM