Help with NEWTON Physics Please.

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
DGENXP
Posts: 124
Joined: Tue Dec 27, 2005 2:49 am
Contact:

Help with NEWTON Physics Please.

Post by DGENXP »

Hi all

I have created a project which uses irrlicht and Newton. i have followed the intergration tutorial. What i need to know now though is in the simplest possible way how to use the Damn physics the only other tutorial that i have come across is the one that is added to the tutorials section of the irrlich website. This is way to complicated to follow.

when i completed it, it didn't work.

I use Dev C++ and irrlicht 0.14.0 i know this is out of date so just please get to the point mentioned in this post I beg of you.

what i want to do:

make a Sydney.md2 rigid and a Feary.MD2 a physical object and make them colide as if dropped onto it :)

here is the Initialisation code i used:

Code: Select all

#include <Irrlicht.h>
#include "newton.h"

static NewtonWorld* nWorld;

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

int main()
{
   nWorld = NewtonCreate (NULL, NULL);
   
   IrrlichtDevice *device = createDevice(EDT_OPENGL,
                            dimension2d<s32>(640,480), false);

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

   while(device->run())
   {
      driver->beginScene(true, true, video::SColor(0,0,0,0));
      smgr->drawAll();
      driver->endScene();
   }
   device->drop();
   return 0;
}
Please any help will be appreciated:

P.S: I may be able to build a full RPG APP But When it comes to physics im no issac newton. so please help me as simply as you can.

Cheers: DGEN XP
DGENXP
Posts: 124
Joined: Tue Dec 27, 2005 2:49 am
Contact:

Post by DGENXP »

Why does everyone recommend using Newton when nobody seems to know how to use it.

I still cannot work it out myself so please if anyone knows then please post here or help me find the right place to put this post.
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

I think the reason that nobody is responding is because it is pretty obvious that you aren't using the resources availible to you. A search for Newton on these forums turns code that would help you. Then there is the Newton forums and documentation...
the only other tutorial that i have come across is the one that is added to the tutorials section of the irrlich website. This is way to complicated to follow.
I think you are giving up to easily. Spend some time looking over the Newton examples and documentation, the stuff isn't all that complicated. Start out with a simple example and learn from that.

http://irrlicht.sourceforge.net/tut_newton.html
http://sio2.g0dsoft.com/modules/wmpdownloads/
http://s-fonline.com/webhosting/dhenton ... ondemo.php

Travis
JonLT
Posts: 152
Joined: Thu Mar 15, 2007 5:47 pm
Location: Denmark

Post by JonLT »

A search for Newton on these forums turns code that would help you. Then there is the Newton forums and documentation
Do this and you'll be fine!
Newton isn't really that hard when you get to know it (I guess it's that way with most of life)
But what you need to do is, create a newton body for every scene node you want to repsond to the physics. Most of the fun is happening in the newton callbacks, check out mercior's tutorial, it really helped me!
grassblade
Posts: 27
Joined: Mon Jan 09, 2006 11:17 am

Post by grassblade »

Look and try hard as Vitek has suggested. Anyway here is some code
rom the wiki which i found working. There is some problem with the compiled lib files of Newton with DevC++. Have a look at Newton Forum.

Code: Select all

#include "Irrlicht.h"
#include "newton.h"
#include <iostream>

using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
 
//Now we create a pointer to a Newton "World" object 
static NewtonWorld* nWorld;
 //the is the main newton object. Next we add a pointer for a rigid body object 
static NewtonBody* body;
 
 IVideoDriver* driver = 0;
 IrrlichtDevice *device = 0;
 ISceneManager* smgr = 0;
 
scene::ISceneNode* boxNode = 0;
ISceneNode *cam = 0;
unsigned int lasttick;

void InitScene();
void DrawScene();

