Historique des modifications - Message

Message #10715

Sujet: Test de gravité - Irrlicht + Newton


TypeDateAuteurContenu
Création du message25-08-2012 11:12:15Kaze
Désolé pour le retard donc voila :

main.cpp :

Code c++ :


#include "main.hpp"

int main(){
    
    Evenement engine;
    engine.Init();
    return engine.Run();
    
}


main.hpp :

Code c++ :


#ifndef MAIN_HPP
#define MAIN_HPP

#include <irr/Irrlicht.h>
#include <iostream>

#include "evenement.hpp"

#endif


physics.cpp :

Code c++ :


#include "physics.hpp"

NewtonWorld* Physics::newtonWorld = NULL;
NewtonBody* Physics::newtonBody = NULL;

// CONSTRUCTEUR ----------------------------------------------------------------

Physics::Physics(irr::IrrlichtDevice *device, irr::video::IVideoDriver *driver, irr::scene::ISceneManager *sceneManager){                   
    newtonDevice = device;
    newtonDriver = driver;
    newtonSceneManager = sceneManager;
    
    Physics::newtonWorld = NewtonCreate();
    
    // Initialisation du point initial du monde physique.
    pointS[0] = -500.0;
    pointS[1] = -500.0;
    pointS[2] = -500.0;
    
    // Initialisation du point final du monde physique.
    pointE[0] = 500.0;
    pointE[1] = 500.0;
    pointE[2] = 500.0;
    
    NewtonSetWorldSize(Physics::newtonWorld, pointS, pointE);
    
    newtonNode = NULL;
}

// DESTRUCTEUR -----------------------------------------------------------------

Physics::~Physics(){
    NewtonDestroy(newtonWorld);
}

// BUILDS ----------------------------------------------------------------------

void Physics::Cube(irr::core::vector3df position, irr::core::vector3df taille, float masse){
    
    irr::scene::IMesh* cubeMesh = newtonSceneManager->getMesh("data/smallcube.3ds");
    newtonNode = newtonSceneManager->addMeshSceneNode(cubeMesh);
    newtonNode->setMaterialTexture(0, newtonDriver->getTexture("data/crate.jpg"));
    newtonNode->setMaterialFlag(irr::video::EMF_LIGHTING, false);
    
    NewtonCollision* collision;
    irr::core::matrix4 mat;
    
    collision = NewtonCreateBox(Physics::newtonWorld, taille.X, taille.Y, taille.Z, 0, NULL);
    Physics::newtonBody = NewtonCreateBody(Physics::newtonWorld, collision, mat.pointer());
    NewtonReleaseCollision(newtonWorld, collision); 
    
    NewtonBodySetUserData(newtonBody,reinterpret_cast<void*>(newtonNode));
    
    if(masse == 0.0){newtonNode->setPosition(position);}
    
    if(masse != 0.0){
        irr::core::vector3df inertie;
        inertie.X = (masse/12)*(pow(taille.Y,2)+pow(taille.Z,2));
        inertie.Y = (masse/12)*(pow(taille.X,2)+pow(taille.Z,2));
        inertie.Z = (masse/12)*(pow(taille.X,2)+pow(taille.Y,2));
        NewtonBodySetMassMatrix (newtonBody, masse, inertie.X, inertie.Y, inertie.Z);
       
        NewtonBodySetTransformCallback(newtonBody, SetMeshTransformEvent);
        NewtonBodySetForceAndTorqueCallback(newtonBody, ApplyForceAndTorqueEvent);
    }
    
    mat.setTranslation(position);
    NewtonBodySetMatrix(newtonBody, mat.pointer());
}

void Physics::Player(irr::core::vector3df position, float masse){
    
    irr::scene::IMesh* cubeMesh = newtonSceneManager->getMesh("data/smallcube.3ds");
    newtonNode = newtonSceneManager->addMeshSceneNode(cubeMesh);
    newtonNode->setMaterialTexture(0, newtonDriver->getTexture("data/crate.jpg"));
    newtonNode->setMaterialFlag(irr::video::EMF_LIGHTING, false);
    
    NewtonCollision* collision;
    irr::core::matrix4 mat;
    
    collision = NewtonCreateBox(Physics::newtonWorld, 40, 40, 40, 0, NULL);
    Physics::newtonBody = NewtonCreateBody(Physics::newtonWorld, collision, mat.pointer());
    NewtonReleaseCollision(newtonWorld, collision); 
    
    NewtonBodySetUserData(newtonBody,reinterpret_cast<void*>(newtonNode));
    
    irr::core::vector3df inertie;
    inertie.X = (masse/12)*3200;
    inertie.Y = (masse/12)*3200;
    inertie.Z = (masse/12)*3200;
    NewtonBodySetMassMatrix (newtonBody, masse, inertie.X, inertie.Y, inertie.Z);
       
    NewtonBodySetTransformCallback(newtonBody, SetMeshTransformEvent);
    NewtonBodySetForceAndTorqueCallback(newtonBody, ApplyForceAndTorqueEvent);
    
    mat.setTranslation(position);
    NewtonBodySetMatrix(newtonBody, mat.pointer());
}

