PDA

View Full Version : Newton weirdness


IBethune
2005.03.24, 06:42 PM
I have implemented a simple 'bouncy block' program using the Newton physics SDK, but I have a few problems.

Firstly, when a block drops ontothe platform, it appears to settle a bit, but then sometimes 'jumps' back up into the air (in an obviously unphysical kind of way). Secondly, is there a way to set an object as static e.g. the ground? so that objects will collide and bounce off it, but it will never move?

Have a look at the demo http://www.pyramid-productions.net/Trucks.tgz (1.76 MB) to see what I mean - hopefully I will have managed to package up all the frameworks correctly this time :wacko:

The (hopefully relevant) code is:

int defaultID;

box = new NewtonBody*[2];

speed = 0.0;
steer = 0.0;

NewtonCollision* collision;

world = NewtonCreate(NULL,NULL);

// Setup default material

defaultID = NewtonMaterialGetDefaultGroupID(world);
NewtonMaterialSetDefaultSoftness (world, defaultID, defaultID, 0.05f);
NewtonMaterialSetDefaultElasticity (world, defaultID, defaultID, 1.0f);
NewtonMaterialSetDefaultCollidable (world, defaultID, defaultID, 1);
NewtonMaterialSetDefaultFriction (world, defaultID, defaultID, 1.0f, 0.5f);


// set the linear solver model for faster speed
NewtonSetSolverModel (world, 8);

// set the adpative friction model for faster speed
NewtonSetFrictionModel (world, 1);

// create the collision shape
collision = NewtonCreateBox (world, 10.0f, 10.0f, 10.0f, NULL);

// create the rigid body
box[0] = NewtonCreateBody (world, collision);

// Get rid of the collision
NewtonReleaseCollision (world, collision);

// set the body mass and inertia
NewtonBodySetMassMatrix (box[0], 5.0f, 1.0f, 1.0f, 1.0f);

// set the transformation matrix
dMatrix matrix = GetIdentityMatrix();
matrix.m_posit.m_x = 0.0f;
matrix.m_posit.m_y = 0.0f;
matrix.m_posit.m_z = 50.0f;

PrintMatrix(matrix);
NewtonBodySetMatrix (box[0], &matrix[0][0]);

// animate the body by setting the angular veocity
dVector omega (1.0f, 1.0f, 1.0f);
NewtonBodySetOmega (box[0], &omega[0]);

// Set the physics callback
NewtonBodySetForceAndTorqueCallback (box[0], PhysicsApplyForceAndTorque);

NewtonBodySetMaterialGroupID(box[0], defaultID);

NewtonBodySetLinearDamping(box[0], 0.0f);
float damp[3] = {0.0f, 0.0f, 0.0f};
NewtonBodySetAngularDamping(box[0], damp);

// Create the floor

// create the collision shape
collision = NewtonCreateBox (world, 100.0f, 100.0f, 1.0f, NULL);

// create the rigid body
box[1] = NewtonCreateBody (world, collision);

// Get rid of the collision
NewtonReleaseCollision (world, collision);

// set the body mass and inertia
//NewtonBodySetMassMatrix (box[1], 1000000.0f, 1000000.0f, 10000000.0f, 1000000.0f);

// set the transformation matrix
matrix = GetIdentityMatrix();
matrix.m_posit.m_x = 0.0f;
matrix.m_posit.m_y = 0.0f;
matrix.m_posit.m_z = -0.5f;
PrintMatrix(matrix);
NewtonBodySetMatrix (box[1], &matrix[0][0]);

NewtonBodySetMaterialGroupID(box[1], defaultID);



...


void PhysicsApplyForceAndTorque (const NewtonBody* body)
{
float mass, Ixx, Iyy, Izz;

NewtonBodyGetMassMatrix (body, &mass, &Ixx, &Iyy, &Izz);
float force[4] = {0.0f, 0.0f, -mass * 9.8f};
NewtonBodyAddForce (body, force);
}


Any light on this would be great.

Cheers

- Iain

OneSadCookie
2005.03.24, 07:49 PM
To make an object static, don't set its mass matrix. The default has infinite mass and infinite rotational inertia.

As for the bounciness, I haven't seen it myself. You might need to up the auto-freeze threshold, or you might need to up your physics framerate. In my current experiment, 60Hz physics let a block fall through another one, where 100Hz was fine. YMMV.

IBethune
2005.03.25, 08:09 AM
I eventually found that the combination of low 'softness' with high 'elasticity' is bad for stability of the system. I've gone with softness=0.15 and elasticity=0.4, which is actually pretty realistic.

I need to work out the moments of inertia of the principle axes of a cube also, as setting these too low causes the cube to spin easily when it collides. I'll try to figure it out.

- Iain

IBethune
2005.03.25, 08:24 AM
If my maths is good, I calculated the moment of inertia of the principal axes of a cube to be

Ix = M*(y^2 + z^2)/12

and similarly for the other axes.

- Iain