Bonjour tout le Monde.
Avez-vous d'autre tutoriaux ou demos sur les collisions?
Merci beaucoup
Cordialement
Tutorial sur les collisions
un code que j ai ecrit qui permet les collisions et le ramassage de plusieurs boites lorsque l one st a proximité et qu on les vise.
J ai mis quelques commentaires sur le code.
en espérant que ca va t aider.
J ai mis quelques commentaires sur le code.
Code: Select all
#include <irrlicht.h>
using namespace irr;
using namespace video;
using namespace scene;
using namespace core;
using namespace gui;
char sbuffer[50];
IrrlichtDevice *device=0;
//Ajout de la gestion des évenements
class MyEventReceiver : public IEventReceiver
{
public:
virtual bool OnEvent(SEvent event)
{
if(event.EventType==EET_MOUSE_INPUT_EVENT) {
if(event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
//Action lors du clique gauche
printf("click!\n");
}
}
return true;
}
};
int main()
{
bool munition1 = true;
bool munition2 = true;
bool munition3 = true;
MyEventReceiver receiver;
//Creation de la fenetre d'affichage
device = createDevice(EDT_OPENGL, dimension2d<s32>( 800, 600), 16, false, false, false);
//Gestionnaire de video
IVideoDriver* driver = device->getVideoDriver();
//gestionnaire de video
ISceneManager* smgr = device->getSceneManager();
//gestionnair e du GUI
IGUIEnvironment* guienv = device->getGUIEnvironment();
//Ajout de texte à l'ecran
guienv->addStaticText( L"demo de gromito", rect<int>( 10, 10, 200, 22), true);
//Chargement de la map
IAnimatedMesh* mapmesh = smgr->getMesh("maps/niveau01.3DS");
IAnimatedMeshSceneNode* map = smgr->addAnimatedMeshSceneNode( mapmesh );
map->setMaterialTexture( 0, driver->getTexture("maps/Wall06.jpg"));
//Chargement munitions 1
IAnimatedMesh* ammomesh1 = smgr->getMesh("maps/mp.3ds");
IAnimatedMeshSceneNode* ammo1=smgr->addAnimatedMeshSceneNode(ammomesh1);
ammo1->setPosition(vector3df(0,20,500));
ammo1->setScale( vector3df( 2, 2, 2));
//Chargement munitions 2
IAnimatedMesh* ammomesh2 = smgr->getMesh("maps/mp.3ds");
IAnimatedMeshSceneNode* ammo2=smgr->addAnimatedMeshSceneNode(ammomesh2);
ammo2->setPosition(vector3df(0,20,100));
ammo2->setScale( vector3df( 2, 2, 2));
//Chargement munitions 3
IAnimatedMesh* ammomesh3 = smgr->getMesh("maps/mp.3ds");
IAnimatedMeshSceneNode* ammo3=smgr->addAnimatedMeshSceneNode(ammomesh3);
ammo3->setPosition(vector3df(500,20,0));
ammo3->setScale( vector3df( 2, 2, 2));
//Chargement d'un mechant
IAnimatedMesh* persomesh = smgr->getMesh("maps/sydney.md2");
IAnimatedMeshSceneNode* perso = smgr->addAnimatedMeshSceneNode(persomesh);
perso->setScale( vector3df( 2, 2, 2));
perso->setPosition(vector3df( -178, -135, -3259));
perso->setMaterialTexture( 0, driver->getTexture("maps/sydney.bmp"));
perso->setMD2Animation( EMAT_STAND);
//Ajout de la source de la lumiere
map->setMaterialFlag(EMF_LIGHTING, false);
ammo1->setMaterialFlag(EMF_LIGHTING, false);
ammo2->setMaterialFlag(EMF_LIGHTING, false);
ammo3->setMaterialFlag(EMF_LIGHTING, false);
perso->setMaterialFlag( EMF_LIGHTING, false);
//Ajout de la camera
ICameraSceneNode* camera=smgr->addCameraSceneNodeFPS();
camera->setPosition(vector3df(100,100,0)); //Positionnement de la camera
camera->setFarValue( 10000);
//Gestion des collisions
IMetaTriangleSelector* metaselector;
metaselector = smgr->createMetaTriangleSelector();
//Collisions entre la camera et la map
ITriangleSelector* mapselector = smgr->createOctTreeTriangleSelector(mapmesh->getMesh(0), map, 128);
metaselector->addTriangleSelector(mapselector);
mapselector->drop();
//Collision entre la camera et munitions1
ITriangleSelector* ammoselector1 = smgr->createTriangleSelector(ammomesh1->getMesh(0), ammo1);
metaselector->addTriangleSelector(ammoselector1);
ammoselector1->drop();
//Collision entre la camera et munitions2
ITriangleSelector* ammoselector2 = smgr->createTriangleSelector(ammomesh2->getMesh(0), ammo2);
metaselector->addTriangleSelector(ammoselector2);
ammoselector2->drop();
//Collision entre la camera et munitions3
ITriangleSelector* ammoselector3 = smgr->createTriangleSelector(ammomesh3->getMesh(0), ammo3);
metaselector->addTriangleSelector(ammoselector3);
ammoselector3->drop();
//Collision entre la camera et le perso
ITriangleSelector* persoselector = smgr->createTriangleSelector( persomesh->getMesh(0), perso);
metaselector->addTriangleSelector(persoselector);
persoselector->drop();
ISceneNodeAnimator* anim = smgr->createCollisionResponseAnimator(
metaselector, camera, core::vector3df(30,50,30),
vector3df(0,-100,0), 100.0f, vector3df(0,50,0));
camera->addAnimator(anim);
anim->drop();
//Curseur non visible
device->getCursorControl()->setVisible(false);
//Ajout de l'image de la crosshair
ITexture* images = driver->getTexture("maps/crosshair.bmp");
driver->makeColorKeyTexture(images,position2d<s32>(0,0));
while(device->run())
{
driver->beginScene(true, true, video::SColor(0,200,200,200));
//Dessin de la scene
smgr->drawAll();
//Ajout de la crosshair
position2d<s32> m = device->getCursorControl()->getPosition();
driver->draw2DImage(images,m, rect<s32>(0,0,16,16), 0, SColor(100,255,255,255), true);
//Dessin du GUI
guienv->drawAll();
//Fin de la scene
driver->endScene();
//Calcul de la collision entre le perso et les munitions
vector3d<f32> direction = camera->getTarget()-camera->getPosition();
direction.normalize();
line3d<f32> bullet;
bullet.start = camera->getPosition();
bullet.end = camera->getPosition()+direction * 100;
triangle3d<f32> dummy;
vector3d<f32> mapcollision;
vector3d<f32> modelcollision;
bool gotmodel1 = smgr->getSceneCollisionManager()->getCollisionPoint(bullet,ammoselector1,modelcollision,dummy);
bool gotmodel2 = smgr->getSceneCollisionManager()->getCollisionPoint(bullet,ammoselector2,modelcollision,dummy);
bool gotmodel3 = smgr->getSceneCollisionManager()->getCollisionPoint(bullet,ammoselector3,modelcollision,dummy);
if(gotmodel1){
printf("ramassage arme");
munition1 = false;
ammo1->setPosition(vector3df( 10000, 10000, 10000));
}
if(gotmodel2){
printf("ramassage arme");
munition2 = false;
ammo2->setPosition(vector3df( 10000, 10000, 10000));
}
if(gotmodel3){
printf("ramassage arme");
munition3 = false;
ammo3->setPosition(vector3df( 10000, 10000, 10000));
}
vector3df<f32> cam_position;
cam_position = camera->getPosition();
int len = sprintf(sbuffer,"Position camera: %f %f %f\n \0", cam_position.X,cam_position.Y,cam_position.Z);
//printf("len: %d - %s", len, sbuffer);
}
device->drop();
return 0;
}
yes but you just change
en
that's all
Code: Select all
ISceneNodeAnimator* anim = smgr->createCollisionResponseAnimator(
metaselector, camera, core::vector3df(30,50,30),
vector3df(0,-100,0), 100.0f, vector3df(0,50,0));
Code: Select all
ISceneNodeAnimator* anim = smgr->createCollisionResponseAnimator(
metaselector, camera, core::vector3df(30,50,30),
vector3df(0,-100,0), vector3df(0,50,0), 100.0f);