Newton mercior tutorial code: boxes frozen again

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!
levante
Posts: 13
Joined: Wed Oct 11, 2006 1:03 pm
Contact:

Newton mercior tutorial code: boxes frozen again

Post by levante »

Hi all

I am trying to integrate Newton in my Irrlicht project so I was trying to reproduce an example very similar to mercior's tutorial. I perfectly know that mercior tutorial is old and so I tried to modify my example code with code snippets found here on this forum but I still can't get my newton code to work. I need help! This is my code, very simple test application like in mercior tutorial. Loads a bsp level and throws boxes with left mouse button. The problem is that when I thow boxes they don't move! Boxes are frozen in middle-air or even through walls:

Code: Select all

#include <irrlicht.h>
#include <Newton.h>

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

static NewtonWorld* nWorld;
IrrlichtDevice *device = NULL;
IVideoDriver* driver = NULL;
ISceneManager* smgr = NULL;
float NewtonToIrr = 32.0f;
float IrrToNewton = 1/NewtonToIrr;
IGUIEnvironment *guienv = NULL;
ICameraSceneNode *cam = NULL;

struct NewtonCube {
	IAnimatedMesh *mesh;
	ISceneNode *node;
	NewtonBody *body;
	const NewtonBody *waterBody;
};

NewtonCube *cubes[512];
int currentCube = 0;

// allocation of memory from Newton
void* _cdecl NewtonAlloc (int sizeInBytes) {
   return new char[sizeInBytes];
}

// destruction of memory from Newton
void _cdecl NewtonFree (void *ptr, int sizeInBytes) {
   char *tmp;
   tmp = (char*) ptr;
   delete[] tmp;
}

/*
Function:   AddGlobalForce (vector3df Force, vector3df Point, matrix4 BodyMatrix,const NewtonBody* body)

Returns:   Nothing
Parameters:   vector3df Force -- the force you want to apply
         vector3df Point -- global space location to apply the force
         matrix4 BodyMatrix -- Newton Matrix describing object
         NewtonBody* body   -- the body in question

Purpose:   Add a force to an object displaced from the center of mass

Note:      Because of the displacement, you will get a spin or torque, and
         you have to tell Newton what that spin is. Thats what this function
         does.

*/


void AddGlobalForce (vector3df Force, vector3df Point, matrix4 BodyMatrix,const NewtonBody* body)
{
   vector3df R = Point - BodyMatrix.getTranslation();
    vector3df Torque = R.crossProduct(Force);
    NewtonBodyAddForce (body,&Force.X);
    NewtonBodyAddTorque (body,&Torque.X);
}

/*
Function:   AddLocalForce (vector3df Force, vector3df LocalPoint, matrix4 BodyMatrix,const NewtonBody* body)

Returns:   Nothing
Parameters:   vector3df Force -- the force you want to apply, in local space
         vector3df Point -- local space location to apply the force
         matrix4 BodyMatrix -- Newton Matrix describing object
         NewtonBody* body   -- the body in question

Purpose:   Takes a local force and displacement and translates it into a global
         force request

Note:      Because of the displacement, you will get a spin or torque, and
         you have to tell Newton what that spin is. Thats what this function
         does.Called only from within ApplyTorqueandForceHandler

*/

void AddLocalForce (vector3df LocalForce, vector3df LocalPoint, matrix4 BodyMatrix,const NewtonBody* body)
{
     vector3df GlobalForce,GlobalPoint;
     BodyMatrix.transformVect(LocalForce,GlobalForce);
    BodyMatrix.transformVect(LocalPoint,GlobalPoint);
   if (LocalForce.X == 0.0f && LocalForce.Y == 0.0f && LocalForce.Z == 0.0f)
   {

   }
   else
   {
      AddGlobalForce (GlobalForce, GlobalPoint,BodyMatrix,body);

   }
}


