New in irrlicht and newton need help

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Paio

New in irrlicht and newton need help

Post by Paio »

excuse badly by my ingles, but it is not my native language.
I am new in irrlicht and at the moment I am implementing a small scen with Newton where a box moves by a scen in x that is simply a floor and some boxes. The code is executed, but the objects go away downwards and it does not control the collision to me between the objects.
Code is the following one:

Code: Select all


#include <irrlicht.h>
#include <newton.h>
#include <iostream>
#include <dvector.h>

using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

// Define all the variables we will be needing

NewtonWorld *nWorld;


IAnimatedMeshSceneNode *carro;
IAnimatedMeshSceneNode *mapa;

const float NewtonToIrr= 32.0f;

IrrlichtDevice* device;
IVideoDriver* driver;
ISceneManager* smgr;
IGUIEnvironment* guienv;

int go=0;

class MyEventReceiver : public IEventReceiver 
{ 
public: 
        virtual bool OnEvent(SEvent event) 
        { 
          if (event.EventType == EET_KEY_INPUT_EVENT) 
          { 
            if (event.KeyInput.Key == KEY_ESCAPE && !event.KeyInput.PressedDown ) 
            { 
            device->closeDevice(); 
            return true; 
            } 
          //vector3df v = cube->getPosition(); 
          if (event.KeyInput.Key == KEY_KEY_W && go!=2) 
          { 
            //v.Z+=10; 
            //cube->setPosition(v); 
            go=1;
            return true; 
          } 
          if (event.KeyInput.Key == KEY_KEY_S && go!=1) 
          { 
            //v.Z-=10; 
            //cube->setPosition(v); 
            go=2;
            return true; 
          } 
          if (event.KeyInput.Key == KEY_KEY_A && go!=4) 
          { 
            //v.X-=10; 
            //cube->setPosition(v); 
            go=3;
            return true; 
          } 
          if (event.KeyInput.Key == KEY_KEY_D && go!=3) 
          { 
            //v.X+=10; 
            //cube->setPosition(v); 
            go=4;
            return true; 
          } 
               
         } 
        return false; 
        } 
}; 

void moviendo(ISceneNode* cube)
{
 vector3df v = cube->getPosition(); 
 if (go == 1 ) 
          { 
            v.Z+=1; 
            cube->setPosition(v); 
          } 
          if (go == 2) 
          { 
            v.Z-=1; 
            cube->setPosition(v); 
          } 
          if (go == 3) 
          { 
            v.X-=1; 
            cube->setPosition(v); 
          } 
          if (go == 4) 
          { 
            v.X+=1; 
            cube->setPosition(v); 
          }
}


static void setForceAndTorque(const NewtonBody* body)
{
       float mass;
       float Ixx;
       float Iyy;
       float Izz;
       
       NewtonBodyGetMassMatrix(body,&mass, &Ixx, &Iyy, &Izz);
       
       core::vector3df force(0,-mass*9.8,0.0);
       core::vector3df torque(0,0,0);
       
       NewtonBodyAddForce(body, &force.X);
       NewtonBodyAddTorque(body, &torque.X);
}


static void setTransform(const NewtonBody* body, const float* matrix)
{
       //get objeto
       ISceneNode* node=(ISceneNode*)NewtonBodyGetUserData(body);
       // copiar la matriz a una matriz de Irrlicht
       core::matrix4 mat;
       memcpy(mat.M, matrix, sizeof(float)*16);
       //set transformacion
       node->setPosition(mat.getTranslation()* NewtonToIrr);
}