void Physics::Sol(){
    
    float largeur = pointE[0] - pointS[0];
    float longueur = pointE[2] - pointS[2];
    irr::core::vector3df position = irr::core::vector3df(0,-100,0);
    
    irr::scene::IAnimatedMesh* solMesh = newtonSceneManager->addHillPlaneMesh("Sol", irr::core::dimension2d<irr::f32>(200,170), irr::core::dimension2d<irr::u32>(5,5));
    
    newtonNode = newtonSceneManager->addMeshSceneNode(solMesh);
    newtonSceneManager->getMeshManipulator()->makePlanarTextureMapping(solMesh->getMesh(0), 0.01f);
    newtonNode->setMaterialTexture(0, newtonDriver->getTexture("data/brique.png"));
    newtonNode->setMaterialFlag(irr::video::EMF_LIGHTING, false);
    
    NewtonCollision* collision;
    irr::core::matrix4 mat;
    
    collision = NewtonCreateBox(Physics::newtonWorld, largeur, 1, longueur, 0, NULL);
    Physics::newtonBody = NewtonCreateBody(Physics::newtonWorld, collision, mat.pointer());
    NewtonReleaseCollision(newtonWorld, collision); 
    
    NewtonBodySetUserData(newtonBody,reinterpret_cast<void*>(newtonNode));
    
    newtonNode->setPosition(position);
    mat.setTranslation(position);
    NewtonBodySetMatrix(newtonBody, mat.pointer());
}

// EVENTS ----------------------------------------------------------------------




// METHODES --------------------------------------------------------------------

void Physics::Update(){
     
    if(newtonDevice->getTimer()->getTime() > lasttick + 10)
    {   
        lasttick = newtonDevice->getTimer()->getTime();
        NewtonUpdate(newtonWorld, 0.01f);
    }
}

// CALLBACKS -------------------------------------------------------------------

void Physics::SetMeshTransformEvent(const NewtonBody* newtonBody, const float* matrix, int)
{
    irr::core::matrix4 mat;
    memcpy(mat.pointer(), matrix, sizeof(float)*16);

    irr::scene::ISceneNode *newtonNode = (irr::scene::ISceneNode *)NewtonBodyGetUserData(newtonBody);
    if (newtonNode)
    {
        newtonNode->setPosition(mat.getTranslation());
        newtonNode->setRotation(mat.getRotationDegrees());
    }
}

void Physics::ApplyForceAndTorqueEvent(const NewtonBody* newtonBody, float, int)
{
    float masse;
    float inertieX;
    float inertieY;
    float inertieZ;
    float force[3];
    float torque[3];
   
    NewtonBodyGetMassMatrix (newtonBody, &masse, &inertieX, &inertieY, &inertieZ);
   
    force[0] = 0.0f;
    force[1] = -9.81 * masse * 10;
    force[2] = 0.0f;
   
    torque[0] = 0.0f;
    torque[1] = 0.0f;
    torque[2] = 0.0f;
   
    NewtonBodyAddForce(newtonBody, force);
    NewtonBodyAddTorque(newtonBody, torque);
}

// -----------------------------------------------------------------------------


physics.hpp :

Code c++ :


#ifndef PHYSICS_HPP
#define PHYSICS_HPP

#include "newt/Newton.h"

#include "main.hpp"

class Physics
{
    public :
    
    // Constructeur - Destructeur ----------------------------------------------
    Physics(irr::IrrlichtDevice *device, irr::video::IVideoDriver *driver, irr::scene::ISceneManager *sceneManager);
    ~Physics();
    
    // Builds ------------------------------------------------------------------
    void Cube(irr::core::vector3df position, irr::core::vector3df taille, float masse);
    void Player(irr::core::vector3df position, float masse);
    void Sol();
    
    // Events ------------------------------------------------------------------
    
    
    
    // Methodes ----------------------------------------------------------------
    void Update();
    
    // Callbacks ---------------------------------------------------------------
    
