Problem with Newton and irrlicht[SOLVED]

Post your questions, suggestions and experiences regarding game design, integration of external libraries here. For irrEdit, irrXML and irrKlang, see the
ambiera forums
Post Reply
petra999
Posts: 10
Joined: Sat May 31, 2008 5:54 am

Problem with Newton and irrlicht[SOLVED]

Post by petra999 »

Hello!

I tried to do something with irrlicht and newton.
I do some easy program with this->http://gpwiki.org/index.php/Irrlicht:Physics tutorial.

But i have done something bad, and nothing happend(no gravity, etc), what i do wrong?
I do you know some nice tutorials to newton, and newton with irrlicht?

Thanks a lot for any reply.

petra999

And source code:

Code: Select all

#include <Irrlicht.h>
#pragma comment(lib, "Irrlicht.lib")
#include <newton.h>
#pragma comment(lib, "Newton.h")

using namespace irr;
using namespace video;
using namespace core;
using namespace scene;
unsigned int lasttick;



int main()
{
    NewtonWorld* nWorld = NewtonCreate(NULL, NULL);
    NewtonBody* nCube = 0;
    NewtonCollision* collision = NewtonCreateBox(nWorld, 0, 0, 0, NULL);
    nCube = NewtonCreateBody(nWorld, collision);
    NewtonReleaseCollision(nWorld, collision);


	IrrlichtDevice* device = createDevice(EDT_OPENGL, dimension2d<s32>(1000, 680), 16,	false, false, false, 0);

	IVideoDriver* driver = device->getVideoDriver();
	ISceneManager* smgr = device->getSceneManager();

	ISceneNode* cube = smgr->addCubeSceneNode();
    cube->setPosition(vector3df(0,1110,50));
	cube->setMaterialFlag(EMF_LIGHTING, false);
	cube->setMaterialTexture( 0, driver->getTexture("C:/irrlicht-1.4.1/media/irrlichtlogo.jpg"));

    NewtonBodySetUserData(nCube, cube);
    NewtonBodySetMassMatrix(nCube, 1000, 10, 10, 10);
    matrix4 mat;
    mat.setTranslation(vector3df(0, 1110, 50));
    NewtonBodySetMatrix(nCube, &mat.M[0]);
    float omega[3] = {1, 2, 1};
    NewtonBodySetOmega(nCube, &omega[0]);


	ISceneNode* cube1 = smgr->addCubeSceneNode();
    cube1->setPosition(vector3df(0,1125,50));
	cube1->setMaterialFlag(EMF_LIGHTING, false);
	cube1->setMaterialTexture( 0, driver->getTexture("C:/irrlicht-1.4.1/media/dirlogo.jpg"));


    ISceneNode* podloga = smgr->addCubeSceneNode();
    podloga->setScale(vector3df(1000, 1, 1000));
	podloga->setMaterialFlag(EMF_LIGHTING, false);
	podloga->setMaterialTexture( 0, driver->getTexture("C:/irrlicht-1.4.1/media/wall.jpg"));

	ICameraSceneNode* cam = smgr->addCameraSceneNodeFPS();
	cam->setPosition(vector3df(50, 1110, 50));
    smgr->addSkyDomeSceneNode(driver->getTexture("C:/irrlicht-1.4.1/media/irrlicht2_bk.jpg"),16,16,1.3f,2.0f);
    cam->setFarValue(90000);
    cam->setTarget(cube->getPosition());


	while (device->run())
	{
		if (device->isWindowActive())
		{
			driver->beginScene(true, true, SColor(255,255,255,255));

			smgr->drawAll();
            //cam->setTarget(cube->getPosition());
            	if (device->getTimer()->getTime() > lasttick + 10)
            	{
                    lasttick = device->getTimer()->getTime();
                    NewtonUpdate(nWorld, 0.01f);
                }

            float matrix[4][4];
            NewtonBodyGetMatrix(nCube, &matrix[0][0]);
            matrix4 mat;
            memcpy(mat.M, matrix, sizeof(float)*16);
            cube->setPosition(mat.getTranslation());
            cube->setRotation(mat.getRotationDegrees());

			driver->endScene();
		}
	}

	device->drop();
	NewtonDestroy(nWorld);

	return 0;
}
Last edited by petra999 on Wed Jun 18, 2008 5:55 pm, edited 1 time in total.
B@z
Posts: 876
Joined: Thu Jan 31, 2008 5:05 pm
Location: Hungary

Post by B@z »

hi!
i'm not using newton now, but if i remember correctly, there was a problem with the world's size.
if the model is out of the world's bounding box, it freezes, isn't effected by gravity.

i don't remember well, but maybe this makes sense :P
petra999
Posts: 10
Joined: Sat May 31, 2008 5:54 am

Post by petra999 »

you've got right. i change:

Code: Select all

    NewtonCollision* collision = NewtonCreateBox(nWorld, 0, 0, 0, NULL); 
to

Code: Select all

    NewtonCollision* collision = NewtonCreateBox(nWorld, 10000, 10000, 10000, NULL); 
Now my cube is rotating nice.
But why rotating? why not falling down?

Do you know some nice tutorial?

THANKS A LOT!!!!
B@z
Posts: 876
Joined: Thu Jan 31, 2008 5:05 pm
Location: Hungary

Post by B@z »

sorry, i don't remember how to use it xD

i used this source:

http://newtondynamics.com/downloads/MerciorDemo.zip
B@z
Posts: 876
Joined: Thu Jan 31, 2008 5:05 pm
Location: Hungary

Post by B@z »

i played with newton a lot, but i droped it xD i realized that i don't need it.
but i give the code what i made.
it's messy a lot, but maybe you can get something.

http://web.t-online.hu/andreas101/Newt.rar
petra999
Posts: 10
Joined: Sat May 31, 2008 5:54 am

Post by petra999 »

thanks!
matibee
Posts: 3
Joined: Tue Jun 17, 2008 11:02 am

Post by matibee »

You have to add a callback function to your body in which you'll add gravity yourself. There's no concept of gravity explicitly built into Newton - it's just another force acting on a body.

Code: Select all

matrix4  g_matPos;

void BodyForceCB (const NewtonBody* body)
{
  _vec3f vGravity( 0.0f, -9.8f, 0.0f );

  float mass, Ixx, Iyy, Izz;

  // get the mass of the object
  NewtonBodyGetMassMatrix (body, &mass, &Ixx, &Iyy, &Izz);

  // keep the objects postion for rendering	 
  NewtonBodyGetMatrix( body, (float *)&g_matPos[0] );

  // calculate the force on the object
  _vec3f vForce = vGravity * mass;
  NewtonBodySetForce (body, (float *)&vForce );
}
In this function you can;
add other forces to the body
user the (void *) user data of the body to access a particular instance of a body (this func could be used on all bodies this way and you don't need different callbacks for every body)

In your set up code, add this callback to your body

Code: Select all

NewtonBodySetForceAndTorqueCallback( nCube, BodyForceCB );
And why is your collision cudbe 10000 units square??? It's too big for Newton to handle well. Is the cube model really this big?

Cheers
Matt
petra999
Posts: 10
Joined: Sat May 31, 2008 5:54 am

Post by petra999 »

now is working, thanks!
Post Reply