void _cdecl NewtonDebugCollision(const NewtonBody* body, int vertexCount, const float* FaceArray, int faceId)
{
   core::vector3df p0 (FaceArray[0], FaceArray[1], FaceArray[2] );

   const video::SColor c0(255,55,255,0);

   for (int i = 2; i < vertexCount; i ++)
   {
      core::vector3df p1( FaceArray[(i-1) * 3 + 0], FaceArray[(i-1) * 3 + 1], FaceArray[(i-1) * 3 + 2] );
      core::vector3df p2( FaceArray[i * 3 + 0], FaceArray[i * 3 + 1], FaceArray[i * 3 + 2] );

      core::triangle3df t;
      t.set(p1*NewtonToIrr,
           p2*NewtonToIrr,
           p0*NewtonToIrr);

       driver->draw3DTriangle(t, c0);
   }
}



void _cdecl NewtonDebugBody (const NewtonBody* body) {
   matrix4 mat;
   SMaterial material;

   material.Texture1 = 0;
   material.Lighting = false;
    driver->setTransform(video::ETS_WORLD, mat);
    driver->setMaterial(material);
   NewtonBodyForEachPolygonDo(body,NewtonDebugCollision);
}


static void setTransform(const NewtonBody* body, const float* matrix) {
    //get graphic object
    ISceneNode* node = (ISceneNode*) NewtonBodyGetUserData(body);

    //copy matrix to an irrlicht matrix
    matrix4 mat;
    memcpy(mat.M, matrix, sizeof(float)*16);

    //set transformation
    node->setPosition(mat.getTranslation() * NewtonToIrr);
   node->setRotation(mat.getRotationDegrees());
}

static void setForceAndTorque(const NewtonBody* body) {
    float mass;
    float Ixx;
    float Iyy;
    float Izz;
   matrix4 mat;
   mat.makeIdentity();
   NewtonBodyGetMatrix(body,mat.M);

    NewtonBodyGetMassMatrix (body, &mass, &Ixx, &Iyy, &Izz);

    vector3df force(0, -mass* 9.8f, 0.0);
    vector3df torque(0, 00.0f, 0);
   vector3df mforce(0,0,5);
   vector3df displace(2,0,0);
    NewtonBodyAddForce(body, &force.X);
    AddLocalForce(mforce,displace,mat,body);
}


class MyEventReceiver : public IEventReceiver {
public:
   virtual bool OnEvent(SEvent event) {
	if (event.EventType == irr::EET_MOUSE_INPUT_EVENT && event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
	/* make a cube where the camera is and set its velocity to follow the target */

	vector3df camvec = (cam->getTarget() - cam->getPosition()).normalize() * 500;

            NewtonCube *tmp = new NewtonCube;
            tmp->mesh = smgr->getMesh("media/smallcube.3ds");
            tmp->node = smgr->addAnimatedMeshSceneNode(tmp->mesh);
            tmp->node->setMaterialTexture(0, driver->getTexture("media/crate.jpg"));
            tmp->node->setMaterialFlag(EMF_LIGHTING, false);
            tmp->waterBody = NULL;

            /* Create a box primitive. 38 is just an estimated value of the size of the model */
            float x = 40 * IrrToNewton;
            float y = 40 * IrrToNewton;
            float z = 40 * IrrToNewton;
            NewtonCollision* collision;
            collision = NewtonCreateBox(nWorld, x, y, z, NULL);

            // creatye the body
            NewtonBody* body;
            body = NewtonCreateBody(nWorld, collision);
            tmp->body = body;


            // the box is metal
            //NewtonBodySetMaterialGroupID (body, m_metalMaterial);

            /* release the collsion tree (this way the application does not have to do book keeping of Newton objects */
            NewtonReleaseCollision (nWorld, collision);

            // Set user data pointer to the scene node
            NewtonBodySetUserData(body, tmp);

            // Set body mass & inertia matrix
            float mass = 10.0f;
            float Ixx = 0.7f * mass * (y * y + z * z) / 12.0f;
            float Iyy = 0.7f * mass * (x * x + z * z) / 12.0f;
            float Izz = 0.7f * mass * (x * x + y * y) / 12.0f;
            NewtonBodySetMassMatrix (body, mass, Ixx, Iyy, Izz);

            // leave the sleep tolerance alone
            NewtonBodySetFreezeTreshold(body, 1.0, 1.0, 1);

            // Set callback functions for the body
            NewtonBodySetTransformCallback(body, setTransform);
            NewtonBodySetForceAndTorqueCallback(body, setForceAndTorque);

            // Set the position of the body
            matrix4 mat;
            mat.setTranslation(cam->getPosition() * IrrToNewton);
            NewtonBodySetMatrix(body, &mat.M[0]);
            tmp->node->setPosition(cam->getPosition());
            setTransform(body, &mat.M[0]);

            vector3df nVeloc (camvec * IrrToNewton);
            NewtonBodySetVelocity(body, &nVeloc.X);

            cubes[currentCube] = tmp;
            currentCube ++;

        }

      return false;
      }
};

