3D Rad - Free 3D game maker - Forum

This forum is now archived!

This forum is locked, and is a read-only version. A new community-ran forum can be found at classdev.net

News:

The 3DRad community can be found at classdev.net.

Pages: [1]

Author Topic: Waypoint following AI character  (Read 1819 times)

jestermon

« on: March 14, 2010, 10:00:25 AM »
Here's my next experiment in moving a character over a terrain. This one makes use of 5 random waypoints, and moves between them. In most game engines, there are a million ways to do this, but with 3DRad, due to it's unique OO component design, one must plug the components together with some nimble scripting in order to achieve this.

I have looked at the different possibilities, and decided to work with the basic concepts and functionality of the RobotDemo, and use a �pseudo-trainer� (an invisible guide) that moves between the waypoints, that keeps the main �skinmesh� character always facing in the right direction, as it follows it.

The movement algorithm for the �trainer guide� is still crude, but will be enhanced with refined interpolations, as my experiments progress.

Using this waypoint method instead of using a path, allows a character to move through a maze or streets in a more random fashion, thus adding an element of surprize to the game. By using different waypoint sets ( which can be loaded from file or server, as required), they can be used to enhance a story line, by making an AI character go to various places, and act out different roles as the game progresses.

One can add different paths to the equation as well, which will be the subject of my next experiment.

I have replace the compiled project file with the original editable one.
The runtime libraries have not been included in this archive in order to keep the file small.

jestermon

« Reply #1 on: March 14, 2010, 10:01:33 AM »

jestermon

« Reply #2 on: March 15, 2010, 06:51:29 AM »
I should read the forum posts more often and in more detail (sigh): the new CharacterAI, when available will make this line of research redundant. Oh, well, I am new to Rad, so you guys will have to excuse me running blindly ahead in my enthusiasm of this amazing new toy (3drad) I am playing with. Well perhaps someone can learn something from the 6 hours of  fun I had figuring this out.  :)
« Reply #3 on: March 15, 2010, 08:42:14 AM »
Very good stuff, the guy looks creepy with the short jeans and shirt...LOL!
Roll out!
http://www.youtube.com/watch?v=VD8MvMaPNO4

Do you have kids between the ages of 3 and 9?

http://www.amazon.com/dp/B007K1EFC6

jestermon

« Reply #4 on: March 15, 2010, 07:58:57 PM »
UPDATED VERSION.. doing it the propper way
http://www.weebly.com/uploads/1/6/2/1/1621282/characterwaypointai2.zip

The fun part about making mistakes in gaming programming, is that you learn so much from those mistakes, and climb up the ladder of experience that much faster. Perhaps egos say one should not admit to mistakes; but when a programmer is dedicated to actually getting that game done, he/she actually enjoys the experience of learning through mistakes, and ignores ego.

Having played with 3Drad for about a month now, I am bound to make assumptions, and follow them through. The advantage to game programming is that your �character' can re-spawn, and run the gauntlet again.

My first attempt at doing waypoints for a character  AI, was one such mistake based on an assumption. Since I could not find any reference to actually move the character, I figured on using the Force object to achieve this. It worked very crudely, but did give me reasonable results.

Having seen no reference to character AI control on the forum, I thought it may be fun to share the idea.. Till I read about the upcoming CharacterAI release... So I investigated the existing Character object in more detail to see what hidden gems I had missed, and suddenly the light came on.. So here is the result of enlightenment..

Most advanced users of 3DRad already know the following, but I am posting this for noobs such as myself, so that they don't run down blind alleys when trying to figure out how a Character works.

In order to script an AI character, you need to know a few things..
For your Character Object ...
1..You need a RigidBody sphere, (a ball to roll all over your game area).
2..You need a Target, which MUST be a Skinmesh (so it can be relocated)
3..You need to DISABLE any key-controls that you wish to pass onto your script
4..You need to set your walk speed to something manageable
5..You need a Skinmesh for the visual character (eg. Your vampire duck character)

