Can't use Newton

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
TeTine
Posts: 17
Joined: Tue Nov 13, 2007 10:36 pm
Location: Nantes, France
Contact:

Can't use Newton

Post by TeTine »

Hi,
I'm trying to use newton with irrlicht using this tuto : http://irrlicht.sourceforge.net/phpBB2/ ... hp?t=25573
I modified it a bit to have something easier to understand but it can't run and it makes this annoying windows "send report" message and crash.

The bug seems to occur when I try to call ISceneNode::setPosition() or ISceneNode::setRotation().

And When I try to comment NewtonBodySetTransformCallback(), the program runs and the nodes are created but nothing happens regarding NewtonBodySetForceAndTorqueCallback.

I'm using visual express 2005, Irrlicht 1.4 and Newton 1.53.

If someone has an idea...
Irrlicht noob =p
B@z
Posts: 876
Joined: Thu Jan 31, 2008 5:05 pm
Location: Hungary

Post by B@z »

can't you send the code of the surroundings of the crashing code?

so before and after of the setPosition?
TeTine
Posts: 17
Joined: Tue Nov 13, 2007 10:36 pm
Location: Nantes, France
Contact:

Post by TeTine »

Code: Select all

#pragma comment(lib, "Irrlicht.lib")
#pragma comment(lib, "Newton.lib")

#define NEWTON_GRAVITY -800.0f

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

// Irrlicht:
#include <irrlicht.h>
#include <wchar.h>

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

// Newton:
#include <Newton.h>

// Frame drawing variables:
int lastFPS;
u32 lasttick;

// Irrlicht vars:
IVideoDriver* driver;
ISceneManager* smgr;
IGUIEnvironment* guienv;
IrrlichtDevice *device;

// Newton vars:
NewtonWorld *nWorld;
NewtonBody *body;
NewtonCollision *collision;

// Scene vars:
ICameraSceneNode *cam;


void SetMeshTransformEvent(const NewtonBody* body, const float* matrix)
{
   // Instantiate Irricht CMatrix4 template as float, copy the matrix into it:
   // ggb: Old tutorial used matrix4
   CMatrix4<float> mat;
   mat.setM(matrix);

   // Retreive the user data attached to the Newton body:
   ISceneNode *tmp = (ISceneNode *)NewtonBodyGetUserData(body);
   if (tmp)
   {
	  //vector3df posTmp = tmp->getPosition();
	  //printf("position : %f %f %f\n", posTmp.X, posTmp.Y, posTmp.Z);
      // Position the node:
      tmp->setPosition(mat.getTranslation());      // set position
      tmp->setRotation(mat.getRotationDegrees());   // and rotation
   }
}

void ApplyForceAndTorqueEvent (const NewtonBody* body)
{
   float mass;
   float Ixx;
   float Iyy;
   float Izz;
   float force[3];
   float torque[3];

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

   force[0] = 0.0f;
   force[1] = NEWTON_GRAVITY * mass;
   force[2] = 0.0f;

   torque[0] = 0.0f;
   torque[1] = 500.0f;
   torque[2] = 0.0f;

   NewtonBodyAddForce (body, force);
   NewtonBodyAddTorque (body, torque);
}