MyEventReceiver receiver;

int main() {

      device = createDevice(video::EDT_OPENGL, core::dimension2d<s32>(800, 600), 16, false, false, false, &receiver);

      device->setWindowCaption(L"Newton and Irrlicht Engine Demo");

	driver = device->getVideoDriver();
	smgr = device->getSceneManager();
	guienv = device->getGUIEnvironment();

	/** NEWTON INITIALIZATION **/
	nWorld =     NewtonCreate(NewtonAlloc, NewtonFree);

   // 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);

     /** BSP LOADING IN NEWTON **/

//Level
   device->getFileSystem()->addZipFileArchive("media/map-20kdm2.pk3");
   scene::IAnimatedMesh* q3levelmesh = smgr->getMesh("20kdm2.bsp");
   scene::ISceneNode* q3node = 0;

     if (q3levelmesh)
      q3node = smgr->addOctTreeSceneNode(q3levelmesh->getMesh(0));
   q3node->setPosition(core::vector3df(-1370,-130,-1400));

     //LevelNewtonCollision
     NewtonCollision* nmapcollision = NewtonCreateTreeCollision(nWorld, NULL);
     NewtonTreeCollisionBeginBuild(nmapcollision);
     int cMeshBuffer, j;
     int v1i, v2i, v3i;
     IMeshBuffer *mb;
     float vArray[9]; // vertex array (3*3 floats)
     int tmpCount = 0;


   core::aabbox3d<f32> box;
   //size the box in Newton units
    box = q3levelmesh->getMesh(0)->getBoundingBox();
   vector3df size = box.getExtent()*IrrToNewton;

     for (cMeshBuffer=0; cMeshBuffer<q3levelmesh->getMesh(0)->getMeshBufferCount(); cMeshBuffer++)
   {
      mb = q3levelmesh->getMesh(0)->getMeshBuffer(cMeshBuffer);
      S3DVertex2TCoords* mb_vertices = (S3DVertex2TCoords*)mb->getVertices();
      u16* mb_indices  = mb->getIndices();
      // add each triangle from the mesh
      for (j=0; j<mb->getIndexCount(); j+=3)
      {

   //   printf("buff %d count %d\n",cMeshBuffer,j);

       v1i = mb_indices[j];
         v2i = mb_indices[j+1];
         v3i = mb_indices[j+2];
       vArray[0] = mb_vertices[v1i].Pos.X* IrrToNewton;
         vArray[1] = mb_vertices[v1i].Pos.Y* IrrToNewton;
         vArray[2] = mb_vertices[v1i].Pos.Z* IrrToNewton;
         vArray[3] = mb_vertices[v2i].Pos.X* IrrToNewton;
         vArray[4] = mb_vertices[v2i].Pos.Y* IrrToNewton;
         vArray[5] = mb_vertices[v2i].Pos.Z* IrrToNewton;
         vArray[6] = mb_vertices[v3i].Pos.X* IrrToNewton;
         vArray[7] = mb_vertices[v3i].Pos.Y* IrrToNewton;
         vArray[8] = mb_vertices[v3i].Pos.Z* IrrToNewton;

          NewtonTreeCollisionAddFace(nmapcollision, 3, &vArray[0], 12, 1);
     }
   }

      NewtonTreeCollisionEndBuild(nmapcollision, 0);
      NewtonBody* nmapbody = NewtonCreateBody(nWorld, nmapcollision);

      // set the newton world size based on the bsp size
      float boxP0[3];
      float boxP1[3];
      float matrix[4][4];
     matrix4 mmm;
     mmm.setTranslation(vector3df(-1370,-130,-1400)*IrrToNewton);

     NewtonBodySetMatrix(nmapbody,&mmm.M[0]);
      NewtonCollisionCalculateAABB (nmapcollision, &matrix[0][0],  &boxP0[0], &boxP1[0]);
      NewtonSetWorldSize (nWorld, (float*)boxP0, (float*)boxP1);


    device->getCursorControl()->setVisible(false);
    cam = smgr->addCameraSceneNodeFPS();
    cam->setPosition(vector3df(0.0, 100.0, 150.0));

    while(device->run()) {
             NewtonUpdate(nWorld, 0.01f);
             driver->beginScene(true, true, SColor(0,200,200,200));
	smgr->drawAll();
	guienv->drawAll();
	driver->endScene();
    }

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

I AM linking both Newton.lib and Irrlicht.lib and using correct dlls of course.
Please help me :cry: .
[/code]
Marco Del Percio
sio2
Competition winner
Posts: 1003
Joined: Thu Sep 21, 2006 5:33 pm
Location: UK

Post by sio2 »

I got this to compile but when I ran it there were media files missing.

I substituted the missing 3ds model for sphere.3ds and the missing crate texture for rockwall.bmp. This time I got a GPF. Looking into it...
levante
Posts: 13
Joined: Wed Oct 11, 2006 1:03 pm
Contact:

Post by levante »

Thanks for help sio2...

What the hell is a GPF!!?? :D
Marco Del Percio
sio2
Competition winner
Posts: 1003
Joined: Thu Sep 21, 2006 5:33 pm
Location: UK

Post by sio2 »

Eeek!

In the function:

Code: Select all

static void setTransform(const NewtonBody* body, const float* matrix)
... the line...

Code: Select all

ISceneNode* node = (ISceneNode*) NewtonBodyGetUserData(body);
...gives a GPF. It should be ...

Code: Select all

NewtonCube *nc = (NewtonCube *)NewtonBodyGetUserData(body);
ISceneNode* node = nc->node;
Now I at least have a sphere floating in mid air. :wink:

Looking at getting it to move and collide...
sio2
Competition winner
Posts: 1003
Joined: Thu Sep 21, 2006 5:33 pm
Location: UK

Post by sio2 »

levante wrote:Thanks for help sio2...

What the hell is a GPF!!?? :D
GPF == General Protection Fault. Otherwise known as a "crash". :wink:

..and sio2 (SiO2) is silicon dioxide. 8)
levante
Posts: 13
Joined: Wed Oct 11, 2006 1:03 pm
Contact:

Post by levante »

Hehehe Thaks thanks ok you were right it was my fault because of mixing copy & paste. Sorry, terribly sorry!! Now this is the correct code without GPF, I also changed the setTransform and force&Torque functions with prototypes by mercior tutorial, yet I get the frozen boxes result :-( nothing is moving with gravity :-(

Code: Select all

#include <Irrlicht.h>
#include "newton.h"
#include <memory.h>
#include <iostream>
#define NEWTON_GRAVITY -800.0f


static NewtonWorld* nWorld;

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

IrrlichtDevice *device = NULL;
IVideoDriver* driver = NULL;
ISceneManager* smgr = NULL;
float NewtonToIrr = 32.0f;
float IrrToNewton = 1/NewtonToIrr;
IGUIStaticText* pText = NULL;
bool debugOn = true;
ICameraSceneNode *camera = NULL;

struct NewtonCube {
   IAnimatedMesh *mesh;
   ISceneNode *node;
   NewtonBody *body;
   const NewtonBody *waterBody;
};

// allocation of memory from Newton
void* _cdecl
NewtonAlloc (int sizeInBytes)
{
   return new char[sizeInBytes];
}

// destruction of memory from Newton
void _cdecl
NewtonFree (void *ptr, int sizeInBytes)
{
   char *tmp;
   tmp = (char*) ptr;
   delete[] tmp;
}

void setMeshTransformEvent(const NewtonBody* body, const float* matrix)
{
	// copy the matrix into an irrlicht matrix4
	matrix4 mat;
	memcpy(mat.M, matrix, sizeof(float)*16);

	// Retreive the user data attached to the newton body
	ISceneNode *tmp = ((NewtonCube *)NewtonBodyGetUserData(body))->node;
	if (tmp)
	{
		// Position the node
		tmp->setPosition(mat.getTranslation() * NewtonToIrr);		// set position

		// Irr and Newton use the same ordering for Euler angle. But newton is more stable handling the
		// gimbar lock (this is when (the secing angle is a integer multiple of 90.0 degree)
		// uset the Newton function instead

		//tmp->setRotation(mat.getRotationDegrees());	// and rotation
		tmp->setRotation(mat.getRotationDegrees());
	}
}

void applyForceAndTorqueEvent(const NewtonBody* body)
{
   float mass;
   float Ixx;
   float Iyy;
   float Izz;

   NewtonBodyGetMassMatrix (body, &mass, &Ixx, &Iyy, &Izz);

   vector3df force (0.0f * IrrToNewton, NEWTON_GRAVITY * mass * IrrToNewton, 0.0f * IrrToNewton);
   vector3df torque (0.0f * IrrToNewton, 0.0f * IrrToNewton, 0.0f * IrrToNewton);

   NewtonBodyAddForce (body, &force.X);
   NewtonBodyAddTorque (body, &torque.X);
}

class MyEventReceiver : public  IEventReceiver
{
public:

///////////////////////////

   virtual bool OnEvent(SEvent event) {
       if (event.EventType == irr::EET_KEY_INPUT_EVENT&& event.KeyInput.PressedDown) {

         switch(event.KeyInput.Key)
         {
            case KEY_ESCAPE:
            device->closeDevice();
            break;

            case KEY_KEY_D:
            debugOn = !debugOn;
            break;

         }



      }// pressdown


      if (event.EventType == irr::EET_MOUSE_INPUT_EVENT && event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
   /* make a cube where the camera is and set its velocity to follow the target */

            vector3df camvec = (camera->getTarget() - camera->getPosition()).normalize() * 500;

            NewtonCube *tmp = new NewtonCube;
            tmp->mesh = smgr->getMesh("media/smallcube.3ds");
            tmp->node = smgr->addAnimatedMeshSceneNode(tmp->mesh);
            tmp->node->setMaterialTexture(0, driver->getTexture("media/crate.jpg"));
            tmp->node->setMaterialFlag(EMF_LIGHTING, false);
            tmp->waterBody = NULL;


            /* Create a box primitive. 38 is just an estimated value of the size of the model */
            float x = 40 * IrrToNewton;
            float y = 40 * IrrToNewton;
            float z = 40 * IrrToNewton;
            NewtonCollision* collision;
            collision = NewtonCreateBox(nWorld, x, y, z, NULL);

            // creatye the body
            NewtonBody* body;
            body = NewtonCreateBody(nWorld, collision);
            tmp->body = body;


            // the box is metal
            //NewtonBodySetMaterialGroupID (body, m_metalMaterial);

            /* release the collsion tree (this way the application does not have to do book keeping of Newton objects */
            NewtonReleaseCollision (nWorld, collision);

            // Set user data pointer to the scene node
            NewtonBodySetUserData(body, tmp);

            // Set body mass & inertia matrix
            float mass = 10.0f;
            float Ixx = 0.7f * mass * (y * y + z * z) / 12.0f;
            float Iyy = 0.7f * mass * (x * x + z * z) / 12.0f;
            float Izz = 0.7f * mass * (x * x + y * y) / 12.0f;
            NewtonBodySetMassMatrix (body, mass, Ixx, Iyy, Izz);

            // leave the sleep tolerance alone

            NewtonBodySetFreezeTreshold(body, 1.0, 1.0, 1);

            // Set callback functions for the body
            NewtonBodySetTransformCallback(body, setMeshTransformEvent);

            NewtonBodySetForceAndTorqueCallback(body, applyForceAndTorqueEvent);

            // Set the position of the body
            matrix4 mat;
            mat.setTranslation(camera->getPosition() * IrrToNewton);
            NewtonBodySetMatrix(body, &mat.M[0]);

            tmp->node->setPosition(camera->getPosition());
            setMeshTransformEvent(body, &mat.M[0]);

            vector3df nVeloc (camvec * IrrToNewton);
            NewtonBodySetVelocity(body, &nVeloc.X);


      }
      return 0;
   }
};


MyEventReceiver receiver;



int main() {
device = createDevice(EDT_OPENGL, dimension2d<s32>(800,600), 32,false,false,false,&receiver);

driver = device->getVideoDriver();
smgr = device->getSceneManager();
IGUIEnvironment* env = device->getGUIEnvironment();

    pText = env->addStaticText(L"fred",core::rect<int>(20,20,440,200), false);
   pText->setOverrideColor(video::SColor(255,240,240,235));
   pText->setOverrideFont(
   env->getFont("midfont.bmp"));


   //1. Create Newtonworld
   nWorld =     NewtonCreate(NewtonAlloc, NewtonFree);

   // 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);





   camera = smgr->addCameraSceneNodeFPS(0, 200.0f, 250.0f);
   camera->setPosition(vector3df(0,10,-120));
   camera->setTarget(vector3df(0,0,0));

   //Level
   device->getFileSystem()->addZipFileArchive("media/map-20kdm2.pk3");
   scene::IAnimatedMesh* q3levelmesh = smgr->getMesh("20kdm2.bsp");
   scene::ISceneNode* q3node = 0;

     if (q3levelmesh)
      q3node = smgr->addOctTreeSceneNode(q3levelmesh->getMesh(0));
   q3node->setPosition(core::vector3df(-1370,-130,-1400));

     //LevelNewtonCollision
     NewtonCollision* nmapcollision = NewtonCreateTreeCollision(nWorld, NULL);
     NewtonTreeCollisionBeginBuild(nmapcollision);
     int cMeshBuffer, j;
     int v1i, v2i, v3i;
     IMeshBuffer *mb;
     float vArray[9]; // vertex array (3*3 floats)
     int tmpCount = 0;

   core::aabbox3d<f32> box;
   //size the box in Newton units
    box = q3levelmesh->getMesh(0)->getBoundingBox();
   vector3df size = box.getExtent()*IrrToNewton;


     for (cMeshBuffer=0; cMeshBuffer<q3levelmesh->getMesh(0)->getMeshBufferCount(); cMeshBuffer++)
   {
      mb = q3levelmesh->getMesh(0)->getMeshBuffer(cMeshBuffer);
      S3DVertex2TCoords* mb_vertices = (S3DVertex2TCoords*)mb->getVertices();
      u16* mb_indices  = mb->getIndices();
      // add each triangle from the mesh
      for (j=0; j<mb->getIndexCount(); j+=3)
      {

   //   printf("buff %d count %d\n",cMeshBuffer,j);

       v1i = mb_indices[j];
         v2i = mb_indices[j+1];
         v3i = mb_indices[j+2];
       vArray[0] = mb_vertices[v1i].Pos.X* IrrToNewton;
         vArray[1] = mb_vertices[v1i].Pos.Y* IrrToNewton;
         vArray[2] = mb_vertices[v1i].Pos.Z* IrrToNewton;
         vArray[3] = mb_vertices[v2i].Pos.X* IrrToNewton;
         vArray[4] = mb_vertices[v2i].Pos.Y* IrrToNewton;
         vArray[5] = mb_vertices[v2i].Pos.Z* IrrToNewton;
         vArray[6] = mb_vertices[v3i].Pos.X* IrrToNewton;
         vArray[7] = mb_vertices[v3i].Pos.Y* IrrToNewton;
         vArray[8] = mb_vertices[v3i].Pos.Z* IrrToNewton;

          NewtonTreeCollisionAddFace(nmapcollision, 3, &vArray[0], 12, 1);
     }
   }

      NewtonTreeCollisionEndBuild(nmapcollision, 0);
      NewtonBody* nmapbody = NewtonCreateBody(nWorld, nmapcollision);

      // set the newton world size based on the bsp size
      float boxP0[3];
      float boxP1[3];
      float matrix[4][4];
     matrix4 mmm;
     mmm.setTranslation(vector3df(-1370,-130,-1400)*IrrToNewton);

     NewtonBodySetMatrix(nmapbody,&mmm.M[0]);
      NewtonCollisionCalculateAABB (nmapcollision, &matrix[0][0],  &boxP0[0], &boxP1[0]);
      NewtonSetWorldSize (nWorld, (float*)boxP0, (float*)boxP1);

   while(device->run())
   {
      NewtonUpdate(nWorld, 0.01f);
      //camera->setTarget(node->getPosition());

      driver->beginScene(true, true, video::SColor(0,0,0,0));


      smgr->drawAll();
     env->drawAll();
      driver->endScene();
   }
   device->drop();
   // 4. Destroy World
   NewtonDestroy(nWorld);
   return 0;
}
Now I have the correct cast to NewtonCube though nothing is moving :-(
Please help!
Marco Del Percio
levante
Posts: 13
Joined: Wed Oct 11, 2006 1:03 pm
Contact:

Post by levante »

By the way, on some similar topic posts I have read that it's a problem of Newton calculating AABB and that I should put bigger dimensions in NewtonSetWorldSize. I even tried but got no results! :cry:
Any help is appreciated. Thanks in advance
Marco Del Percio
sio2
Competition winner
Posts: 1003
Joined: Thu Sep 21, 2006 5:33 pm
Location: UK

Post by sio2 »

I'd forgotten about Merciors demo on the Newton website. I've just had a play with it - very interesting!

Replacing the irrlicht dll from the demo with one freshly made gives a linking error, so I'm going to try the Mercior demo with the latest Irrlicht first and then see if I can get your demo going.

This is interesting for me as I too would like to get Newton working with the latest Irrlicht. :)
sio2
Competition winner
Posts: 1003
Joined: Thu Sep 21, 2006 5:33 pm
Location: UK

Post by sio2 »

It seems the Newton API has changed since the Mecior* demo, so it's not just a simple recompile. After tinkering (gotta learn more about Newton, anyway!) I got it working with just the boxes that "fall from the sky" and shooting boxes with left-mouse. Still working out ragdolls (getting a GPF).

One thing not in the compiled demo that comes with the zip is water, yet it's supported in the source. I enabled/compiled the water and now the boxes float in a nice pool of water (Irrlicht's addHillPlaneMesh()). 8)

