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
}