void BoidManager::updateBirdsPosition() { XMVECTOR acceleration, Seek, Avoid, Align, Wall, CurPos; XMFLOAT4 newPos; float maxSpeed = 0.0f; maxSpeed += 10.0f; if (maxSpeed > 1.0f) maxSpeed = 1.0f; float seekFactor = 1.0f; float avoidFactor = 2.0f; float alignFactor = 1.0f; for (int i = 0; i < maxBirds; i++) { Seek = findSeek(*m_birds.at(i)) * seekFactor; Avoid = findAvoid(*m_birds.at(i)) * avoidFactor; Align = findAlign(*m_birds.at(i)) * alignFactor; //Calculate the desired vector acceleration = m_birds.at(i)->getVector() + Seek + Align + Avoid; //turn around if about to hit wall Wall = avoidWall(*m_birds.at(i), acceleration); acceleration = vMult(XMVector4Normalize(acceleration), Wall); CurPos = XMLoadFloat4(&m_birds.at(i)->getPosition()); CurPos += acceleration; XMStoreFloat4(&newPos, CurPos); m_birds.at(i)->setVector(acceleration); m_birds.at(i)->setPosition(newPos); updateWorldMatrix(*m_birds.at(i)); } }
//=============================================== // Unit Tests //=============================================== int main() { // set for command line argument -t to run unit tests. bool test = true; if (test) { std::cout<<"\n=================\nBegin UNIT TESTS\n=================\n\n"; Light::Light n = *new Light::Light(0, 1, 1, 0, 0.5, 0.5, 0.5); n.print(); Intersect::Intersect i = *new Intersect::Intersect(); if(i.isHit()) { std::cout<<"INCORRECT! hit is true.\n\n"; } else { std::cout<<"CORRECT! hit is false.\n\n"; i.setHit(true); } if(i.isHit()) { std::cout<<"CORRECT! hit is true.\n\n"; } else { std::cout<<"INCORRECT! hit is false.\n\n"; } std::vector<float> ptest(3); ptest[0] = 0; ptest[1] = 1; ptest[2] = 2; i.setPoint(ptest); std::vector<float> p = i.getPoint(); std::cout<< "[" << p[0] << ", " << p[1] << ", " << p[2] << "]\n"; Vertex::Vertex a = *new Vertex::Vertex(0,0,0); Vertex::Vertex b = *new Vertex::Vertex(1,0,0); Vertex::Vertex c = *new Vertex::Vertex(0,1,0); a.print(); a = a.sub(b); a.print(); std::vector<float> d = c.toVec(); std::cout<<"vec ["<< d[0] << ", " << d[1] << ", " << d[2] << "]\n"; Vertex::Vertex e1 = *new Vertex::Vertex(d); e1.print(); Vertex a1 = *new Vertex::Vertex(0,1,0); Vertex b1 = *new Vertex::Vertex(1,-1,0); Vertex c1 = *new Vertex::Vertex(-1,-1,0); Triangle t = *new Triangle::Triangle(a1,b1,c1); p[0] = 0; p[1] = 0; p[2] = 0; std::vector<float> e(3); e[0] = 0; e[1] = 0; e[2] = 3; Ray::Ray r = *new Ray::Ray(e, p); i = t.intersect(r); if (i.isHit()) { std::cout<<"Triangle Intersected - OKAY\n"; } Sphere::Sphere s = *new Sphere::Sphere(1, p); if (s.intersect(r).isHit()) { std::cout<<"Sphere Intersected - OKAY\n"; } e[0] = 0; e[1] = -4; e[2] = -4; p[0] = 1; p[1] = 1; p[2] = -1; r.setEye(e); r.setPoint(p); std::cout<<"projected "; vPrint(r.project(1.5)); PPM* ppm = new PPM(640, 480, 255); int val; for (float h = 0; h<480; h++) { for (float w =0; w<640; w++) { val = h;//std::min(255, (int)((w/639)*255.0f)); ppm->addPixel(*(new Pixel::Pixel(val, val, val))); } } ppm->save("output"); /* for (float h = 0; h < 3; h++) { for (float w = 0; w< ppm->getW(); w++) { std::cout<<w<<" "; ppm->getPixel(w, h).print(); } } */ std::cout<<"End PPM tests\n"; Pixel::Pixel px = *new Pixel::Pixel(255, 255, 255); Pixel::Pixel black = *new Pixel::Pixel(0,0,0); Pixel::Pixel pfloat = *new Pixel::Pixel(1.0f, 0.9f, 0.05f); Pixel::Pixel pmax = *new Pixel::Pixel(2.0f, 1.0f, 0.5f); px.print(); black.print(); black.add(px); black.print(); pfloat.print(); pmax.print(); std::vector<float> test1(3); test1[0] = 1; test1[1] = 0; test1[2] = 0.5; std::vector<float> test2(3); test2[0] = 0; test2[1] = 1; test2[2] = 0.5; std::cout<<vDot(test1, test2)<<"\n"; vPrint(vSub(test1, test2)); vPrint(vAdd(test1, test2)); vPrint(vMult(test1, test2)); vPrint(normalize(test1)); vPrint(normalize(test2)); vPrint(vCross(test1, test2)); vPrint(vScale(-1, test1)); std::cout<<"\n=================\nEnd UNIT TESTS\n=================\n\n"; return 0; } else { return 0; } }