void InitScene()
{
      device = createDevice(EDT_OPENGL,
                          dimension2d<s32>(640,480),
                          false);

     driver = device->getVideoDriver();
     smgr = device->getSceneManager();
     
     nWorld = NewtonCreate (NULL, NULL);
 
    
   // boxNode = smgr->addAnimatedMeshSceneNode(smgr->getMesh("../../media/smallcube.3ds"));
    
    //boxNode->setMaterialTexture(0, driver->getTexture("../../media/Faerie5.bmp"));
 //boxnode =smgr->addTestSceneNode();
 boxNode = smgr->addTestSceneNode();
 boxNode->setMaterialTexture(0, driver->getTexture("../../media/Faerie5.bmp"));
 boxNode->setScale(core::vector3df(8,8,8));
 
     NewtonCollision *collision;
     collision = NewtonCreateBox(nWorld, 0, 0, 0, NULL);
 

        body = NewtonCreateBody (nWorld, collision);

        NewtonReleaseCollision (nWorld, collision);        
 

 
        NewtonBodySetUserData(body, boxNode);
 


        NewtonBodySetMassMatrix (body, 100.0f, 1.0f, 1.0f, 1.0f);
 


        matrix4 mat;
        mat.setTranslation(vector3df(0,0,0));
        NewtonBodySetMatrix(body, &mat.M[0]);

        float omega[3] = {1.0f, 2.0f, 1.0f};
        NewtonBodySetOmega (body, &omega[0]);
 
 
        cam = smgr->addCameraSceneNodeMaya(0,-1500.0f,200.0f,500.0f,0);
        cam->setPosition(vector3df(0, 300, 0));

}
 

 
void DrawScene()
{
        if (device->getTimer()->getTime() > lasttick + 10) {        
                lasttick = device->getTimer()->getTime();
                NewtonUpdate(nWorld, 0.01f);
        }
 
        float matrix[4][4];
        NewtonBodyGetMatrix(body, &matrix[0][0]);
    
        matrix4 mat;
        memcpy(mat.M, matrix, sizeof(float)*16);
 
    boxNode->setPosition(mat.getTranslation());
    boxNode->setRotation(mat.getRotationDegrees());
}
 

int main()
{
   
  InitScene();
     
   
   while(device->run())
   {
     
 DrawScene();
      driver->beginScene(true, true, video::SColor(0,0,0,0));

      smgr->drawAll();

      driver->endScene();
   }
   NewtonDestroy(nWorld);

   device->drop();

   return 0;
}
Wish u well.
DGENXP
Posts: 124
Joined: Tue Dec 27, 2005 2:49 am
Contact:

Post by DGENXP »

yes thank you for the code you supplied though this is the code that i initially worked from and all it does it rotate the node is it supposed to do this?

here is my code so far:

Code: Select all

#include <Irrlicht.h>
#include "newton.h"

static NewtonWorld* nWorld;
static NewtonBody* PlayrPhys;

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

IAnimatedMesh* test;
IAnimatedMeshSceneNode* Player;
NewtonCollision *collision; 

int main()
{
   nWorld = NewtonCreate (NULL, NULL);
   
   IrrlichtDevice *device = createDevice(EDT_OPENGL,
                            dimension2d<s32>(640,480), false);

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

collision = NewtonCreateBox(nWorld, 0, 0, 0, NULL);
PlayrPhys = NewtonCreateBody (nWorld, collision); 



test = smgr->getMesh("characters/knight.md2");
Player = smgr->addAnimatedMeshSceneNode(test);
Player->setPosition(vector3df(0.0f,4.0f,14.0f));

NewtonReleaseCollision (nWorld, collision); 
NewtonBodySetUserData(PlayrPhys, Player); 
NewtonBodySetMassMatrix (PlayrPhys, 100.0f, 1.0f, 1.0f, 1.0f); 

   while(device->run())
   {
      driver->beginScene(true, true, video::SColor(155,155,0,0));
      smgr->drawAll();
      driver->endScene();
   }
   device->drop();
   return 0;
}
i thought tha ths would make the knight fall forever but it does not i just cannot work out why this is have i missed something I know i have to add a Newton Update in the while statement. i have just forgotten to add this so please can you help?

