View Full Version : simulate crowd animation by flocking

 seadonghai10 October 2006, 03:45 AMthis is a demo to simulate a group of fish for a game . it is easy to simulate crowd animation by flocking system .there are some shoot screen http://features.cgsociety.org/gallerycrits/177099/177099_1159933092_submedium.jpg http://features.cgsociety.org/gallerycrits/177099/177099_1159933054_submedium.jpg http://features.cgsociety.org/gallerycrits/177099/177099_1159932981_submedium.jpg But flocking is simple .I think fuzzy logic is better .I will use fuzzy logic and maya API to simulate crowd animation in the future.
hogobingo
10 October 2006, 05:44 PM
best try but can u show me the code please

10 October 2006, 04:36 AM
you know there are a lots of class ,I can show you the main function code .

#include "main.h"
using namespace irr;

using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
int i;
#define Maxnumber 100

box *box1;
boid *boid_fish[Maxnumber];
flock *flock_test;
obstacle *obs[MaxObstacles];
vector temp_vector;
vector temp_velocity;
vector temp_angle;
scene::ISceneNode* fishnode[Maxnumber] ;
//scene::ISceneNode* obsnode[MaxObstacles];
scene::IAnimatedMeshSceneNode* obsnode[MaxObstacles];
scene::IAnimatedMeshSceneNode* node;
scene::IAnimatedMeshSceneNode* earthnode;
scene::ICameraSceneNode* camera;
//core::vector3df temp_vector ;

#pragma comment(lib, "Irrlicht.lib")
void initboid()
{

box1 = new box(50.0,50.0,50.0);
boid_fish[0] = new boid(0);

for(i = 0; i < Maxnumber; i++)
{
boid_fish[i] = new boid(i);

}

flock_test = new flock();
for (i = 0; i < Maxnumber; i++)
{
}

}
void initobstacle()
{
for(i = 0 ; i < MaxObstacles ; i ++)
{
obs[i] = new obstacle(20.0);
//obs[i] ->Draw();
}
}
void boidupdate()
{
/*for(i = 0; i < Maxnumber ; i++)
{

boid_fish[i]->flockit(i,boid_fish[0]);
temp_vector = boid_fish[i]->boid_position;
fishnode[i] ->setPosition(core::vector3df(temp_vector.x ,
temp_vector.y ,
temp_vector.z ));
//printf("temp_vector.x = %f\n" ,temp_vector.x );
}*/

for (i = 0; i < flock::FlockCount; i++)
{

flock::ListOfFlocks[i]->Update();

}
for (i = 0; i < Maxnumber; i++)
{

temp_angle = boid_fish[i]->boid_angle;
temp_vector = boid_fish[i]->boid_position;
fishnode[i] ->setPosition(core::vector3df(temp_vector.x ,
temp_vector.y ,
temp_vector.z ));
fishnode[i] ->setRotation(core::vector3df(temp_angle.x *180/3.14,
temp_angle.y*180/3.14 ,
temp_angle.z*180/3.14 ));

}

}

void deleteboid()
{
for (i = 0; i < Maxnumber; i++)
{
delete boid_fish[i];
}
}
void deleteonstacle()
{
for (i = 0; i < MaxObstacles; i++)
{
delete obs[i];
}
}

int main()
{
//引擎初始化
IrrlichtDevice *device =
createDevice(EDT_DIRECTX8, dimension2d<s32>(640, 480), 16,
false, false, false, 0);

device->setWindowCaption(L"flocking Demo");

IVideoDriver* driver = device->getVideoDriver();
ISceneManager* smgr = device->getSceneManager();
IGUIEnvironment* guienv = device->getGUIEnvironment();

//guienv->addStaticText(L"Hello World! This is the Irrlicht Software renderer!",
// rect<int>(10,10,200,22), true);
//场景初始化

initboid();
initobstacle();
video::SMaterial material_xiaoliyu;
video::SMaterial material_earth;

//material_xiaoliyu.Texture1 = driver->getTexture("image/xiaoliyu-an.jpg");
//material_xiaoliyu.Lighting = false;
//material_xiaoliyu.FogEnable = true;
//node->setMaterialTexture(0, driver->getTexture("image/xiaoliyu-an.jpg"));

//material_earth.Texture1 = driver->getTexture("image/xiaoliyu-an.jpg");
//node->setMaterialTexture(0, driver->getTexture("image/xiaoliyu-an.jpg"));
//node->setMaterialTexture(0, driver->getTexture("image/earth.bmp"));
material_earth.Texture1 = driver->getTexture("model/earth.bmp");
material_earth.Lighting = false;
//material_earth.Wireframe = true;

scene::IAnimatedMesh* earth= smgr->getMesh("model/earth.x");

if(earth)
{
for (i = 0; i < MaxObstacles; i++)
{
temp_vector = obs[i]->GetPosition();
//earthnode = smgr->addAnimatedMeshSceneNode( earth , obsnode[i]);
obsnode[i]->setPosition(core::vector3df(temp_vector.x ,
temp_vector.y ,
temp_vector.z ));
obsnode[i]->setScale(core::vector3df(150.0 ,
150.0 ,
150.0 ));
}
}
//scene::IAnimatedMesh* xiaoliyu= smgr->getMesh("model/xiaoliyu-an-sm.x");
//if (xiaoliyu)
// {
for (i = 0; i < Maxnumber; i++)
{

temp_vector = boid_fish[i]->boid_position;
temp_velocity =boid_fish[i]->boid_velocity;
//node = smgr->addAnimatedMeshSceneNode( xiaoliyu , fishnode[i]);
//node->setMaterialTexture(0, driver->getTexture("image/xiaoliyu-an.jpg"));

//node->setScale(core::vector3df(3.0f,3.0f,3.0f));
//node->setFrameLoop(0, 6000);
//node->setAnimationSpeed(2000 + boid_fish[i]->boid_id* 50 );

fishnode[i] ->setScale(core::vector3df(1.0,1.0,3.0 ));
fishnode[i] ->setPosition(core::vector3df(temp_vector.x ,
temp_vector.y ,
temp_vector.z ));

}

// }

//相机设置

camera->setPosition(core::vector3df(-700,-700,-700));
camera->setTarget(core::vector3df(0,0,0));
camera->setFarValue(40000.0f);
device->getCursorControl()->setVisible(false);

while(device->run())
{

driver->beginScene(true, true, SColor(0,100,100,100));
//printf ("aa" );
boidupdate();

smgr->drawAll();
guienv->drawAll();

driver->endScene();

}
deleteboid();
deleteonstacle();
device->drop();
return 0;
}

CGTalk Moderation
10 October 2006, 04:36 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.

1