How to make a working camera integrated with 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
Iaro

How to make a working camera integrated with Newton ?

Post by Iaro »

Hello,

I managed to get the newton engine up and running - a cube colliding
with a 3ds surface - , but I'm having problems doing the same thing with the camera. It falls on the ground and I can rotate it but can't move.

Code: Select all


camera = smgr->addCameraSceneNodeFPS(0,350,1500);
camera ->setFarValue(20000.0f);
NewtonBody * cbody;
NewtonCollision * ccoll;
ccoll = NewtonCreateBox(nWorld,38,100,38,NULL);
cbody = NewtonCreateBody(nWorld,ccoll);
NewtonBodySetUserData(cbody, camera);
NewtonBodySetMassMatrix (cbody, 10.0f, 150.0f, 150.0f, 150.0f);
NewtonBodySetFreezeTreshold(cbody, 1.0, 1.0, 1.0, 1.0); 
NewtonBodySetTransformCallback(cbody, SetMeshTransformEvent2);
NewtonBodySetForceAndTorqueCallback(cbody, ApplyForceAndTorqueEvent);
matrix4 cmat;
cmat.setTranslation(vector3df(1300 ,8044 ,1249));
NewtonBodySetMatrix(cbody, &cmat.M[0]);
NewtonBodySetAutoFreeze (cbody , 0); 
NewtonWorldUnfreezeBody(nWorld, cbody); 
NewtonReleaseCollision(nWorld, ccoll);
The callbacks:

Code: Select all

static void _cdecl SetMeshTransformEvent2(const NewtonBody* cbody, const float* matrix)
{
	// copy the matrix into an irrlicht matrix4
	matrix4 cmat;
	memcpy(cmat.M, matrix, sizeof(float)*16);

	// Retreive the user data attached to the newton body
	ICameraSceneNode *camera = (ICameraSceneNode *)NewtonBodyGetUserData(cbody);
	if (camera)
	{
		// Position the node
		camera->setPosition(cmat.getTranslation());		// set position
	//	camera->setRotation(cmat.getRotationDegrees());	// and rotation
	}
}



static void _cdecl 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] = mass * -800.0f; 
   force[2] = 0.0f; 

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

   NewtonBodyAddForce (body, force); 
   NewtonBodyAddTorque (body, torque); 
}
Thanks in advance,
Robert
Guest

Post by Guest »

For the camera maybe it is better not to apply forces, and set the velocity directly,

Code: Select all

static void _cdecl 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] = mass * -800.0f; 
   force[2] = 0.0f; 

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

   NewtonBodyAddForce (body, force); 
   NewtonBodyAddTorque (body, torque); 
*/
    
    float camVeloc[3];
    float camOmega[3];

   ICameraSceneNode *camera = (ICameraSceneNode *)NewtonBodyGetUserData(cbody); 

   camera->GetDesiredVelocity(camVeloc);
  camOmega[0] = 0;
  camOmega[1] = 0;
  camOmega[2] = 0;

  NewtonSetVelocity(body, camVeloc);
  NewtonSetOmega(body, camOmega);
}
this code should move the camera kynematically with collision,
Iaro

Post by Iaro »

Thank you very much, i'll try it out.
Iaro

still not working

Post by Iaro »

Hi,
I've tried it out and still couldn't make the camera to work. Here are a couple of problems :
1.I had to uncomment the NewtonBodyGetMassMatrix function else the
aplication would crash at startup.(illegal operation in newton.dll)
2.The NewtonBodyAddForce function still had to be used, else the camera
wouldn't be affected by gravity.
3.Couldn't find the GetDesiredVelocity function in Irrlicht or Newton documentation so I had to use NewtonBodyGetVelocity(cbody,camVeloc);.
4. Still can't control the camera - from time to time it just bounces on the ground.

I tried other metods like adding impulse to camera but it doesn't work very well.

Code: Select all

    case KEY_KEY_S:
{
float f1[3],f2[3];
f1[0]=-100;f1[1]=0;f1[2]=0;
f2[0]=0;f2[1]=0;f2[2]=0;
NewtonAddBodyImpulse(nWorld,cbody,f1,f2);
}
return true;
     
If someone has managed to make this work could you please post the code.

Robert
Electron
Posts: 874
Joined: Sun Mar 14, 2004 12:05 am
Location: Massachusetts USA

Post by Electron »

well, I'm not sure exactly what your trying to make, but for an fps or something I'd probably make the camera a child node of the character, and not worry about applying any forces to it directly, it would just move when the character did. I haven't actually tried that though
Electron
Posts: 874
Joined: Sun Mar 14, 2004 12:05 am
Location: Massachusetts USA

Post by Electron »

well, I'm not sure exactly what your trying to make, but for an fps or something I'd probably make the camera a child node of the character, and not worry about applying any forces to it directly, it would just move when the character did. I haven't actually tried that though
puh
Posts: 356
Joined: Tue Aug 26, 2003 3:53 pm

Re: How to make a working camera integrated with Newton ?

Post by puh »

Iaro, did you reach success with using Newton for camera collision? If so could you please post your code, as I think it'll be interesting not only for me...
Iaro

Post by Iaro »

Well I can move the camera using the impulse function but I don't know
how to move it in the direction the camera is facing.
Also the camera sometimes gets blocked in the terrain ( usually when climbing).
On the Newton forum I found a thread related to jumping but I think this also involves camera movement.
http://www.physicsengine.com/forum/viewtopic.php?t=254

Robert
roberto
Posts: 3
Joined: Sun Mar 14, 2004 7:26 am

Post by roberto »

I don’t know if you seen it. But there is a new patch for Newton. It has a tutorial on how to control a character or camera. Perhaps you can use with you camera code.

Roberto
Post Reply