Please i really want to get this newton library working so i can implement it into my program.

just do what you think is needed and please explain where i am going wrong as i want to learn how to use Newton and not need to always rely on others. So any help would be much appreciated

Cheers DGENXP
sio2
Competition winner
Posts: 1003
Joined: Thu Sep 21, 2006 5:33 pm
Location: UK

Post by sio2 »

Get IrrNewt or IPhysics. Or get my demo and look at the source.

Your post only hints at this, so I may be wrong, but are you looking for ragdoll physics? That's more involved...
olivehehe_03
Posts: 157
Joined: Tue Mar 20, 2007 8:30 am

Post by olivehehe_03 »

DGENXP wrote: here is my code so far:

Code: Select all

   while(device->run())
   {
      driver->beginScene(true, true, video::SColor(155,155,0,0));
      smgr->drawAll();
      driver->endScene();
   }
The problem here is that you're not updating the physics every loop. The code from the DrawScene function in one of the other posts looks like it does this right, just stick that in your main loop (trial and error for where it goes, don't ask me, I don't know :D)

Code: Select all

        if (device->getTimer()->getTime() > lasttick + 10) {       
                lasttick = device->getTimer()->getTime();
                NewtonUpdate(nWorld, 0.01f);
        }
Anyways, what I'm really here for is the info about using newton, which is pretty good, thanks for the links and code
Tell me what you cherish most. Give me the pleasure of taking it away.
roxaz
Posts: 575
Joined: Tue Jan 23, 2007 8:35 pm
Location: LT

Post by roxaz »

sio2 wrote:Get IrrNewt or IPhysics. Or get my demo and look at the source.

Your post only hints at this, so I may be wrong, but are you looking for ragdoll physics? That's more involved...
physics frameworks take away oportunity of better learning and maybe some newton features
sio2
Competition winner
Posts: 1003
Joined: Thu Sep 21, 2006 5:33 pm
Location: UK

Post by sio2 »

roxaz wrote:
sio2 wrote:Get IrrNewt or IPhysics. Or get my demo and look at the source.

Your post only hints at this, so I may be wrong, but are you looking for ragdoll physics? That's more involved...
physics frameworks take away oportunity of better learning and maybe some newton features
I've pointed the OP to three sets of source code that will get the OP up-and-running and in a position to learn how to use native Newton API or use the wrappers if they do the job well enough. My own demo uses Newton API calls.

All you have done is post snide "know it all" comments that help no-one.
DGENXP
Posts: 124
Joined: Tue Dec 27, 2005 2:49 am
Contact:

Post by DGENXP »

hi again

ok I have finaly been able to get 1 node to fall but the problem is now to create more than 1 and how would i get it to colide with another node

Code: Select all

#include "Irrlicht.h" 
#include "newton.h" 
#include <iostream> 

#include <Irrlicht.h>
#include <memory.h>
#include <iostream>

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

//Now we create a pointer to a Newton "World" object
#define NEWTON_GRAVITY 900.0f 
static NewtonWorld* nWorld; 

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;
}

//the is the main newton object. Next we add a pointer for a rigid body object 
static NewtonBody* body; 
static NewtonBody* Floor; 
  
 IVideoDriver* driver = 0; 
 IrrlichtDevice *device = 0; 
 ISceneManager* smgr = 0; 
  
scene::ISceneNode* boxNode = 0; 
scene::ISceneNode* FloorNode = 0; 
ISceneNode *cam = 0; 
unsigned int lasttick; 

void InitScene(); 
void DrawScene(); 
  