    static void SetMeshTransformEvent(const NewtonBody* body, const float* matrix, int);
    static void ApplyForceAndTorqueEvent(const NewtonBody* body, float, int);
    
    // -------------------------------------------------------------------------
    
    private :
    
    irr::IrrlichtDevice* newtonDevice;
    irr::video::IVideoDriver* newtonDriver;
    irr::scene::ISceneManager* newtonSceneManager;
    
    static NewtonWorld* newtonWorld;
    static NewtonBody* newtonBody;
    irr::scene::ISceneNode* newtonNode;
    
    unsigned int lasttick;
    float pointS[3];
    float pointE[3];
};

#endif


evenement.cpp :

Code c++ :


#include "evenement.hpp"

Evenement::Evenement(){}

void Evenement::Init(){
    
    eventDevice = irr::createDevice(irr::video::EDT_OPENGL, irr::core::dimension2d<irr::u32>(640,480), false);
    eventDriver = eventDevice->getVideoDriver();
    eventSceneManager = eventDevice->getSceneManager();
    eventSceneManager->addSkyBoxSceneNode(eventDriver->getTexture("data/irrlicht2_up.bmp"),eventDriver->getTexture("data/irrlicht2_dn.bmp"),eventDriver->getTexture("data/irrlicht2_rt.bmp"),eventDriver->getTexture("data/irrlicht2_lf.bmp"),eventDriver->getTexture("data/irrlicht2_ft.bmp"),eventDriver->getTexture("data/irrlicht2_bk.bmp"));
    
    eventDevice->setWindowCaption(L"Test sur les evenements lies a la physique.");
    
    // Caméra
    irr::scene::ICameraSceneNode* camera = eventSceneManager->addCameraSceneNode();
    camera->setPosition(irr::core::vector3df(200,100,0));
    camera->setTarget(irr::core::vector3df(0,0,0));
    
    // Installation de la physique.
    Physics newton(eventDevice, eventDriver, eventSceneManager);
    newton.Sol();
    
    // Création d'un cube soumis à la gravité.
    irr::core::vector3df positionA = irr::core::vector3df(0,50,0);
    irr::core::vector3df tailleA = irr::core::vector3df(40,40,40);
    float masseA = 100.0;
    newton.Cube(positionA, tailleA, masseA);
    
    // Création d'un cube soumis à la gravité.
    irr::core::vector3df positionB = irr::core::vector3df(0,0,25);
    irr::core::vector3df tailleB = irr::core::vector3df(40,40,40);
    float masseB = 100.0;
    newton.Cube(positionB, tailleB, masseB);
    
    // Création d'un cube controler.
    irr::core::vector3df positionC = irr::core::vector3df(-50,-79,0);
    float masseC = 100.0;
    newton.Player(positionC, masseC);
    
    eventDevice->setEventReceiver(this);
}

int Evenement::Run(){
    while (eventDevice->run())
    {
        newton->Update();
        
        eventDriver->beginScene(true, true, irr::video::SColor(255,255,255,255));
        eventSceneManager->drawAll();
        eventDriver->endScene();
    }
    eventDevice->drop();
	return 0;
}

bool Evenement::OnEvent(const irr::SEvent& event){
     
    if(event.EventType == irr::EET_MOUSE_INPUT_EVENT)
	{
        if(event.MouseInput.Event == irr::EMIE_LMOUSE_PRESSED_DOWN)
        {
            //newton->Cube(irr::core::vector3df(0,50,0), irr::core::vector3df(40,40,40), 100.0);
		    return true;
        }
	}
    return false;
}


evenement.hpp :

Code c++ :


#ifndef EVENEMENT_HPP
#define EVENEMENT_HPP

#include "main.hpp"
#include "physics.hpp"

class Physics;

class Evenement : public irr::IEventReceiver
{
    public :
    
    Evenement();
    void Init();
    int Run();
    
    protected :
              
    virtual bool OnEvent(const irr::SEvent& event);
    
    private :
    
    irr::IrrlichtDevice* eventDevice;
    irr::video::IVideoDriver* eventDriver;
    irr::scene::ISceneManager* eventSceneManager;
    
    Physics* newton;
    
};

#endif

Retour

OptionsLiens officielsCaractéristiquesStatistiquesCommunauté
Préférences cookies
Corrections
irrlicht
irrklang
irredit
irrxml
Propulsé par Django
xhtml 1.0
css 2.1
884 membres
1441 sujets
11339 messages
Dernier membre inscrit: Saidov17
129 invités en ligne
membre en ligne: -
RSS Feed