int main(){

    MyEventReceiver receiver;
    
    // OpenGL device.
	device = createDevice(EDT_OPENGL, dimension2d<s32>(640, 480), 32, false, false, false, &receiver);    device->setWindowCaption(L"IrrNewton Template");
    // Video driver device.
    driver = device->getVideoDriver();
    smgr = device->getSceneManager();
    guienv = device->getGUIEnvironment();
    
    //Add a Newton World.
    nWorld = NewtonCreate(NULL, NULL);
    
    // Set up default material properties for newton
	int i = NewtonMaterialGetDefaultGroupID(nWorld);
	NewtonMaterialSetDefaultFriction   (nWorld, i, i, 0.8f, 0.4f);
	NewtonMaterialSetDefaultElasticity (nWorld, i, i, 0.3f);
	NewtonMaterialSetDefaultSoftness   (nWorld, i, i, 0.05f);
	NewtonMaterialSetCollisionCallback (nWorld, i, i, NULL, NULL, NULL, NULL);

    
    
    // Add an FPS camera to the scene.
    ICameraSceneNode *nCamera = smgr->addCameraSceneNodeFPS();
    // Add a light so it'll be easier to distinguish the angles of the test map.
    smgr->addLightSceneNode(0, vector3df(0.0f, 100.0f, 100.0f));

// add skybox
	smgr->addSkyBoxSceneNode(driver->getTexture("irrlicht2_up.bmp"),driver->getTexture("irrlicht2_dn.bmp"),driver->getTexture("irrlicht2_lf.bmp"),driver->getTexture("irrlicht2_rt.bmp"),driver->getTexture("irrlicht2_ft.bmp"),driver->getTexture("irrlicht2_bk.bmp"));


 
    // Load the world into Irrlicht.
    mapa = smgr->addAnimatedMeshSceneNode(smgr->getMesh("mapa.x"));
    mapa->setMaterialFlag(EMF_LIGHTING,false);



    // Load the box into Irrlicht.
    carro = smgr->addAnimatedMeshSceneNode(smgr->getMesh("car.x"));
    carro->setMaterialFlag(EMF_LIGHTING,false);
    
    
////////////////////////////////////////////
//    DEFINICIONES DE NEWTON
////////////////////////////////////////////    
    
    // Define Newton Collision
    NewtonCollision* myShape= NewtonCreateBox(nWorld, 10,10,10,NULL);
    
    // Crea Newton Body
    NewtonBody* myBody= NewtonCreateBody(nWorld, myShape);
    
    // Set Propiedades de Body
    NewtonBodySetMassMatrix(myBody, 1.0f, 1.0f, 1.0f, 1.0f);
    
    NewtonBodySetForceAndTorqueCallback(myBody,setForceAndTorque);
    
    // Integracion con Irrlicht Objeto
    NewtonBodySetUserData(myBody, (void*)carro);
    
    // Transformacion Callback
    NewtonBodySetTransformCallback(myBody,setTransform);
    

//////////////// OTRO OBJETO  ///////////////////
    // Define Newton Collision
    NewtonCollision* myShape2= NewtonCreateBox(nWorld, 10,10,10,NULL);
    
    // Crea Newton Body
    NewtonBody* myBody2= NewtonCreateBody(nWorld, myShape);
    
    // Set Propiedades de Body
    NewtonBodySetMassMatrix(myBody2, 1.0f, 1.0f, 1.0f, 1.0f);
    
    NewtonBodySetForceAndTorqueCallback(myBody2,setForceAndTorque);
    
    // Integracion con Irrlicht Objeto
    NewtonBodySetUserData(myBody2, (void*)mapa);
    
    // Transformacion Callback
    NewtonBodySetTransformCallback(myBody2,setTransform);

////////////////////////////////////////////////////////////   
    // Start the game loop, in here is where all code will be.
    while(device->run()){

        driver->beginScene(true, true, SColor(0,200,200,200));
        
        moviendo(carro);
        
        // Update the Newton world every so often. If you disable this, nothing will happen with the physics, this is great for pausing games.
        NewtonUpdate(nWorld, 0.01);

        smgr->drawAll();
        guienv->drawAll();
        
        driver->endScene();
    }

    NewtonDestroy(nWorld);
    device->drop();
    return 0;
}

thanks for the aid.

Note: the translation to ingles I did it by means of Babel Fish ;)
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

There could be two errors you made...