void InitScene() 
{ 
      device = createDevice(EDT_OPENGL, 
                          dimension2d<s32>(640,480), 
                          false); 

     driver = device->getVideoDriver(); 
     smgr = device->getSceneManager(); 
      
     nWorld = NewtonCreate (NewtonAlloc, NewtonFree); 
  
	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);
    
 boxNode = smgr->addTestSceneNode(); 

 FloorNode = smgr->addTestSceneNode(); 
 FloorNode->setPosition(core::vector3df(64,0, 8));  

 FloorNode->setScale(core::vector3df(2,32,32)); 
 boxNode->setScale(core::vector3df(2,16,8)); 
  
     NewtonCollision *collision; 
     collision = NewtonCreateBox(nWorld, 0, 0, 0, NULL); 
  

        body = NewtonCreateBody (nWorld, collision); 
        Floor = NewtonCreateBody (nWorld, collision); 
                
        NewtonReleaseCollision (nWorld, collision);  
              
        NewtonBodySetUserData(body, boxNode); 
        NewtonBodySetUserData(Floor, FloorNode); 
        
        NewtonBodySetMassMatrix (body, 100.0f, 1.0f, 1.0f, 1.0f); 
        NewtonBodySetMassMatrix (Floor, 100.0f, 1.0f, 1.0f, 1.0f);   


        matrix4 mat; 
        mat.setTranslation(vector3df(0,0,0)); 
        NewtonBodySetMatrix(body, &mat.M[0]); 
        NewtonBodySetMatrix(Floor, &mat.M[0]); 
       // float omega[3] = {0.0f, 1.0f, 0.0f};         


       // NewtonBodySetVelocity (body, &omega[0]);
       // NewtonBodyAddForce (body, &omega[0]);  
  
        cam = smgr->addCameraSceneNodeMaya(0,-1500.0f,200.0f,500.0f,0); 
        cam->setPosition(vector3df(0, 300, 0)); 

} 
  
// Newton callback
void applyForceAndTorqueEvent(const NewtonBody* body)
{
	float mass;
	float Ixx;
	float Iyy;
	float Izz;

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

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

	NewtonBodySetForce(body, &force.X);
} 

void applyForceAndTorque(const NewtonBody* Floor)
{
	float massF;
	float IxxF;
	float IyyF;
	float IzzF;

	NewtonBodyGetMassMatrix (Floor, &massF, &IxxF, &IyyF, &IzzF);

	vector3df force (NEWTON_GRAVITY * massF , 0.0f , 0.0f);
	vector3df torque (0.0f, 0.0f, 0.0f);

	NewtonBodySetForce(Floor, &force.X);
}

void DrawScene() 
{     
      NewtonBodySetForceAndTorqueCallback(body, applyForceAndTorqueEvent); 
      NewtonBodySetForceAndTorqueCallback(Floor, applyForceAndTorque);  
      
        if (device->getTimer()->getTime() > lasttick + 10) {        
                lasttick = device->getTimer()->getTime(); 
                NewtonUpdate(nWorld, 0.01f); 
        } 
  
        float matrix[4][4]; 
        NewtonBodyGetMatrix(body, &matrix[0][0]); 
    
        float matrixF[4][4]; 
        NewtonBodyGetMatrix(Floor, &matrixF[0][0]); 
    
        matrix4 mat; 
        memcpy(mat.M, matrix, sizeof(float)*16); 
        
    
    boxNode->setPosition(mat.getTranslation()); 
    boxNode->setRotation(mat.getRotationDegrees()); 
     
 
    

} 
  

int main() 
{ 
    
  InitScene(); 
      
    
   while(device->run()) 
   { 
      
 DrawScene(); 
      driver->beginScene(true, true, video::SColor(0,0,0,0)); 

      smgr->drawAll(); 

      driver->endScene(); 
   } 
   NewtonDestroy(nWorld); 

   device->drop(); 

   return 0; 
}
Why will the body not colide with the floor it just seems to pass right through it, i have created them both as individual bodies and applied 2 different callbacks but it just does not seem to work

Any help or code fixes would be appreciated

cheers DGENXP
Someone once said A Jack of all trades is a master of none.

Join the Community at the RB3D Forums:

http://www.rpbuilder.org/forum
Post Reply