Game Development Community

Adding flocking to AIPlayer

by James Daniel · in Torque Game Engine · 01/28/2005 (9:30 am) · 13 replies

I'm in the process of adding flocking into the engine (in the AIPlayer module) and I've run into a few problems. First off let me tell you I'm basing my design on the flocking chapter of "AI for Game Developers".

To use the flocking model I've chosen I need to keep an average of the Bots neighbors facing vectors. My first problem is finding the facing of the Bots. I've been looking for a vector that can be the facing but I've run into the problem that the Vectors in the engine are just points (x/y/z co-ords only) and not true vectors (x/y/z/w). Why no magnitude? And where can I find it?

Has anyone done anything with Bot facing and can someone point me to it?

#1
01/28/2005 (9:38 am)
They are direction vectors. If you want it with a magnitude (although the magnitude is already in X/Y/Z), use getVelocity.

Regardless, any facing vector will lack magnitude, because you can't be looking in a specified direction more than another person...

Of course, if you want the direction vector to another bot, it would be bot1.position - bot2.position.
#2
01/28/2005 (10:11 am)
I think that players only rotate around the z-axis (i.e. turn left/right) and can't "turn" up or down, although they can look up and down. Thus the "rotation" for player objects really only uses the z component of the vector given by "getRotation" to indicate rotation in the z-axis.

If you want full 3D facing, you may have to use an object that supports full 3D facing (like a vehicle) or modify the player object to support that kind of stuff.
#3
01/28/2005 (10:24 am)
Yay flocking.
ya'll might like to look at my home-grown 2D flocking model.
it was easy to program, i'd be happy to yap about it.
elenzil.com/orion/progs/flock/Flock1.exe
#4
01/28/2005 (2:12 pm)
Mike;

Mathematically speaking the vectorF defines a point. To be a mathematical vector you also need a magnitude. That is/was what is/was confusing me. From what you say the magnitude of the vectors is the length of the line drawn from the origin of the object co-ords to the point defined by the VectorF. True?

Next, by front I mean the side of the bot where, in the objects co-ords, the z co-ord is positive and the x/y are both zero. Or to put it another way, with a human, or in my case a cow, bot it is the direction the eyes are looking when it is said to be looking 'strait a head". Sometimes these two definitions of 'front' don't agree and it is important that I know this if it is true in the torque engine. This direction is very important to doing the flocking AI as this one of the simple was to calculate Field of View for LOS.

Gary:
I'm just doing 2D flocking for now as I'm really just doing hurding for some cows. Thanks for the pointer I'll look at the bots rotation and see if that will give me what I need.
#5
01/28/2005 (3:06 pm)
Quote:
Mathematically speaking the vectorF defines a point. To be a mathematical vector you also need a magnitude. That is/was what is/was confusing me. From what you say the magnitude of the vectors is the length of the line drawn from the origin of the object co-ords to the point defined by the VectorF. True?

yep.
mathematically speaking, a 3-dimensional vector is simply 3 coordinates: x, y, z.

when you add w, it's now a 4-dimensional vector and is usually
used to represent a rotation, not a direction.

the magnitude of a 3-vector is contained in the x y z.
for example:
the vector [1 0 0] has magnitude 1,
the vector [0 2 0] has magnitude 2,
the vector [a b c] has magnitude sqrt (a*a + b*b + c*c).

it's true that in computer graphics,
vectors sometimes have a w component,
but except for rare situations w is always set to 1,
and you're better off ignoring it until you're comfortable with matrix math.
#6
01/28/2005 (3:11 pm)
>the magnitude of the vectors is the length of the line
>drawn from the origin of the object co-ords to the
>point defined by the VectorF. True?
True.
The layman's definition of vector is something with magnitude and direction. If you go a certain distance in a certain direction, you will arrive at a point. Thus a point is a vector. If you are really interested in "mathematically speaking," look up the 8 postulates of vector spaces. This is just a more complicated way of arriving at the same result, though.

>the direction the eyes are looking
I am certain that I saw something about line of sight in one of Mark Holcomb's resources (about AI coincidentally). He programmed his bots to respond differently to players who were standing in sight. Sounds like he must have used the sort of information that you are looking for.
#7
01/28/2005 (8:29 pm)
I did some simple flocking for my Game in a day entry. I created a decendant of AIPlayer that would trigger a script call for each processTick (for prototyping). I'll attach the script code I used to do the flocking below. If you look at it, I don't actually modify the velocity of the player, I just feed it newly calculated destinations. It's a bit hacky (but quick for a GID!). The other way to look at it is that the flocking algo is just layered on top of the move to this destination algo.

