boid.cpp

00001 #include "boid.h"
00002 #include "debug.h"
00003 
00004 using namespace irr;
00005 using namespace core;
00006 using namespace video;
00007 using namespace scene;
00008 
00009 extern Debug* dbg;
00010 
00011 Boid::Boid(IrrlichtDevice* d, const c8* filePath)
00012 {
00013     //ctor
00014     device = d;
00015     meshFilePath.append(filePath);
00016     loadMesh();
00017     // dbg:
00018     gui::IGUIFont* font = device->getGUIEnvironment()->getBuiltInFont();
00019     dbgText = device->getSceneManager()->addTextSceneNode(font,
00020                 L"", SColor(100, 255, 255, 255), mesh, vector3df(0, 20, 0), -1);
00021 }
00022 
00023 Boid::Boid(IrrlichtDevice* d, Boid& other)
00024 {
00025     //ctor
00026     device = d;
00027     position = other.position;
00028     velocity = other.velocity;
00029     speed = other.speed;
00030     forward = other.forward;
00031     meshFilePath = other.meshFilePath;
00032     loadMesh();
00033     // dbg:
00034     gui::IGUIFont* font = device->getGUIEnvironment()->getBuiltInFont();
00035     dbgText = device->getSceneManager()->addTextSceneNode(font,
00036                 L"", SColor(100, 255, 255, 255), mesh, vector3df(0, 20, 0), -1);
00037 }
00038 
00039 Boid::~Boid()
00040 {
00041     //dtor
00042     device = NULL;
00043     mesh->remove();
00044 }
00045 
00046 void Boid::update(u32 deltaTime)
00047 {
00048     position += velocity * (deltaTime/1000.0f);
00049     mesh->setPosition(position);
00050     mesh->setRotation(velocity.getHorizontalAngle());
00051     forward = velocity;
00052 }
00053 
00054 void Boid::loadMesh()
00055 {
00056     ISceneManager* smgr = device->getSceneManager();
00057     mesh = smgr->addAnimatedMeshSceneNode(smgr->getMesh(meshFilePath.c_str()) );
00058     if (mesh)
00059     {
00060         mesh->setMaterialFlag(EMF_LIGHTING, false);
00061     }
00062     else
00063     {
00064         dbg->log("Boid::loadMesh() cannot load", meshFilePath );
00065         mesh = (IAnimatedMeshSceneNode*) smgr->addTextSceneNode(device->getGUIEnvironment()->getBuiltInFont(),
00066             L"cannot load mesh", SColor(100,255,255,255));
00067     }
00068 }
00069 
00070 void Boid::setScale(f32 scale)
00071 {
00072     mesh->setScale(vector3df(scale, scale, scale));
00073 }
00074 
00075 bool Boid::inBoidNeighborhood(Boid* other, f32 minDistance, f32 maxDistance, f32 cosMaxAngle)
00076 {
00077     if (other == this)
00078     {
00079         return false;
00080     }
00081     else
00082     {
00083         vector3df offset = other->position - position;
00084         f32 distanceSquared = offset.getLengthSQ();
00085 
00086         // definitely in neighborhood if inside minDistance sphere
00087         if (distanceSquared < (minDistance * minDistance))
00088         {
00089             return true;
00090         }
00091         else
00092         {
00093             // definitely not in neighborhood if outside maxDistance sphere
00094             if (distanceSquared > (maxDistance * maxDistance))
00095             {
00096                 return false;
00097             }
00098             else
00099             {
00100                 // otherwise, test angular offset from forward axis
00101                 vector3df unitOffset = offset / sqrt(distanceSquared);
00102                 f32 forwardness = forward.dotProduct(unitOffset);
00103                 return forwardness > cosMaxAngle;
00104             }
00105         }
00106     }
00107 }
00108 
00109 void Boid::addDebugText(c8* text, s32 value)
00110 {
00111     // holds all texts ever added, but no text more than once
00112     static array<stringw> texts;
00113 
00114     stringw searchFor(text);
00115     s32 len = searchFor.size();
00116     for (u32 i=0; i<texts.size(); i++)
00117     {
00118         if (texts[i].equalsn(searchFor, len))
00119         {
00120             // found text, delete it
00121             texts.erase(i);
00122             break;
00123         }
00124     }
00125 
00126     // add text to array
00127     c8 buf[256];
00128     sprintf(buf, "%s:%i", text, value);
00129     stringw tmp(buf);
00130     texts.push_back(tmp);
00131 
00132     // build whole debug text
00133     stringw display(L"");
00134     for (u32 i=0; i<texts.size(); i++)
00135     {
00136         display.append(texts[i]);
00137         display.append("  ");
00138     }
00139 
00140     dbgText->setText(display.c_str());
00141 }

Generated on Sun Dec 2 17:09:57 2007 for Swarm by  doxygen 1.4.6-NO