- Code: Select all
void NDE_SetMeshTransformEvent(const NewtonBody* body, const float* matrix)
{
mat4x4_t mat;
memcpy(mat, matrix, sizeof(float)*16);
if (body)
{
vec3_t origin;
// Position the node
//tmp->setPosition(mat.getTranslation()); // set position
//tmp->setRotation(mat.getRotationDegrees()); // and rotation
mat3x3_t mat3;
Matrix4_Matrix3 (mat, mat3);
Matrix3_TransformVector (mat3, vec3Origin, origin);
Com_Printf (0, "%f %f %f\n", origin[0], origin[1], origin[2]);
}
}
void NDE_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] = NEWTON_GRAVITY * mass;
force[2] = 0.0f;
torque[0] = 0.0f;
torque[1] = 0.0f;
torque[2] = 0.0f;
NewtonBodyAddForce (body, force);
NewtonBodyAddTorque (body, torque);
}
void TestPhysics (void)
{
NewtonCollision *collision;
NewtonBody *body;
mat4x4_t mat = {0,0,0,0,0,0,0,0,0,0,0,0,0,0};
// Create a box primitive.
collision = NewtonCreateBox(mWorld, 38, 38, 38, NULL);
body = NewtonCreateBody(mWorld,collision);
// Set user data pointer to the scene node
NewtonBodySetUserData(body, NULL);
// Set body mass & inertia matrix
NewtonBodySetMassMatrix (body, 10.0f, 150.0f, 150.0f, 150.0f);
// Set the freeze threshhold to 1 unit (default is 0.01 but irrlight uses a large unit scale)
NewtonBodySetFreezeTreshold(body, 1.0, 1.0, 1.0);
// Set callback functions for the body
NewtonBodySetTransformCallback(body, NDE_SetMeshTransformEvent);
NewtonBodySetForceAndTorqueCallback(body, NDE_ApplyForceAndTorqueEvent);
// Set the position of the body
Matrix4_Translate (mat, 0, 0, 0);
NewtonBodySetMatrix(body, &mat[0]);
}
static void *cmdtest;
void NDE_CGInit ()
{
cmdtest = cgi.Cmd_AddCommand("testphysics", TestPhysics, "OMG");
}
void NDE_Init ()
{
int i;
Com_Printf (0, "====== Initializing Newdon Dynamics Engine ======\n");
// Init newton
mWorld = NewtonCreate(NULL, NULL);
// Set up default material properties for newton
i = NewtonMaterialGetDefaultGroupID(mWorld);
NewtonMaterialSetDefaultFriction (mWorld, i, i, 0.8f, 0.4f);
NewtonMaterialSetDefaultElasticity (mWorld, i, i, 0.3f);
NewtonMaterialSetDefaultSoftness (mWorld, i, i, 0.05f);
NewtonMaterialSetCollisionCallback (mWorld, i, i, NULL, NULL, NULL, NULL);
Com_Printf (0, "====== Done Initializing NDE =====\n");
}
void NDE_Shutdown ()
{
Com_Printf (0, "Shutting down NDE...\n");
if (g_newtonMap)
// release the collision tree
NewtonReleaseCollision(mWorld, g_newtonMap);
// destory the newton world object
if (mWorld)
NewtonDestroy(mWorld);
else
{
Com_Printf (0, "Nothing to shutdown..\n");
return;
}
Com_Printf (0, "Done\n");
}
void NDE_Update ()
{
if (!mWorld)
return;
NewtonUpdate(mWorld, 0.01f);
}
The update function does get called. Any ideas, help? I'm using the lib_mt library, no DLL, with _NEWTON_USE_LIB defined in Newton.h (it didn't seem to recognize that I chose the library).
EDIT: here's the BSP load code in case maybe it's the problem:
- Code: Select all
void R_CreateLevelBody ()
{
int i;
mBspModel_t *BspModel = &ri.scn.worldModel->bspModel;
mesh_t *curSurf;
int j;
int v1i, v2i, v3i;
float vArray[9]; // vertex array (3*3 floats)
int tmpCount = 0;
vec3_t *mb_vertices;
index_t *mb_indices;
// set the newton world size based on the bsp size
float boxP0[3];
float boxP1[3];
float matrix[4][4];
if (!BspModel)
return;
CL_NewtonCreateTreeCollision();
CL_NewtonTreeCollisionBeginBuild();
for (i=0; i < BspModel->numSurfaces; i++)
{
curSurf = BspModel->surfaces[i].mesh;
mb_vertices = curSurf->vertexArray;
mb_indices = curSurf->indexArray;
// add each triangle from the mesh
for (j=0; j < curSurf->numIndexes; j+=3)
{
v1i = mb_indices[j];
v2i = mb_indices[j+1];
v3i = mb_indices[j+2];
vArray[0] = mb_vertices[v1i][0];
vArray[1] = mb_vertices[v1i][1];
vArray[2] = mb_vertices[v1i][2];
vArray[3] = mb_vertices[v2i][0];
vArray[4] = mb_vertices[v2i][1];
vArray[5] = mb_vertices[v2i][2];
vArray[6] = mb_vertices[v3i][0];
vArray[7] = mb_vertices[v3i][1];
vArray[8] = mb_vertices[v3i][2];
CL_NewtonTreeCollisionAddFace(3, (float*)vArray, 12, 1);
}
}
CL_NewtonTreeCollisionEndBuild();
CL_NewtonCreateBody();
CL_NewtonBodyGetMatrix (&matrix[0][0]);
CL_NewtonCollisionCalculateAABB (&matrix[0][0], &boxP0[0], &boxP1[0]);
// you can pad the box here if you wish
//boxP0.y -= somevalue;
//boxP1.y += somevaluef;
CL_NewtonSetWorldSize ((float*)boxP0, (float*)boxP1);
}
The boxP0 and boxP1 are this, on q2dm1 "The Edge":
- boxP1 0x0012ee94 float [3]
[0] 2064.0000 float
[1] 1792.0000 float
[2] 1216.0000 float
- boxP0 0x0012eea8 float [3]
[0] -192.00000 float
[1] -448.00000 float
[2] 192.00000 float
EDIT: Second question, I don't know how to work with matrices, and the engine I use only comes with functions for copying and translating between matrices, but the entities themselves use vectors.. is there any way to convert between the two?
-Paril