I'm trying to integrate Newton into my project and there's something I seriously don't understand.
I've followed a lot of tutorials about setting the engine up, I make a floor, my cube moves down to the floor, but it doesn't collide, doesn't interact with the floor, pass through it and continues it's way to the infinity...
Here's my floor code :
- Code: Select all
void G_NewtonFloor() {
dVector floorSize (100.0f, 2.0f, 100.0f);
dMatrix location (GetIdentityMatrix());
location.m_posit.m_x = 0.1f;
location.m_posit.m_y = -5.0f;
location.m_posit.m_z = 0.1f;
// create a box for floor
floorCollision = NewtonCreateBox (g_NewtonWorld, floorSize.m_x, floorSize.m_y, floorSize.m_z, NULL);
// create the the floor collision, and body with default values
floorBody = NewtonCreateBody (g_NewtonWorld, floorCollision);
NewtonReleaseCollision (g_NewtonWorld, floorCollision);
NewtonBodySetMaterialGroupID (floorBody, NewtonMaterialGetDefaultGroupID (g_NewtonWorld));
// set the transformation for this rigid body
NewtonBodySetMatrix (floorBody, &location[0][0]);
// set the newton world size based on the bsp size
float boxP0[3];
float boxP1[3];
float matrix[4][4];
NewtonBodyGetMatrix (floorBody, &matrix[0][0]);
NewtonCollisionCalculateAABB (floorCollision, &matrix[0][0], &boxP0[0], &boxP1[0]);
// you can pad the box here if you wish
//boxP0.y -= somevalue;
//boxP1.y += somevaluef;
NewtonSetWorldSize (g_NewtonWorld, (float*)boxP0, (float*)boxP1);
}
Here's my cube code :
- Code: Select all
extern "C" void G_PhysicsAddEntity(gentity_t *ent) {
dFloat Ixx;
dFloat Iyy;
dFloat Izz;
NewtonBody* rigidBody;
NewtonCollision* collision;
dVector origin;
dVector inertia;
dFloat mass, volume;
dMatrix matrix;
float objSizeX, objSizeY, objSizeZ;
matrix.m_posit.m_x = ent->s.origin[0];
matrix.m_posit.m_y = ent->s.origin[1];
matrix.m_posit.m_z = ent->s.origin[2];
objSizeX = 10.0f;//(ent->r.mins[0] + ent->r.maxs[0])/2;
objSizeY = 10.0f;//(ent->r.mins[1] + ent->r.maxs[1])/2;
objSizeZ = 10.0f;//(ent->r.mins[2] + ent->r.maxs[2])/2;
collision = NewtonCreateBox (g_NewtonWorld, objSizeX, objSizeY, objSizeZ, NULL);
volume = 0.5f * NewtonConvexCollisionCalculateVolume (collision);
// calculate the moment of inertia and the relative center of mass of the solid
NewtonConvexCollisionCalculateInertialMatrix (collision, &inertia[0], &origin[0]);
mass = 10;
Ixx = mass * inertia[0];
Iyy = mass * inertia[1];
Izz = mass * inertia[2];
//create the rigid body
rigidBody = NewtonCreateBody (g_NewtonWorld, collision);
// release the collision geometry when not need it
NewtonReleaseCollision (g_NewtonWorld, collision);
// set the correct center of gravity for this body
NewtonBodySetCentreOfMass (rigidBody, &origin[0]);
// set the mass matrix
NewtonBodySetMassMatrix (rigidBody, mass, Ixx, Iyy, Izz);
// activate
// NewtonBodyCoriolisForcesMode (blockBoxBody, 1);
// save the pointer to the graphic object with the body.
NewtonBodySetUserData (rigidBody, ent);
// assign the wood id
NewtonBodySetMaterialGroupID (rigidBody, NewtonMaterialGetDefaultGroupID (g_NewtonWorld));
// set continue collision mode
NewtonBodySetContinuousCollisionMode (rigidBody, 1);
// set a destructor for this rigid body
NewtonBodySetDestructorCallback (rigidBody, PhysicsBodyDestructor);
// set the transform call back function
NewtonBodySetTransformCallback (rigidBody, PhysicsSetTransform);
// set the force and torque call back function
NewtonBodySetForceAndTorqueCallback (rigidBody, PhysicsApplyGravityForce);
// set the matrix for both the rigid body and the graphic body
NewtonBodySetMatrix (rigidBody, &matrix[0][0]);
//NewtonBodySetFreezeTreshold(rigidBody, 1.0, 1.0, 1.0, 1.0);
PhysicsSetTransform (rigidBody, &matrix[0][0], 0);
}
Here's my transform callback :
- Code: Select all
// set the transformation of a rigid body
void PhysicsSetTransform (const NewtonBody* body, const dFloat* matrix, int threadIndex)
{
gentity_t *ent;
dMatrix matr, matf;
ent = (gentity_t*)NewtonBodyGetUserData(body);
NewtonBodyGetMatrix(body, &matr[0][0]);
VectorSet(ent->s.origin, matr.m_posit.m_x, matr.m_posit.m_y, matr.m_posit.m_z);
VectorCopy(ent->s.origin, ent->r.currentOrigin);
VectorCopy(ent->s.origin, ent->s.pos.trBase);
G_Printf("ENT %s : X = %f Y = %f Z = %f\n", ent->targetname, matr.m_posit.m_x, matr.m_posit.m_y, matr.m_posit.m_z);
}
Here's my applyForceAndTorqueCallback :
- Code: Select all
void PhysicsApplyGravityForce (const NewtonBody* body, dFloat timestep, int threadIndex)
{
dFloat Ixx;
dFloat Iyy;
dFloat Izz;
dFloat mass;
dMatrix matr;
NewtonBodyGetMassMatrix (body, &mass, &Ixx, &Iyy, &Izz);
dVector force (0.0f, 0.0f, (mass * -9.8f));
NewtonBodySetForce (body, &force.m_x);
}
I seriously must have missed something or anything must be wrong, could you show me where's the error please ?
And how can I calculate a cube size from it's bounding boxes please ?
I have 6 coordinates for the size : mins.x mins.y mins.z ; maxs.x maxs.y maxs.z
Thanks a lot.