*The download is "MerciorDemo.zip" yet the project name is "Mecior"!
levante
Posts: 13
Joined: Wed Oct 11, 2006 1:03 pm
Contact:

Post by levante »

Great job SiO2... so if you have understood how to get it working can u tell me what's wrong in my code then?
Marco Del Percio
sio2
Competition winner
Posts: 1003
Joined: Thu Sep 21, 2006 5:33 pm
Location: UK

Post by sio2 »

levante wrote:Great job SiO2... so if you have understood how to get it working can u tell me what's wrong in my code then?
Yep. Just looking at your demo now. 8)
sio2
Competition winner
Posts: 1003
Joined: Thu Sep 21, 2006 5:33 pm
Location: UK

Post by sio2 »

Oh yeah - I just noticed that the missing media referred to in your initial post is in the MerciorDemo from the Newton website. :mrgreen:
sio2
Competition winner
Posts: 1003
Joined: Thu Sep 21, 2006 5:33 pm
Location: UK

Post by sio2 »

levante wrote:Great job SiO2... so if you have understood how to get it working can u tell me what's wrong in my code then?
Strange. I put a breakpoint at:
void setMeshTransformEvent(const NewtonBody* body, const float* matrix)
and it got called only once (on creation of a crate). No wonder the box isn't moving! I'm now trying to figure out why...
sio2
Competition winner
Posts: 1003
Joined: Thu Sep 21, 2006 5:33 pm
Location: UK

Post by sio2 »

Solved it! 8) :mrgreen:

Newton was doing exactly what you told it to do. Since Newton thought the crate was outside the "physics world" it was being efficient and wasn't calling the update callbacks.

The line in your source that is causing the problem is this:

Code: Select all

NewtonSetWorldSize (nWorld, (float*)boxP0, (float*)boxP1);
Comment out this line and the crates suddenly move (albeit *very* fast! - needs tweaking).

Alternatively, you could supply the correct bounding box. :wink:
levante
Posts: 13
Joined: Wed Oct 11, 2006 1:03 pm
Contact:

Post by levante »

YOU ARE MY SWEET, WONDERFUL PERSONAL GOD SiO2!!!!! I adore you!!!!!!!!!! I have been working and thinking on this for weeks and yes I already knew that it was a problem of world size... If u remember I already wrote it in previous post but I didn't know how to change world size!!! I would have never thought of removing that line... It looks impossible to me that it works without defining world size!!
Anyway great JOB, excellent, applause!!!!
:D :D :D :D :D :D :D
Marco Del Percio
Post Reply