int main(int argc, char* argv[])
{

   // Init the Irrlicht engine:
   device = createDevice(EDT_OPENGL, dimension2d<s32>(800, 600), 16, false, true, false);

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

   // Init Newton:
   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 the camera:
   cam = smgr->addCameraSceneNodeMaya();
   cam->setPosition(vector3df(0, 50, 0));

   // Initialize variables:
   lasttick = 0;
	
   //testInits
   
	IAnimatedMesh* testSphere = smgr->addSphereMesh("testSphere");
	smgr->addAnimatedMeshSceneNode(testSphere);

	ILightSceneNode * light = smgr->addLightSceneNode();
	light->setPosition(vector3df(0,0,100));

	
	collision = NewtonCreateBox(nWorld, 38, 38, 38, NULL);
	body = NewtonCreateBody(nWorld, collision);
	NewtonBodySetUserData(body, testSphere);
	NewtonBodySetMassMatrix (body, 10.0f, 150.0f, 150.0f, 150.0f);
	NewtonBodySetFreezeTreshold(body, 1.0, 1.0, 1);

	float omega[3]; omega[1] = 1.0f; omega[2] = 0.0f; omega[3] = 0.0f;
	NewtonBodySetOmega (body, &omega[0]);
	
	NewtonBodySetTransformCallback(body, SetMeshTransformEvent);
   NewtonBodySetForceAndTorqueCallback(body, ApplyForceAndTorqueEvent);

   // Main Loop:
   while(device->run())
   {
      driver->beginScene(true, true, video::SColor(0,220,220,255));

      // Render the scene:
      smgr->drawAll();

      driver->endScene();

      // Draw fps counter:
      int fps = driver->getFPS();
      if (lastFPS != fps)
      {
        wchar_t tmp[1024];
        swprintf(tmp, 1024, L"Newton Example [fps:%d] [triangles:%d]",
            fps, driver->getPrimitiveCountDrawn());
        device->setWindowCaption(tmp);
        lastFPS = fps;
      }

      // Update newton 100 times / second:
      if (device->getTimer()->getTime() > lasttick + 10) {
        lasttick = device->getTimer()->getTime();
        NewtonUpdate(nWorld, 0.01f);
      }
   }

   // Clean up memory:
	NewtonReleaseCollision(nWorld, collision);


   // Finish Newton & Irrlicht:
   NewtonDestroy(nWorld);
   device->drop();

   return 0;
} 
Here it is. I decided to show all the code since it is not this huge.

edit: just so that you know, i tried to run the code of the tutorial without changing a thing but it crashed the same way.
Irrlicht noob =p
B@z
Posts: 876
Joined: Thu Jan 31, 2008 5:05 pm
Location: Hungary

Post by B@z »

this works for me:

Code: Select all

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

	// Retreive the user data attached to the newton body
	ISceneNode *tmp = (ISceneNode *)NewtonBodyGetUserData(body);
	if (tmp)
	{
		// Position the node
		tmp->setPosition(mat.getTranslation());		// set position
		tmp->setRotation(mat.getRotationDegrees());	// and rotation
	}
}
maybe your "tmp" doesn't get the data?
set a break point before initialising tmp, and check, that it's surely gets the data from the body.
B@z
Posts: 876
Joined: Thu Jan 31, 2008 5:05 pm
Location: Hungary

Post by B@z »

ohh, and are you sure that the model is loaded correctly? :D
there aren't any error messages in the console window?
TeTine
Posts: 17
Joined: Tue Nov 13, 2007 10:36 pm
Location: Nantes, France
Contact:

Post by TeTine »

Well, I used one of Irrlicht primitives and when i disable Newton i can see the sphere in the scene. I get no error message in the console window regarding the loading.

Regarding the code, I read that Irrlicht now uses CMatrix4 class instead of matrix4 (the first attempt i made didn't work because of that it seems).
but I'll try this code again, even if i remember having some problems with memcpy :/
I'll post this evening to show what happens to the tmp var.
Thx for the answer =)
Irrlicht noob =p
TeTine
Posts: 17
Joined: Tue Nov 13, 2007 10:36 pm
Location: Nantes, France
Contact:

Post by TeTine »

Ok, here's what i get :
"Exception non gérée à 0x3f800000 dans newtIrr3.exe : 0xC0000005: Violation d'accès lors de la lecture de l'emplacement 0x3f800000."
Sorry it's french but it says that there's a non handled exception which consists in an adress that can't be read.

I'm totally lost =p
Irrlicht noob =p
B@z
Posts: 876
Joined: Thu Jan 31, 2008 5:05 pm
Location: Hungary

Post by B@z »

hmm.. i don't get it ><;
are you sure you're using the correct dll?
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

It's nothing to do with a dll, it's merely because there's an unhandled exception, as the message suggests!

That's most likely caused by a null pointer, debug the code to find which line it's crashing on.
Image Image Image
Post Reply