1. mapa is your floor, right ??? The floor should have no mass, because if a body has a mass, than the body is dynamic and falls down...

2. mapa is a mesh, are you sure the Newton body you created for it is in the right place and has the right size ??? (same for cara)
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Paio

Post by Paio »

Ok, i change this
// Set Propiedades de Body
NewtonBodySetMassMatrix(myBody2, 1.0f, 1.0f, 1.0f, 1.0f);

for this
// Set Propiedades de Body
NewtonBodySetMassMatrix(myBody2, 0.0f, 1.0f, 1.0f, 1.0f);

and put down the "mapa" 500 in Y position
but the other objet (carro) fall and no collided with "mapa"
Mapa is right in scale and position.
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

No, don't set any MassMatrix for the floor !!!
Delete the complete NewtonBodySetMassMatrix line !!!
Even a zero mass is a mass !!!

I suggest to have a look at the Newton demo 2 (using collision callbacks)...
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Guest

Post by Guest »

I'm here again.
I look Demo 2 of Newton and now my code have callback function, but "carro" no have good movement. He stops without collision.

Code: Select all

#include <irrlicht.h>
#include <newton.h>
#include <iostream>
#include <dvector.h>

using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

// Define all the variables we will be needing

NewtonWorld *nWorld;


NewtonBody *WorldBody;
NewtonBody *BoxBody;
IAnimatedMeshSceneNode *nTestBox;
IAnimatedMeshSceneNode *nWorldObject;

IAnimatedMeshSceneNode *carro;
IAnimatedMeshSceneNode *mapa;

const float NewtonToIrr= 32.0f;

IrrlichtDevice* device;
IVideoDriver* driver;
ISceneManager* smgr;
IGUIEnvironment* guienv;

int go=0;

class MyEventReceiver : public IEventReceiver 
{ 
public: 
        virtual bool OnEvent(SEvent event) 
        { 
          if (event.EventType == EET_KEY_INPUT_EVENT) 
          { 
            if (event.KeyInput.Key == KEY_ESCAPE && !event.KeyInput.PressedDown ) 
            { 
            device->closeDevice(); 
            return true; 
            } 

          if (event.KeyInput.Key == KEY_KEY_W)
          { 
            go=1;
            return true; 
          } 
          if (event.KeyInput.Key == KEY_KEY_S) 
          { 
            go=2;
            return true; 
          } 
          if (event.KeyInput.Key == KEY_KEY_A) 
          { 
            go=3;
            return true; 
          } 
          if (event.KeyInput.Key == KEY_KEY_D) 
          { 
            go=4;
            return true; 
          } 
               
         } 
        return false; 
        } 
}; 

void moviendo(NewtonBody* cube)
{
 vector3df vel(0,0,0); 
   
         if (go == 1 ) 
          { 
            vel = vector3df(0,0,20);
            NewtonBodySetVelocity( cube,  &vel.X);
          } 
          if (go == 2) 
          { 
            vel = vector3df(0,0,-20);
            NewtonBodySetVelocity( cube,  &vel.X);
          } 
          if (go == 3) 
          { 
            vel = vector3df(-20,0,0);
            NewtonBodySetVelocity( cube,  &vel.X);
          } 
          if (go == 4) 
          { 
            vel = vector3df(20,10,0);
            NewtonBodySetVelocity( cube,  &vel.X);
          }
}

// If you want to apply force and torque to your model, here is the place to do it.
void _cdecl setForceAndTorque(const NewtonBody* nBody){
	// Register some variables used.
	float fMass;
	float fInertiaX;
	float fInertiaY;
	float fInertiaZ;
	dVector dvForce;

	// Get the mass matrix and put the resulting variables in proper floating point variables.
	NewtonBodyGetMassMatrix(nBody, &fMass, &fInertiaX, &fInertiaY, &fInertiaZ);


	// Add gravity always.
	dvForce = dVector(0.0f, -fMass * 9.8f, 0.0f);
	// Set the new force (only gravity in this case).
	NewtonBodySetForce(nBody, &dvForce.m_x);

}




