Game Development Community

Making a bot "look around

by Anthony Cosgrave · in Technical Issues · 09/14/2007 (3:18 pm) · 3 replies

Hi,

I'm not sure if this should be in Mathematics or not...

I'm trying to make a bot look around (i.e. rotate on the spot for X degrees) while guarding a particular position.

I've tried to update its aim location every few "thinks", I also tried to update it's transform by rotating it around the z axis, but no joy at all.

Also, does anyone know how to make a bot wait at a particular spot for a number of seconds?

Thanks for any help,

Anthony

#1
09/15/2007 (7:07 pm)
You want it to just appear like he's looking around, right?

Believe it or not, a path works great for this. Create a path, maybe just 4 points, where all the X Y and Z numbers are the same. The only numbers that change are the Y angles. For example, say the Y angles for the 4 points are 45, 90, 135, 90. Now, if you set up this path as a cyclical or repeating path (I can't remember the correct term for it), then you'll have a path where he looks straight ahead, then to the left, then straight ahead, then to the right, over and over again, without leaving his post. You can control how fast he looks around by varying the time between points on the path.

I believe there's a "smoothing" or "spline" setting that will smooth out the reversal of direction at the end of each sweep. (I've been working in T2D all summer and my TGE is a bit sloppy.)

You could even have him spin all the way around, slowly, by selecting angles of 0, 90, 180, and 270.

--
Regards
Ray
#2
09/16/2007 (5:55 am)
Try something like this:

function AIPlayer::vecRotateAZ(%vec, %angle)   // functions borrowed from killer kork resource
{
  %rot = MatrixCreateFromEuler("0 0 " @ mDegToRad(%angle));
  return MatrixMulVector(%rot, %vec);
}
function AIPlayer::doYaw(%this, %angle)
{
  %pos = %this.getPosition();
  %eye = %this.getEyeVector();

  // rotate eye vector and add to position
  %vec = AIPlayer::vecRotateAz(%eye, %angle);
  %posAim = VectorAdd(%pos, VectorScale(%vec, 100));
  
  // set aim location
  %this.setAimLocation(%posAim);
}

When ever you want the ai player to look around in a circle 90 degrees at a time, do this. If this look around function is triggered by a thinking loop, skip over this code block:

// We want to have him do a complete circle in 2000 milliseconds
// %this assumes the aiPlayer. If you're using %plr or %player, change my references to %this

%this.doYaw(90);
%this.schedule(500,doYaw,90);
%this.schedule(500,doYaw,90);
%this.schedule(500,doYaw,90); // This should clear his aim back to the way it was before.

If your aiPlayer is on a think loop that triggers this and your bot is spinning around like a drill, try this in your think loop:
Note, I'm not too sure of the proper syntax of a switch$ statement, there may be errors in the console.

// In you thinking loop function

switch$(%this.yawIndex)
{
   case 0: // 90 degrees
      %this.doYaw(90);
      %this.yawIndex++; // The next time he thinks, the yaw index is incremented, so the switch 
                                       // statement should call case 2, which yaws even further.
   case 1; // 180 degrees
       %this.doYaw(90);
        %this.yawIndex++;

    case 2: // 270 degrees
       %this.doYaw(90);
       %this.yawIndex++;

    case 3: // 360 degrees, reset the aim and yawIndex.
       %this.doYaw(90);
       %this.clearAim(); // Sets the aim to the direction it last moved to.
       %this.yawIndex = 0;
}
You shouldn't have to define %yawIndex, it will start out as zero. Each time the aiplayer thinks, he turns 90 degrees. If you have the thinking loop set to a low ms value, like 50 or so, he's going to spin around really fast, so you might want to create a seperate schedule function for yawing so it doesnt go so fast. If you have it set to 500 or 400 or above, you're good.

Wow, that was a hard post!
#3
09/18/2007 (2:30 am)
Thanks for the replies! :)

I'll check out both of your solutions.