if ((%obj.client == 0) && ((%obj.cndTick++ % (2)) == 0)) {
    %startFlockSearch = 6;
    %closestFlockDist = %startFlockSearch;
    %closestFlockMember = 0;
    
    %pos = getwords(%obj.getTransform(), 0, 2);
    %accel = "0 0 0";
    // Head towards the other members of the flock
    for (%i = 0; %i < $PotentialFlock.getCount(); %i++) {
      %fm = $PotentialFlock.getObject(%i);
      if (%fm != %obj) {        
        %fmPos = getwords(%fm.getTransform(), 0, 2);
        %dist = VectorDist(%pos, %fmPos);
        // Figure out if this is the new flock
        if ((%dist < %closestFlockDist) && (%fm.ParentFlock != 0) && (%fm.ParentFlock.client == %fm.client)) {
          %closestFlockMember = %fm;
          %closestFlockDist = %dist;
        }
  
        // Figure out acceleration
//        if (%fm.ParentFlock == %obj.ParentFlock) {
          if (%dist < %fm.influenceDistance) {
            %posDiff = VectorSub(%fmPos, %pos);
            %posDiff = VectorNormalize(%posDiff);
            %posDiff = VectorScale(%posDiff, %fm.influenceMult);
            if (%dist < 5) {              
              %posDiff = VectorScale(%posDiff, -0.5);
            }
            %accel = VectorAdd(%accel, %posDiff);
          }
//        }
      }
    }
    // Find new destination
    %accel = VectorNormalize(%accel);
    %NewTarget = VectorAdd(VectorScale(%accel, 100), %pos);
    %obj.setMoveDestination(%NewTarget);
#8
01/28/2005 (11:00 pm)
Thanks for the help guys. Like many here on the forums I found the answer to some of my questions myself while others helped greatly with their answers and insights.

I found out how to get the facing of a bot by stumbling across the consolemethod getForwardVector. This method can easly be turned into an AIPlayer method and it gives the facing vector for the bot.

BTW: When I showed my in house mathematician this thread she was herd to be muttering darkly about '....programmers....purity of mathimatics.....';) I haven't a clue as to what she could possibly be talking about.:D
#9
01/29/2005 (9:18 am)
I am also a mathematician. The definition of vector that is commonly used (direction plus magnitude) is quite a lot narrower than what we learn about in math classes. It is also less clearly defined. Consider the number (5). Is it a vector? It has magnitude 5 and direction +. The number (-5) also has magnitude 5 but direction -. Are these vectors? I leave it to you to decide whether they are vectors by the common definition. A mathematician would simply say that you haven't provided enough information. If you further specified that these numbers were part of the real number line, then he would agree that the real number line is a vector space and that the numbers on that line (such as 5 and -5) were vectors. A mathematician would also say that the function f(x)=x^2+5 is a vector within the vector space of continuous functions.

Vectors must exist within a vector space. The definition of a vector space has two parts: (1) It is a context in which to define two types of other items, called "vectors" and "scalars", and (2) the vector space, its vectors, and its scalars musty satisfy 8 properties: the 8 postulates of vector spaces. I mentioned these postulates in passing in a previous post. If you decide to look these up, let me give you a little advice. When you see a "+" such as "a + b" don't think of it as addition. All you know about "+" is that it is a binary operation (a function of two parameters "a" and "b"). So when you see "a+b" think "+(a,b)". The same is true of *. It is also a function of two parameters. Here are their prototypes in c++ syntax. ( replace * with "multiply" and + with "add" just because c++ doesn't allow these to be function names)

vector multiply(scalar a, vector b);
vector add(vector a, vector b);

Oh and there's one more operation, "-", which I will call "opposite" for the prototype:

vector opposite(vector a);


Anyway, just giving you a brief window into your in house mathematician's world. None of this stuff will help you with Torque, which considers vectors to be anything with 3 components, , which obeys the "normal" definitions of addition and multiplication.
#10
01/29/2005 (9:46 am)
When my house mathmatician read this posting, I heard several emphatic "YES!" noises. When I asked what had made her say that, she said "definition of vector is narrower than we learned in math class" and "mathmatician would say you haven't provided enough information." As she left the computer she was herd to say "Programmers, rrrrrrrrrrrrrrr!"
#11
01/29/2005 (12:55 pm)
Well, it sounds like you're keeping your mathematician entertained.
Glad I could help :)
#12
01/29/2005 (3:16 pm)
@Eric

That was a really interesting post, and I think I'll have to go and look up those 8 postualtes of vector spaces you're talking about.
#13
01/29/2005 (9:26 pm)
James,
I think you'll find this website interesting:
mathworld.wolfram.com/VectorSpace.html