Newton 2.0 and Irrlicht 1.7

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
g0bl1n
Posts: 63
Joined: Thu Feb 21, 2008 8:45 am

Newton 2.0 and Irrlicht 1.7

Post by g0bl1n »

So I've been messing around with some old Newton tutorials. I've gotten a lot of them working but none of them really do any kind of physics that I can see. I can't really do anything on my own because I do not understand everything that is going on. I was hoping someone could shed some light on what this code is doing step by step, I've added in some comments of what I think is going on:

Code: Select all

//Create Newton World
NewtonWorld* nWorld=NewtonCreate();

Code: Select all

//In the Main
//Create Timer Variable
unsigned int lasttick=0;

//Create a cube node
ISceneNode* boxNode=smgr->addCubeSceneNode();

//Create collision manager
NewtonCollision *collision=NewtonCreateBox(nWorld,0,0,0,0,0);

//Create the newton body for the boxNode irrlicht node
NewtonBody* body=NewtonCreateBody(nWorld,collision);

//Release the collision node
NewtonReleaseCollision(nWorld,collision);

//No idea here
NewtonBodySetUserData(body,boxNode);

//set mass of cube?
NewtonBodySetMassMatrix(body,100.0,1.0,1.0,1.0);

//No idea
matrix4 mat;
mat.setTranslation(vector3df(0,0,0));
NewtonBodySetMatrix(body,&mat.pointer()[0]);

//No idea
float omega[3]={1.0,2.0,1.0};
NewtonBodySetOmega(body,&omega[0]);

Code: Select all

//In the main draw while loop
//update newton world in a timely fashion
if (device->getTimer()->getTime() > lasttick + 10)
{	
	lasttick = device->getTimer()->getTime();
	NewtonUpdate(nWorld, 0.01f);
}

//No Idea
float matrix[4][4];
NewtonBodyGetMatrix(body, &matrix[0][0]);

//Not sure, but rotating the cube?
matrix4 mat;
memcpy(mat.pointer(), matrix, sizeof(float)*16);
boxNode->setPosition(mat.getTranslation());
boxNode->setRotation(mat.getRotationDegrees());
I have the world creation and I drop the newton world at the end, but this tutorial doesn't make any sense to me. I get a cube rotating on the y axis, which I understand why its doing that (last little part of the code above). But all the matrices and things like that are throwing me off.

Any help is appreciated! If anyone could help me write a simple example, such as creating a floor object and a single cube object that is created above it, would be most helpful. All I really need is a bare-bones basic little example to get started, most of the ones I have found simply do not work with the new version of newton.

One thing I noticed is that there are a lot of NULL arguments that I cannot initialize that way. Like the world creation line:

Code: Select all

NewtonWorld* nWorld=NewtonCreate(NULL,NULL);
In the new version there aren't any arguments for that call, so I initialized it like this:

Code: Select all

NewtonWorld* nWorld=NewtonCreate();
And it worked...but there have been other instances of NULL in which I just replaced it with 0. Such as:

Code: Select all

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

Code: Select all

NewtonCollision *collision=NewtonCreateBox(nWorld,0,0,0,0,0);
Again, any info/help is most appreciated!!!
Birdman
Posts: 25
Joined: Mon Jan 28, 2008 5:45 am
Location: Buenos Aires

Post by Birdman »

This tutorial is working for me:

http://www.newtondynamics.com/wiki/inde ... utorial%29

Basically newton works with some callbacks, where it applies physics to irr objects.

I made this part work, i can make a Terrain Mesh become a Mesh in Physics, to work as ground.

Then i add some object and make it a Box, and it collision and stop falling in the floor.

Thats far as i got atm, i was trying to finish implementing a Raycast Car, but my wheels are not working propertly and i saw no tutorial yet on how to apply forceto the car. Im almost done but yet have to check what i did to see whats wrong.

Mostly i saw there are some IrrToNewton or NewtonToIrr constants arround being used by others in Newton Physics, but anyway without updated tutos and proper documentation, you can't advance as fast as you would like.
The sleeper must awaken!
g0bl1n
Posts: 63
Joined: Thu Feb 21, 2008 8:45 am

Post by g0bl1n »

Does anyone know of a physics engine that has good documentation and maybe has some examples targeted at Irrlicht 1.7?

I checked out Physx and there is little to none documentation with the IrrPhysx I have found. Also all of the examples are targeted at 1.5 (I think it was 1.5 at least). Also the lib would have to be recompiled for 1.7...

I'm really only using some basic physics in my game and some ragdolls. So I'm pretty sure almost any engine would suit my needs. If anyone could point me in the direction of a ok to well documented physics engine, especially one with some examples for the newest version of Irrlicht, I would be much obliged!
Mag-got
Posts: 42
Joined: Tue Dec 04, 2007 5:53 pm

Re: Newton 2.0 and Irrlicht 1.7

Post by Mag-got »

Code: Select all

//Create collision manager
NewtonCollision *collision=NewtonCreateBox(nWorld,0,0,0,0,0);
It creates a box collision shape

Code: Select all

//No idea here
NewtonBodySetUserData(body,boxNode);
A body has a pointer to it's user data, and the user data can be a pointer to anything you wish. In this case it's the boxNode.
It's boxNode so that the boxNode's position could be updated from the body's transform callback, so that every time the body's transformation changes, it changes the nodes transformation too

Code: Select all

//set mass of cube?
NewtonBodySetMassMatrix(body,100.0,1.0,1.0,1.0);
Yes, and also the body's inertia matrix

Code: Select all

//No idea
matrix4 mat;
mat.setTranslation(vector3df(0,0,0));
NewtonBodySetMatrix(body,&mat.pointer()[0]);