// Update the actual position of the body and node.
void _cdecl setTransform (const NewtonBody* nBody, const float* matrix){
	// Like always, register some variables used.
	ISceneNode *iNode;
    matrix4 mTranslation;
    
    // Get the new position and rotation of the body, so we can apply it to the Irrlicht node.
    memcpy(mTranslation.M, matrix, sizeof(float)*16);
    
    // Get the position of the Newton body and set it's values to our scene node.
	iNode = (ISceneNode *) NewtonBodyGetUserData (nBody);
	iNode->setPosition(mTranslation.getTranslation());
    iNode->setRotation(mTranslation.getRotationDegrees());
}





NewtonBody *addActiveBodyBox(ISceneNode *iNode, f32 fx, f32 fy, f32 fz, f32 fMass, f32 fInertiaX, f32 fInertiaY, f32 fInertiaZ, f32 fPosX, f32 fPosY, f32 fPosZ){
	// Create a collision box for the new active body.
    NewtonCollision *collision = NewtonCreateBox(nWorld, fx, fy, fz, NULL);
    // Create the body using the collision box.
    NewtonBody *nBody = NewtonCreateBody(nWorld, collision);
 
	
	// Set the body inertia model.
    NewtonBodySetMassMatrix(nBody, fMass, fInertiaX, fInertiaY, fInertiaZ);
   
    // Set the node to the newly created body.
    NewtonBodySetUserData(nBody, iNode);
    
    NewtonBodySetAutoFreeze (nBody, 0);	//set autofreeze off
	NewtonWorldUnfreezeBody (nWorld, nBody);// keep the object always active 
    
	// Set the body's Transform Callback.
	NewtonBodySetTransformCallback (nBody, setTransform);
	// Set the body's Force and Torque callback.
	NewtonBodySetForceAndTorqueCallback (nBody, setForceAndTorque);

	// Register a matrix and set the body's position within the matrix.
    matrix4 mTranslation;
    mTranslation.setTranslation(vector3df(fPosX, fPosY, fPosZ));
    NewtonBodySetMatrix(nBody, &mTranslation.M[0]);

   // We don't need the collision box anymore, release it from memory.
    NewtonReleaseCollision(nWorld, collision);
	
	// Return the resulting body.
    return nBody;
}




NewtonBody *addRigidBodyTree(ISceneNode *iNode, char *cFilename)
{
	// Load the specified mesh.
    IAnimatedMesh *mTreemesh = smgr->getMesh(cFilename);
	
	// Set up everything.
    NewtonCollision *ncTreeCollision;
	ncTreeCollision = NewtonCreateTreeCollision(nWorld, NULL);
	NewtonTreeCollisionBeginBuild(ncTreeCollision);

	int cMeshBuffer, j, i;
	int v1i, v2i, v3i;
	IMeshBuffer *mb;

	vector3df vArray[3];

	int tmpCount = 0;
	
	// Start the actual loop.
	IMesh *mesh = mTreemesh->getMesh(0);
	int buffCount = mesh->getMeshBufferCount();
	for (i = 0; i < buffCount; i ++) {
		for (cMeshBuffer=0; cMeshBuffer<mTreemesh->getMesh(i)->getMeshBufferCount(); cMeshBuffer++) {	
			mb = mTreemesh->getMesh(0)->getMeshBuffer(cMeshBuffer);

			video::S3DVertex* mb_vertices = (irr::video::S3DVertex*)mb->getVertices();

			u16* mb_indices  = mb->getIndices();

			// Add each triangle from the mesh.
			for (j=0; j<mb->getIndexCount(); j+=3)
			{
				v1i = mb_indices[j];
				v2i = mb_indices[j+1];
				v3i = mb_indices[j+2];

				// To make things easier, here we can use engine data type.
				vArray[0] = (mb_vertices[v1i].Pos + vector3df(0.0f, 0.0f, 0.0f));
				vArray[1] = (mb_vertices[v2i].Pos + vector3df(0.0f, 0.0f, 0.0f));
				vArray[2] = (mb_vertices[v3i].Pos + vector3df(0.0f, 0.0f, 0.0f));

				// Make sure we do not add degenerated polygons to the tree.
				vector3df e0 (vArray[1] - vArray[0]);
				vector3df e1 (vArray[2] - vArray[0]);

				// The area of the face is the cross product.
				vector3df area (e0.crossProduct (e1));

				// Skip faces with very small area.
				float mag = area.dotProduct (area);
				if (mag > 1.0e-6f) {
					NewtonTreeCollisionAddFace(ncTreeCollision, 3, &vArray[0].X, sizeof (vector3df), 1);
				} 
			}
		}
	}
 
	NewtonTreeCollisionEndBuild(ncTreeCollision, 1);

	// Create a rigid body.
	NewtonBody* bTreebody;
	bTreebody = NewtonCreateBody(nWorld, ncTreeCollision);

	// Release the collsion tree.
	NewtonReleaseCollision (nWorld, ncTreeCollision);
	
    return bTreebody;
}







