Tutorial sur les collisions

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
GuyTina
Posts: 108
Joined: Fri Nov 12, 2004 11:09 am
Location: FRANCE

Tutorial sur les collisions

Post by GuyTina »

Bonjour tout le Monde. :idea:

Avez-vous d'autre tutoriaux ou demos sur les collisions? :?:


Merci beaucoup :D

Cordialement :D
gromito
Posts: 15
Joined: Tue Feb 08, 2005 3:59 pm
Location: Clermont ferrand, FRANCE

Post by gromito »

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.

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;
}
en espérant que ca va t aider.
Remerciements...

Post by Remerciements... »

Bonjour tout le Monde. :D


Je te remercie beaucoup pour ta réponse et surtout pour ton code qui va m'etre tres utile. :D

En été il fait tres chaud dans ta ville ,j'ai de la famille à Pont du Chateau,moi je suis pres de Nancy... :wink:

Je te remercie. :D

Cordialement :D
GabySoft
Posts: 4
Joined: Sat Mar 05, 2005 10:44 am
Contact:

Post by GabySoft »

Excuse me, this works in the v. 0.8 ?
Bye!
gromito
Posts: 15
Joined: Tue Feb 08, 2005 3:59 pm
Location: Clermont ferrand, FRANCE

Post by gromito »

yes but you just change

Code: Select all

ISceneNodeAnimator* anim = smgr->createCollisionResponseAnimator(
      metaselector, camera, core::vector3df(30,50,30),
      vector3df(0,-100,0), 100.0f, vector3df(0,50,0)); 
en

Code: Select all

ISceneNodeAnimator* anim = smgr->createCollisionResponseAnimator(
      metaselector, camera, core::vector3df(30,50,30),
      vector3df(0,-100,0), vector3df(0,50,0), 100.0f); 
that's all
Post Reply