player body physic with newton
Posted: Thu Dec 23, 2004 10:16 pm
Hello !
I'm developing a multiplayer first person shooter game with irrlicht for graphics, newton for physics, and raknet for networking.
It's quite usable now, and i want to implement better player physics. before, i used the collision response animator for the gravity and collision (createCollisionResponseAnimator). now i want to handle the player as an NewtonBox with physics, so that f.e. my player will push the other physic objects away when i run against them.
I did this by creating the body like this:
My Callback function for handling the transform callback:
My first question: Is my attempt to implement this correct?
If not, how should i do this? I see some problems, f.e. the player body will be rotatet when it falls, can i set the rotation just on every TransformCallback ?
Now i want to move the box, according to the key input.
To test this, i tried:
But this code seems to have no effect. Which function do I have to use?
NewtonBodyAddForce()
NewtonBodySetForce()
NewtonBodySetVelocity()
Another problem I don't know how to solve is, when i want to create my bullets f.e. of a m16 as models, and with physics. Do I have to create the NewtonBox or Sphere the normal way, and add a high velocity in the direction the bullet should fly to? Or should i use a normal flyanimator and update the physics position every frame?
I hope somebody can help me
Thanks!!!!
BTW: I want to release a playable beta version in a few weeks, and I will release everything opensource.
I'm developing a multiplayer first person shooter game with irrlicht for graphics, newton for physics, and raknet for networking.
It's quite usable now, and i want to implement better player physics. before, i used the collision response animator for the gravity and collision (createCollisionResponseAnimator). now i want to handle the player as an NewtonBox with physics, so that f.e. my player will push the other physic objects away when i run against them.
I did this by creating the body like this:
Code: Select all
const float x = 10.0f, y = 10.0f, z = 10.0f;
NewtonCollision* collision = NewtonCreateBox(nWorld, x,y,z, NULL);
body = NewtonCreateBody(nWorld, collision);
NewtonBodySetMaterialGroupID (body, m_metalMaterial);
NewtonBodySetUserData(body, Game->camera);
float mass = 10.0f;
float Ixx = 0.7f * mass * (y * y + z * z) / 12.0f;
float Iyy = 0.7f * mass * (x * x + z * z) / 12.0f;
float Izz = 0.7f * mass * (x * x + y * y) / 12.0f;
NewtonBodySetMassMatrix (body, mass, Ixx, Iyy, Izz);
// Set callback functions for the body
NewtonBodySetTransformCallback(body, SetMeshTransformEventPlayer);
NewtonBodySetForceAndTorqueCallback(body, CubeApplyForceAndTorqueEvent);
NewtonBodySetAutoFreeze(body, 0);
// Set the position of the body
core::matrix4 mat;
mat.setTranslation(Game->camera->getPosition() * IrrToNewton);
NewtonBodySetMatrix(body, &mat.M[0]);
SetMeshTransformEventPlayer (body, &mat.M[0]);
Code: Select all
void SetMeshTransformEventPlayer(const NewtonBody* body, const float* matrix)
{
// copy the matrix into an irrlicht matrix4
core::matrix4 mat;
memcpy(mat.M, matrix, sizeof(float)*16);
// Retreive the user data attached to the newton body
scene::ISceneNode *tmp = (scene::ISceneNode *)NewtonBodyGetUserData(body);
if (tmp)
{
// Position the node
tmp->setPosition(mat.getTranslation() * NewtonToIrr); // set position
}
}
If not, how should i do this? I see some problems, f.e. the player body will be rotatet when it falls, can i set the rotation just on every TransformCallback ?
Now i want to move the box, according to the key input.
To test this, i tried:
Code: Select all
if ( App->Keys['W'] )
{
core::vector3df v(10.0f,0.0f,0.0f);
NewtonBodySetForce(Player->body, &v.X );
}
NewtonBodyAddForce()
NewtonBodySetForce()
NewtonBodySetVelocity()
Another problem I don't know how to solve is, when i want to create my bullets f.e. of a m16 as models, and with physics. Do I have to create the NewtonBox or Sphere the normal way, and add a high velocity in the direction the bullet should fly to? Or should i use a normal flyanimator and update the physics position every frame?
I hope somebody can help me
Thanks!!!!
BTW: I want to release a playable beta version in a few weeks, and I will release everything opensource.