Welcome, Artificial Intelligence can be implemented in many different ways but today I will be covering an form of AI that will allow you to calculate distances between sets of objects that can be useful for whether or not an NPC is given permission to shoot an enemy or the player.
The geometry we will be covering is mostly consisted of Analytic geometry following the basic principles of the Cartesian coordinate system. We will modify the 2D Distance Geometrical Distance formula to calculate the distances of 3D Objects later on in the tutorial...
Our first step will be to write our basic C++ code. Then our next step will be to implement the formulas into our code and then calculate the distances and preform the set routines. Please note: I am not covering ways to retrieve the X,Y,Z values of your game objects so that is up to you.
First , to understand the code itself let us take an look at the geometrical 2D Distance formula...
load link to view 2D Formula Image http://upload.wikimedia.org/math/7/0/f/70f21f8d56472663b4a3daf55117b78c.png
Translation:
distance = DeltaX2 - DeltaX1 sqrd + DeltaY2 - DeltaY1 sqrd SQRT
If you read the formula, you should understand the code I wrote with no problem at all.
Demonstration Code: (* Full Source *)
#include
#include
long calculate_2D(long x1,long x2, long y1, long y2);
long sqrd(value);
using namespace std;
int main()
{
cout<<"Calculating the displacements of: X1=3.5,X2=6.7,Y1=9.8,Y2=1.4"<