//No idea
float omega[3]={1.0,2.0,1.0};
NewtonBodySetOmega(body,&omega[0]);
Creates a matrix mat, sets it translation to (0, 0, 0), sets the body's transformation matrix to mat. Then the code sets the body's angular velocity to omega.

Code: Select all

//No Idea
float matrix[4][4];
NewtonBodyGetMatrix(body, &matrix[0][0]);

//Not sure, but rotating the cube?
matrix4 mat;
memcpy(mat.pointer(), matrix, sizeof(float)*16);
boxNode->setPosition(mat.getTranslation());
boxNode->setRotation(mat.getRotationDegrees());
It updates the node's transformation, so that it's the same as the body's.
One thing I noticed is that there are a lot of NULL arguments that I cannot initialize that way.
NULL is 0 (as I have learned)

This code creates a body and rotates it. You need callbacks (force and torque callback, transform callback)

Code: Select all

// add force and torque to rigid body
void  physicsApplyForceAndTorque(const NewtonBody* body)
{
	dFloat Ixx;
	dFloat Iyy;
	dFloat Izz;
	dFloat mass;

	NewtonBodyGetMassMatrix(body, &mass, &Ixx, &Iyy, &Izz);
	float force[3] = {0.0f, mass * GRAVITY, 0.0f};
	NewtonBodySetForce(body, force);
}

// set the transformation of a rigid body
//You have to do this so that the Newton body and irrlicht scene node are in sync,
//and you actually see the changes created by the physics simulation
void  physicsSetTransform(const NewtonBody* body, const float* matrix)
{
	ISceneNode* node;

	// get the graphic object form the rigid body
	//Newton can store a pointer to the scene node in the body object.
	node = (ISceneNode*)NewtonBodyGetUserData(body);

	//There's no easy way to get the information from one to the other, so a memory copy is required.
	//The matrix is a 4x4 float
	matrix4 mat;
	memcpy(mat.M, matrix, sizeof(float)*16);


	//In order to get the transform right, you have to apply the translation and the rotation
	//from the Newton body to the irrlicht scene node.
	vector3df position (mat.getTranslation());

	//Newton and Irrlicht unit sizes do not match,
	//so the position needs to be converted.
	//position *=NEWTONTOIRR;

	vector3df rotation = mat.getRotationDegrees();

	primitive->setPosition (position);
	primitive->setRotation(rotation);
}
If you want to convert the position to what it should be, add these lines somewhere before you use them:

Code: Select all

#define GRAVITY -9.8f
#define NEWTONTOIRR 32.0f; //conversion factor between the two
#define IRRTONEWTON 1.0f/NEWTONTOIRR;
To use the callbacks, you must call these functions:

Code: Select all

NewtonBodySetForceAndTorqueCallback(body, physicsApplyForceAndTorque);
NewtonBodySetTransformCallback(body, physicsSetTransform);
Link: http://newtondynamics.com/wiki/index.ph ... =Main_Page
kwitee
Posts: 8
Joined: Fri May 07, 2010 9:16 pm

Applying dislocation and rotation

Post by kwitee »

Birdman wrote:This tutorial is working for me:

http://www.newtondynamics.com/wiki/inde ... utorial%29

...
Hi, I am using this tutorial for my project. I used the CreateNewtonMesh with some changes to load a static map from *.irr file saved in irrEdit. I noticed that it calculates with mesh scales, but it dont recognize rotation or dislocation of meshes in irrEdit.
This is as I hope the important part of my method:

Code: Select all

this->n_collision = NewtonCreateTreeCollision(this->nWorld, NULL);
		NewtonTreeCollisionBeginBuild(this->n_collision);
		for (u32 i = 0; i < this->irr_mesh->getMeshBufferCount(); i++){
			IMeshBuffer *mb = this->irr_mesh->getMeshBuffer(i);
			vector3df vArray[3];
			S3DVertex* mb_vertices;
			switch(mb->getVertexType())
			{
			case EVT_STANDARD:
				mb_vertices = (S3DVertex*) mb->getVertices();
				break;
			case EVT_2TCOORDS:
				mb_vertices = (S3DVertex2TCoords*) mb->getVertices();
				break;
			case EVT_TANGENTS:
				mb_vertices = (S3DVertexTangents*) mb->getVertices();
				break;
			default:
				printf("Newton error: Unknown vertex type in static mesh: %d\n", mb->getVertexType());
				break;
			}
			u16* mb_indices  = mb->getIndices();
			for (unsigned int j = 0; j < mb->getIndexCount(); j += 3){
				int v1i = mb_indices[j + 0];
				int v2i = mb_indices[j + 1];
				int v3i = mb_indices[j + 2];

				vArray[0] = mb_vertices[v1i].Pos * this->irr_node->getScale().X; // this is as I hope the aplying of scale
				vArray[1] = mb_vertices[v2i].Pos * this->irr_node->getScale().Y;
				vArray[2] = mb_vertices[v3i].Pos * this->irr_node->getScale().Z;

				NewtonTreeCollisionAddFace(this->n_collision, 3, &vArray[0].X, sizeof (vector3df), 1);
			}
		}
		NewtonTreeCollisionEndBuild(this->n_collision, 2);
So my question is, how can I apply rotation and dislocation of Vertexes. I hope I posted it in right place, also sorry for my English.
kwitee
Posts: 8
Joined: Fri May 07, 2010 9:16 pm

Solution

Post by kwitee »

Hi, I have found solution to my problem. So I post it here just in case:
I applied transformation matrix on my rigid body like this:

Code: Select all

CMatrix4<f32> mat;
mat.setTranslation(irrlicht_node->getPosition());
mat.setRotationDegrees(irrlicht_node->getRotation());
NewtonBodySetMatrix(newton_body, mat.pointer());
Post Reply