Simple AI test

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
dejai
Posts: 522
Joined: Sat Apr 21, 2007 9:00 am

Simple AI test

Post by dejai »

I was wondering if this was any good for a simple following AI, I am yet to learn about cast line

not complete code

Code: Select all


while (device->run())
{
vector3df playerpos = player_node->getPosition();
vector3df enemypos = enemy_node->getPosition();
vector3df encounter = playerpos - enemypos;

if (encounter <= 50 || encounter >= -50)
{
// Goto Player Attack 
}
}
Just asking is the concept of the code correct, I am almost sure that I have done something wrong and I may have to do something more like this.

Code: Select all

vector3df playerpos = player_node->getPosition();
vector3df enemypos = enemy_node->getPosition();
vector3df encounter = playerpos - enemypos;
vector3df distance;
distance.X = 50;
distance.Y = 50; 
distance.Z = 50;

if (encounter <=distance  || encounter >= -distance)
{
// Goto Player Attack 
}

}
[/code]
Programming Blog: http://www.uberwolf.com
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Try to generalise your problems. At this stage, AI is a red herring. The general problem is that you want to know how to calculate how far apart two scene nodes are.

That means (thiiiiink) that you want to know the length of the vector between the two nodes, right? So...

Code: Select all

vector3df playerpos = player_node->getPosition();
vector3df enemypos = enemy_node->getPosition();
vector3df difference = playerpos - enemypos;

f32 distance = difference.getLength();
or, using a handy little vector3d method, just this:

Code: Select all

f32 distance = player_node->getPosition().getDistanceFrom(enemy_node->getPosition());
Note that both getLength() and getDistanceFrom() perform a sqrt() calculation, which is relatively computationally expensive. It's more efficient to do getLengthSQ() or getDistanceFromSQ(), and then compare the result to the (pre-calculated) square of the attack distance.

Code: Select all

enum
{
    ATTACK_DISTANCE = 50,
    ATTACK_DISTANCE_SQUARED = 50 * 50
};

// Not very efficient
f32 distance = player_node->getPosition().getDistanceFrom(enemy_node->getPosition())

if(distance < ATTACK_DISTANCE)
{
    // attack
} 

// More efficient
f32 distanceSQ = player_node->getPosition().getDistanceFromSQ(enemy_node->getPosition())

if(distanceSQ < ATTACK_DISTANCE_SQUARED)
{
    // attack
} 
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

Smart :idea:..
Post Reply