Page 1 of 1

How to make a working camera integrated with Newton ?

Posted: Thu Apr 22, 2004 6:04 pm
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

Posted: Thu Apr 22, 2004 10:33 pm
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,

Posted: Fri Apr 23, 2004 4:05 pm
by Iaro
Thank you very much, i'll try it out.

still not working

Posted: Sat May 01, 2004 6:20 am
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

Posted: Sat May 01, 2004 3:55 pm
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

Posted: Sat May 01, 2004 3:56 pm
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

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

Posted: Mon May 10, 2004 7:11 am
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...

Posted: Mon May 10, 2004 4:33 pm
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

Posted: Mon May 10, 2004 6:14 pm
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