For the demo, I...
1..Placed a small rigidbody terrain for the character to run around on.
2..Placed 5 rigidbody cones to use as waypoints. Skinmesh will also work fine
3..Placed a rigidbody sphere and enabled �bounding sphere�, scaled it to (0.3, 0.3 ,0.3)
4..Place a �target� skinmash� (Needed for aiming the character)
5..Placed an animated skinmesh running character
6..Placed a Character Object, and set it as follows:
.. Disabled all keyboard controls.
.. Set target approach damping to 100 (stop a little way from the waypoint)
.. Set �rotation speed if target..� to 30 (for a loose sharp turn)
.. Set walk forward speed to 30 (nice slow running speed for this demo)
.. Linked the character-skinmesh, character sphere, terrain and target-skinmesh
7..Added a script and some text and value displays

The scripting of the AI is where the actual fun comes in..
The main points to note in the following script is
a. Using an array to hold the locations of the waypoints
b. Randomly picking a waypoint from the list
c..Moving the target-skinmesh (locator) to the selected waypoint,
...because this is the direction that the character looks and and moves towards.
d..Keeping watch on the distance between the character and the waypoint
d..Once the character gets within a certain distance, a new waypoint is selected
e.. and so on, ad infinitum, or until ESC is pressed.

Well this little script drives the AI, and once one understands the basic
concepts, 3DRad is total fun, and one can put together a simple game very
quickly.


~~~~~~SCRIPT~~~~~~~~~~~
bool walk = true;
Vector3 targetLocation;
Vector3 myLocation;
float x1;
float y1;
float z1;
float x2;
float y2;
float z2;
float distance;
int tick = 0;
Vector3[] waypoints(5);
int targetIdx;

//Get the next random waypoint index
int NextWaypoint()
{
   return  iFloatRand(0,5);
}

//Position the steering locator
void PositionLocator(Vector3 v)
{
   iObjectLocationSet(OBJ_22,v);
}

void Main()
{

   if(iInitializing()){
      //get waypoint locations
      iObjectLocation(OBJ_88, waypoints[0]);
      iObjectLocation(OBJ_110,waypoints[1]);
      iObjectLocation(OBJ_132,waypoints[2]);
      iObjectLocation(OBJ_154,waypoints[3]);
      iObjectLocation(OBJ_176,waypoints[4]);
      targetIdx = NextWaypoint();
      PositionLocator(waypoints[targetIdx]);
   }

   //get my location.. using rigidbody,
   //dont use the character by mistake (took me half hour to figure this out)
   iObjectLocation(OBJ_66,myLocation);
   x1 = myLocation.x;
   y1 = myLocation.y;
   z1 = myLocation.z;

   //get target location
   targetLocation = waypoints[targetIdx];
   x2 = targetLocation.x;
   y2 = targetLocation.y;
   z2 = targetLocation.z;

   //calculate distance between me and target
   distance = iFloatSqrt(((x1-x2)*(x1-x2))+((z1-z2)*(z1-z2)));

   //if steering character is close enough, go onto next waypoint
   if(distance < 2)
   {
      targetIdx = NextWaypoint();
      PositionLocator(waypoints[targetIdx]);
   }

   //Set walk forward speed for character to full
   //Numbers higher than 1 have no effect..
   //To manage higher speeds, increase the walk speed in the character
   //object.. then use decimal values
   OUT_2 = 1;

   OUT_44 = distance;
   OUT_198 = targetIdx +1;
}

*project file in archive replaced with editable one*

jestermon

« Reply #5 on: March 15, 2010, 08:19:35 PM »
Very good stuff, the guy looks creepy with the short jeans and shirt...LOL!
Thanks, Haha, the character is my little bvh-enabled one, rigged with no weight-painting for use in my tests. Had to put some clothes on the guy, so just a splash of denim and apple did the trick. He is kinda creepy.
Pages: [1]