int main(){

    MyEventReceiver receiver;
    
    // OpenGL device.
	device = createDevice(EDT_OPENGL, dimension2d<s32>(640, 480), 32, false, false, false, &receiver);    device->setWindowCaption(L"IrrNewton Template");
    // Video driver device.
    driver = device->getVideoDriver();
    smgr = device->getSceneManager();
    guienv = device->getGUIEnvironment();
    
    //Add a Newton World.
    nWorld = NewtonCreate(NULL, NULL);
    
    
    // Add an FPS camera to the scene.
    ICameraSceneNode *nCamera = smgr->addCameraSceneNodeFPS();
    // Add a light so it'll be easier to distinguish the angles of the test map.
    smgr->addLightSceneNode(0, vector3df(0.0f, 100.0f, 100.0f));

// add skybox
	smgr->addSkyBoxSceneNode(driver->getTexture("irrlicht2_up.bmp"),driver->getTexture("irrlicht2_dn.bmp"),driver->getTexture("irrlicht2_lf.bmp"),driver->getTexture("irrlicht2_rt.bmp"),driver->getTexture("irrlicht2_ft.bmp"),driver->getTexture("irrlicht2_bk.bmp"));



    // Load the world into Irrlicht.
    mapa = smgr->addAnimatedMeshSceneNode(smgr->getMesh("mapa2.x"));
    mapa->setMaterialFlag(EMF_LIGHTING,false);
    mapa->setPosition(vector3df(0,0.5,0));


    // Load the box into Irrlicht.
    carro = smgr->addAnimatedMeshSceneNode(smgr->getMesh("car.x"));
    carro->setMaterialFlag(EMF_LIGHTING,false);

//    BoxBody= addActiveBodyBox(carro, 3.28f * 2, 3.03f * 2, 3.27f * 2, 100.0f, 10.0f, 10.0f, 10.0f, 10.0f, 10.0f, 10.0f);
    BoxBody= addActiveBodyBox(carro, 1.5f , 1.5f , 1.5f , 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f);

// Make the world node a rigid body. (Rigid objects are fixed, they cannot move dynamically)
    WorldBody = addRigidBodyTree(mapa, "mapa2.x");


// Start the game loop, in here is where all code will be.
    while(device->run()){

        driver->beginScene(true, true, SColor(0,200,200,200));
        
        //moviendo(carro);
        moviendo(BoxBody);
        
        // Update the Newton world every so often. If you disable this, nothing will happen with the physics, this is great for pausing games.
        NewtonUpdate(nWorld, 0.01);

        smgr->drawAll();
        guienv->drawAll();
        
        driver->endScene();
    }

    NewtonDestroy(nWorld);
    device->drop();
    return 0;
}
:?
any help pls
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

Make the Newton world larger !!!

For more Newton related questions have a look for a Newton forum, please !!!
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Post Reply