Hey there again. Im starting to get the hang of C++, enough for me to work on a game - but ive come across a problem when working on the enemy AI. Im trying to make BOIDS and ive ported some good AS3 code that ive used in the past. But the problem im having is one tiny part of this code is causing the weirdest #NAN problem.
float targetVectorX; float targetVectorY; float targetDist;
target = world->getNeighbour(Entity::ALLY_TYPE, this);
targetVectorX = target->posX - posX;
targetVectorY = target->posY - posY;
targetDist = sqrtf(targetVectorX * targetVectorX + targetVectorY * targetVectorY);
targetVectorX /= targetDist;
targetVectorY /= targetDist;
float dot; float cross;
dot = (vecX * targetVectorX + vecY * targetVectorY);
cross = (vecX * targetVectorY - targetVectorX * vecY);
/// PROBLEMS OCCUR HERE /////////
float angleDiff;
if (dot > 0)
{
angleDiff = asinf(cross);
}
else if (dot < 0)
{
angleDiff = PI - asinf(cross);
}
angleDiff = abs(angleDiff);
////////////////////////////////////////
if (cross > 0)
{
rotationVel += angleDiff * turnSpeed;
if (abs(rotationVel) > maxRotationVel) rotationVel = maxRotationVel * (rotationVel / abs(rotationVel));
angle += rotationVel;
}
else if (cross < 0)
{
rotationVel += angleDiff * turnSpeed;
if (abs(rotationVel) > maxRotationVel) rotationVel = maxRotationVel * (rotationVel / abs(rotationVel));
angle -= rotationVel;
}
vecX = cosf(angle);
vecY = sinf(angle);
posX += vecX * moveSpeed;
posY += vecY * moveSpeed;
rotation = angle * RAD_TO_DEG;
Now what happens is the angleDiff gets screwed up and in turn breaks the rest of the game. If i take that bit out the AI works fine, just a bit fast.
My hunch is with it being somthing to do with massive numbers being produced from the ASINF();
Any help would be amazing -> thank you!