Historique des modifications - Message

Message #10694

Sujet: Test de gravité - Irrlicht + Newton


Type Date Auteur Contenu
Création du message 23-08-2012 06:29:41 johnplayer
Je crois que n'as pas compris ce que je voulais dire, utilise ce code :

Physics.hpp
#ifndef PHYSICS_HPP
#define PHYSICS_HPP

#include "main.hpp"

class Physics
{
    public :
   
    Physics(irr::IrrlichtDevice *device, irr::video::IVideoDriver *driver, irr::scene::ISceneManager *sceneManager);
    ~Physics();
   
    void Cube(irr::core::vector3df position, irr::core::vector3df taille, float masse);
    void Update();
   
    // Callbacks ---------------------------------------------------------------
   
    // 1.53
    /*
    static void SetMeshTransformEvent(const NewtonBody* newtonBody, const float* matrix);
    static void ApplyForceAndTorqueEvent(const NewtonBody* body);
    */
    // 2.00
   
    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;
   
};

#endif

Physics.cpp
#include "Physics.hpp"

// sans ces lignes les variables statiques sont mal déclarées d'où le "undefined reference"
NewtonWorld * Physics::newtonWorld = NULL;
NewtonBody * Physics::newtonBody = NULL;

Physics::Physics(irr::IrrlichtDevice *device, irr::video::IVideoDriver *driver, irr::scene::ISceneManager *sceneManager){                   
    newtonDevice = device;
    newtonDriver = driver;
    newtonSceneManager = sceneManager;
   
    newtonWorld = NewtonCreate(); // 2.0
    newtonNode = 0;
}Physics::~Physics(){
    NewtonDestroy(newtonWorld);
}

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;
   
    collision = NewtonCreateBox(newtonWorld, taille.X, taille.Y, taille.Z, 0, NULL); // 2.00
   
    newtonBody = NewtonCreateBody(newtonWorld, collision, NULL); // 2.00
    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);
    }
   
    irr::core::matrix4 mat;
    mat.setTranslation(position);
    NewtonBodySetMatrix(newtonBody, mat.pointer());
}

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

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

// 2.00

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;
    force[2] = 0.0f;
   
    torque[0] = 0.0f;
    torque[1] = 0.0f;
    torque[2] = 0.0f;
   
    NewtonBodyAddForce(newtonBody, force);
    NewtonBodyAddTorque(newtonBody, torque);
}

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

Retour

Options Liens officiels Caractéristiques Statistiques Communauté
Préférences cookies
Corrections
irrlicht
irrklang
irredit
irrxml
Propulsé par Django
xhtml 1.0
css 2.1
884 membres
1440 sujets
11337 messages
Dernier membre inscrit: Saidov17
524 invités en ligne
membre en ligne: -
RSS Feed