NewtonBody* CPhysics::CreateRigidBody(NewtonWorld *world, CObject3D *object, NewtonCollision *collision, float mass) { object->UpdateMatrix(); float matrix[16]; object->matrixModel.FlattenToArray(matrix); Vector3 angles = object->rotation * ToRad; NewtonBody *body = NewtonCreateBody(world, collision, matrix); if (mass > 0) { float inertia[3], origin[3]; NewtonConvexCollisionCalculateInertialMatrix (collision, &inertia[0], &origin[0]); NewtonBodySetMassMatrix(body, mass, mass * inertia[0], mass * inertia[1], mass * inertia[2]); NewtonBodySetCentreOfMass(body, &origin[0]); } NewtonBodySetUserData(body, object); NewtonBodySetTransformCallback(body, CPhysics::TransformCallback); NewtonBodySetDestructorCallback(body, CPhysics::DestroyBodyCallback); NewtonReleaseCollision(world, collision); return body; }
NewtonBody* Body::create(NewtonCollision* collision, float mass, int freezeState, const Vec4f& damping) { Vec4f minBox, maxBox; Vec4f origin, inertia; m_body = NewtonCreateBody(newton::world, collision, this->m_matrix[0]); NewtonBodySetUserData(m_body, this); NewtonBodySetMatrix(m_body, this->m_matrix[0]); NewtonConvexCollisionCalculateInertialMatrix(collision, &inertia[0], &origin[0]); if (mass < 0.0f) mass = NewtonConvexCollisionCalculateVolume(collision) * 0.5f; if (mass != 0.0f) NewtonBodySetMassMatrix(m_body, mass, mass * inertia.x, mass * inertia.y, mass * inertia.z); NewtonBodySetCentreOfMass(m_body, &origin[0]); NewtonBodySetForceAndTorqueCallback(m_body, Body::__applyForceAndTorqueCallback); NewtonBodySetTransformCallback(m_body, Body::__setTransformCallback); NewtonBodySetDestructorCallback(m_body, Body::__destroyBodyCallback); NewtonBodySetFreezeState(m_body, freezeState); NewtonBodySetLinearDamping(m_body, damping.w); NewtonBodySetAngularDamping(m_body, &damping[0]); return m_body; }
NewtonBody* CreateRigidBody(DemoEntityManager* const scene, dFloat mass, NewtonCollision* const deformableCollision) { //create the rigid body NewtonWorld* const world = scene->GetNewton(); dMatrix matrix(GetCurrentMatrix()); //matrix.m_posit.m_y = FindFloor (world, matrix.m_posit.m_x, matrix.m_posit.m_z) + 4.0f; SetMatrix(*scene, dQuaternion(), matrix.m_posit); SetMatrix(*scene, dQuaternion(), matrix.m_posit); NewtonBody* const deformableBody = NewtonCreateDynamicBody(world, deformableCollision, &matrix[0][0]); // set the mass matrix NewtonBodySetMassProperties(deformableBody, mass, deformableCollision); // save the pointer to the graphic object with the body. NewtonBodySetUserData(deformableBody, this); // assign the wood id // NewtonBodySetMaterialGroupID (deformableBody, materialId); // set a destructor for this rigid body NewtonBodySetDestructorCallback(deformableBody, PhysicsBodyDestructor); // set the transform call back function NewtonBodySetTransformCallback(deformableBody, DemoEntity::TransformCallback); // set the force and torque call back function NewtonBodySetForceAndTorqueCallback(deformableBody, PhysicsApplyGravityForce); return deformableBody; }
virtual const void InitRigiBody(const NewtonBody* const body, const char* const bodyName) const { dMatrix matrix; DemoEntityManager* const scene = (DemoEntityManager*)NewtonWorldGetUserData(NewtonBodyGetWorld(body)); NewtonCollision* const collision = NewtonBodyGetCollision(body); DemoMesh* const mesh = new DemoMesh("ragdoll", collision, "smilli.tga", "smilli.tga", "smilli.tga"); NewtonBodyGetMatrix(body, &matrix[0][0]); DemoEntity* const entity = new DemoEntity(matrix, NULL); entity->SetNameID (bodyName); entity->SetMesh(mesh, dGetIdentityMatrix()); scene->Append(entity); mesh->Release(); // save the pointer to the graphic object with the body. NewtonBodySetUserData(body, entity); // assign the wood id NewtonBodySetMaterialGroupID(body, m_material); //set continuous collision mode //NewtonBodySetContinuousCollisionMode (rigidBody, continueCollisionMode); // set a destructor for this rigid body NewtonBodySetDestructorCallback(body, PhysicsBodyDestructor); // set the transform call back function NewtonBodySetTransformCallback(body, DemoEntity::TransformCallback); // set the force and torque call back function NewtonBodySetForceAndTorqueCallback(body, PhysicsApplyGravityForce); }
iPhysicsBody* iPhysics::createBody(iPhysicsCollision* collisionVolume) { con_assert(collisionVolume != nullptr, "zero pointer"); con_assert(collisionVolume->_collision != nullptr, "zero pointer"); iaMatrixf matrix; NewtonWaitForUpdateToFinish(static_cast<const NewtonWorld*>(_defaultWorld)); NewtonBody* newtonBody = NewtonCreateDynamicBody(static_cast<const NewtonWorld*>(_defaultWorld), static_cast<const NewtonCollision*>(collisionVolume->_collision), matrix.getData()); // set callbacks NewtonBodySetDestructorCallback(newtonBody, reinterpret_cast<NewtonBodyDestructor>(PhysicsNodeDestructor)); NewtonBodySetTransformCallback(newtonBody, reinterpret_cast<NewtonSetTransform>(PhysicsNodeSetTransform)); NewtonBodySetForceAndTorqueCallback(newtonBody, reinterpret_cast<NewtonApplyForceAndTorque>(PhysicsApplyForceAndTorque)); NewtonBodySetMassMatrix(newtonBody, 0, 0, 0, 0); NewtonBodySetMatrix(newtonBody, matrix.getData()); start(); iPhysicsBody* result = new iPhysicsBody(newtonBody); _bodyListMutex.lock(); _bodys[result->getID()] = result; _bodyListMutex.unlock(); return result; }
void dNewtonBody::Destroy() { if (m_body) { NewtonWaitForUpdateToFinish(NewtonBodyGetWorld(m_body)); NewtonBodySetDestructorCallback(m_body, NULL); NewtonDestroyBody(m_body); m_body = NULL; } }
static void AddShatterEntity (DemoEntityManager* const scene, DemoMesh* const visualMesh, NewtonCollision* const collision, const ShatterEffect& shatterEffect, dVector location) { dQuaternion rotation; SimpleShatterEffectEntity* const entity = new SimpleShatterEffectEntity (visualMesh, shatterEffect); entity->SetMatrix(*scene, rotation, location); entity->InterpolateMatrix (*scene, 1.0f); scene->Append(entity); dVector origin; dVector inertia; NewtonConvexCollisionCalculateInertialMatrix (collision, &inertia[0], &origin[0]); float mass = 10.0f; int materialId = 0; dFloat Ixx = mass * inertia[0]; dFloat Iyy = mass * inertia[1]; dFloat Izz = mass * inertia[2]; //create the rigid body dMatrix matrix (GetIdentityMatrix()); matrix.m_posit = location; NewtonWorld* const world = scene->GetNewton(); NewtonBody* const rigidBody = NewtonCreateBody (world, collision, &matrix[0][0]); entity->m_myBody = rigidBody; entity->m_myweight = dAbs (mass * DEMO_GRAVITY); // 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, entity); // assign the wood id NewtonBodySetMaterialGroupID (rigidBody, materialId); // set continue collision mode // NewtonBodySetContinuousCollisionMode (rigidBody, continueCollisionMode); // set a destructor for this rigid body NewtonBodySetDestructorCallback (rigidBody, PhysicsBodyDestructor); // set the transform call back function NewtonBodySetTransformCallback (rigidBody, DemoEntity::SetTransformCallback); // set the force and torque call back function NewtonBodySetForceAndTorqueCallback (rigidBody, PhysicsApplyGravityForce); }
Body::~Body() { if (m_body) { if (NewtonBodyGetUserData(m_body)) { NewtonBodySetDestructorCallback( m_body, NULL ); NewtonDestroyBody( m_world->getNewtonWorld(), m_body ); } } }
NewtonBody* CreateSimpleBody (NewtonWorld* const world, void* const userData, dFloat mass, const dMatrix& matrix, NewtonCollision* const collision, int materialId) { // calculate the moment of inertia and the relative center of mass of the solid // dVector origin; // dVector inertia; // NewtonConvexCollisionCalculateInertialMatrix (collision, &inertia[0], &origin[0]); // dFloat Ixx = mass * inertia[0]; // dFloat Iyy = mass * inertia[1]; // dFloat Izz = mass * inertia[2]; //create the rigid body NewtonBody* const rigidBody = NewtonCreateDynamicBody (world, collision, &matrix[0][0]); // set the correct center of gravity for this body (these function are for legacy) // NewtonBodySetCentreOfMass (rigidBody, &origin[0]); // NewtonBodySetMassMatrix (rigidBody, mass, Ixx, Iyy, Izz); // use a more convenient function for setting mass and inertia matrix NewtonBodySetMassProperties (rigidBody, mass, collision); // save the pointer to the graphic object with the body. NewtonBodySetUserData (rigidBody, userData); // assign the wood id NewtonBodySetMaterialGroupID (rigidBody, materialId); // set continuous collision mode // NewtonBodySetContinuousCollisionMode (rigidBody, continueCollisionMode); // set a destructor for this rigid body NewtonBodySetDestructorCallback (rigidBody, PhysicsBodyDestructor); // set the transform call back function NewtonBodySetTransformCallback (rigidBody, DemoEntity::TransformCallback); // 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]); //PhysicsSetTransform (rigidBody, &matrix[0][0], 0); //dVector xxx (0, -9.8f * mass, 0.0f, 0.0f); //NewtonBodySetForce (rigidBody, &xxx[0]); // force the body to be active of inactive // NewtonBodySetAutoSleep (rigidBody, sleepMode); return rigidBody; }
static void AddFracturedEntity(DemoEntityManager* const scene, DemoMesh* const visualMesh, NewtonCollision* const collision, const FractureEffect& fractureEffect, const dVector& location) { dQuaternion rotation; SimpleFracturedEffectEntity* const entity = new SimpleFracturedEffectEntity(visualMesh, fractureEffect); entity->SetMatrix(*scene, rotation, location); entity->InterpolateMatrix(*scene, 1.0f); scene->Append(entity); dVector origin(0.0f); dVector inertia(0.0f); NewtonConvexCollisionCalculateInertialMatrix(collision, &inertia[0], &origin[0]); dFloat mass = 10.0f; int materialId = 0; //create the rigid body dMatrix matrix(dGetIdentityMatrix()); matrix.m_posit = location; NewtonWorld* const world = scene->GetNewton(); NewtonBody* const rigidBody = NewtonCreateDynamicBody(world, collision, &matrix[0][0]); entity->m_myBody = rigidBody; entity->m_myMassInverse = 1.0f / mass; // set the correct center of gravity for this body //NewtonBodySetCentreOfMass (rigidBody, &origin[0]); // set the mass matrix NewtonBodySetMassProperties(rigidBody, mass, collision); // save the pointer to the graphic object with the body. NewtonBodySetUserData(rigidBody, entity); // assign the wood id NewtonBodySetMaterialGroupID(rigidBody, materialId); // set continuous collision mode // NewtonBodySetContinuousCollisionMode (rigidBody, continueCollisionMode); // set a destructor for this rigid body NewtonBodySetDestructorCallback(rigidBody, PhysicsBodyDestructor); // set the transform call back function NewtonBodySetTransformCallback(rigidBody, DemoEntity::TransformCallback); // set the force and torque call back function NewtonBodySetForceAndTorqueCallback(rigidBody, PhysicsApplyGravityForce); }
// destructor callback void _cdecl Body::newtonDestructor( const NewtonBody* body ) { //newton wants to destroy the body.. so first find it. OgreNewt::Body* me; me = (OgreNewt::Body*)NewtonBodyGetUserData( body ); // remove destructor callback NewtonBodySetDestructorCallback( body, NULL ); // remove the user data NewtonBodySetUserData( body, NULL ); //now delete the object. delete me; }
/* ============= CMod_PhysicsAddEntity ============= */ void CMod_PhysicsAddEntity(sharedEntity_t * gEnt) { NewtonCollision* collision = NULL; NewtonBody* body = NULL; std::map<int, bspCmodel>::iterator it = bspModels.find (gEnt->s.modelindex); if ( it == bspModels.end() ) { return; } vec3_t inertia, com; dMatrix matrix (GetIdentityMatrix()); bspCmodel* bmodel = &it->second; collision = NewtonCreateConvexHull (g_world, bmodel->vertices.size(), &bmodel->vertices[0].m_x, sizeof (dVector), 0.0f, &matrix[0][0]); body = NewtonCreateBody (g_world, collision); NewtonConvexCollisionCalculateVolume (collision); NewtonReleaseCollision (g_world, collision); bmodel->rigidBody = body; NewtonBodySetMaterialGroupID (body, defaultMaterialGroup); NewtonBodySetUserData (body, (void*)gEnt); NewtonBodySetDestructorCallback (body, PhysicsEntityDie); NewtonBodySetContinuousCollisionMode (body, 0); NewtonBodySetForceAndTorqueCallback (body, PhysicsEntityThink); NewtonBodySetTransformCallback (body, PhysicsEntitySetTransform); NewtonConvexCollisionCalculateInertialMatrix (collision, &inertia[0], &com[0]); NewtonBodySetCentreOfMass (body, &com[0]); VectorScale (inertia, 10.0f, inertia); // The inertia needs to be scaled by the mass. NewtonBodySetMassMatrix (body, 10.f, inertia[0], inertia[1], inertia[2]); matrix.m_posit.m_x = gEnt->s.origin[0] * UNITS_PER_METRE; matrix.m_posit.m_y = gEnt->s.origin[1] * UNITS_PER_METRE; matrix.m_posit.m_z = gEnt->s.origin[2] * UNITS_PER_METRE; NewtonBodySetMatrix (body, &matrix[0][0]); gEnt->s.pos.trType = TR_INTERPOLATE; VectorCopy (gEnt->s.origin, gEnt->s.pos.trBase); VectorCopy (gEnt->s.origin, gEnt->r.currentOrigin); gEnt->s.apos.trType = TR_INTERPOLATE; VectorCopy (gEnt->s.angles, gEnt->s.apos.trBase); VectorCopy (gEnt->s.angles, gEnt->r.currentAngles); }
Body::Body( World* W, OgreNewt::Collision* col, int bodytype ) { m_world = W; m_collision = col; m_type = bodytype; m_node = NULL; m_matid = NULL; m_userdata = NULL; m_forcecallback = NULL; m_transformcallback = NULL; m_body = NewtonCreateBody( m_world->getNewtonWorld(), col->getNewtonCollision() ); NewtonBodySetUserData( m_body, this ); NewtonBodySetDestructorCallback( m_body, newtonDestructor ); }
void DemoEntityManager::LoadScene (const char* const fileName) { dScene database (GetNewton()); database.Deserialize(fileName); // this will apply all global the scale to the mesh database.FreezeScale(); // this will apply all local scale and transform to the mesh //database.FreezePivot(); // Load the Visual Scene EntityDictionary entDictionary; LoadVisualScene(&database, entDictionary); //Load the physics world dList<NewtonBody*> bodyList; database.SceneToNewtonWorld(m_world, bodyList); // bind every rigidBody loaded to the scene entity for (dList<NewtonBody*>::dListNode* bodyNode = bodyList.GetFirst(); bodyNode; bodyNode = bodyNode->GetNext()) { // find the user data and set to the visual entity in the scene NewtonBody* const body = bodyNode->GetInfo(); dScene::dTreeNode* const sceneNode = (dScene::dTreeNode*)NewtonBodyGetUserData(body); DemoEntity* const entity = entDictionary.Find(sceneNode)->GetInfo(); NewtonBodySetUserData(body, entity); // see if this body have some special setups dScene::dTreeNode* const node = database.FindChildByType(sceneNode, dRigidbodyNodeInfo::GetRttiType()); dAssert (node); dRigidbodyNodeInfo* const bodyData = (dRigidbodyNodeInfo*) database.GetInfoFromNode(node); dVariable* bodyType = bodyData->FindVariable("rigidBodyType"); // set the default call backs if (!bodyType || !strcmp (bodyType->GetString(), "default gravity")) { NewtonBodySetTransformCallback(body, DemoEntity::TransformCallback); NewtonBodySetForceAndTorqueCallback(body, PhysicsApplyGravityForce); NewtonBodySetDestructorCallback (body, PhysicsBodyDestructor); } } // clean up all caches the engine have saved NewtonInvalidateCache (m_world); }
NewtonBody* CreateRigidBody (NewtonWorld* world, Entity* ent, NewtonCollision* collision, dFloat mass) { dVector minBox; dVector maxBox; dVector origin; dVector inertia; NewtonBody* body; // Now with the collision Shape we can crate a rigid body body = NewtonCreateBody (world, collision); // bodies can have a destructor. // this is a function callback that can be used to destroy any local data stored // and that need to be destroyed before the body is destroyed. NewtonBodySetDestructorCallback (body, DestroyBodyCallback); // save the entity as the user data for this body nEWTONbODySetUserData (body, ent); // we need to set physics properties to this body dMatrix matrix (ent->m_curRotation, ent->m_curPosition); NewtonBodySetMatrix (body, &matrix[0][0]); // we need to set the proper center of mass and inertia matrix for this body // the inertia matrix calculated by this function does not include the mass. // therefore it needs to be multiplied by the mass of the body before it is used. NewtonConvexCollisionCalculateInertialMatrix (collision, &inertia[0], &origin[0]); // set the body mass matrix NewtonBodySetMassMatrix (body, mass, mass * inertia.m_x, mass * inertia.m_y, mass * inertia.m_z); // set the body origin NewtonBodySetCentreOfMass (body, &origin[0]); // set the function callback to apply the external forces and torque to the body // the most common force is Gravity NewtonBodySetForceAndTorqueCallback (body, ApplyForceAndTorqueCallback); // set the function callback to set the transformation state of the graphic entity associated with this body // each time the body change position and orientation in the physics world NewtonBodySetTransformCallback (body, SetTransformCallback); return body; }
void Newtonnode::bodyDestructorCallback(NewtonBodyDestructor callback) { NewtonBodySetDestructorCallback(m_pBody,callback); }
void AddRollingBeats (NewtonWorld* nWorld) { dFloat mass; dFloat Ixx; dFloat Iyy; dFloat Izz; NewtonBody* bar; NewtonCollision* collision; dMatrix location (GetIdentityMatrix()); location.m_posit.m_x = 5.0f; location.m_posit.m_y = 2.0f; location.m_posit.m_z = -2.0f; dVector size (10.0f, 0.25f, 0.25f); bar = NULL; // ///////////////////////////////////////////////////////////////////////////////////// // // create a bar and attach it to the world with a hinge with limits // // //////////////////////////////////////////////////////////////////////////////////// { CustomHinge* joint; RenderPrimitive* visualObject; // create the a graphic character (use a visualObject as our body visualObject = new CylinderPrimitive (location, size.m_y, size.m_x); // create a collision primitive to be shared by all links collision = NewtonCreateCylinder (nWorld, size.m_y, size.m_x, NULL); // craete the bar body bar = NewtonCreateBody(nWorld, collision); NewtonReleaseCollision (nWorld, collision); // attach graphic object to the rigid body NewtonBodySetUserData(bar, visualObject); // set a destructor function NewtonBodySetDestructorCallback (bar, PhysicsBodyDestructor); // set the tranform call back function NewtonBodySetTransformCallback (bar, PhysicsSetTransform); // set the force and torque call back funtion NewtonBodySetForceAndTorqueCallback (bar,PhysicsApplyGravityForce); // calculate a acurate momenet of inertia mass = 5.0f; Ixx = 0.7f * mass * (size.m_y * size.m_y + size.m_z * size.m_z) / 12.0f; Iyy = 0.7f * mass * (size.m_x * size.m_x + size.m_z * size.m_z) / 12.0f; Izz = 0.7f * mass * (size.m_x * size.m_x + size.m_y * size.m_y) / 12.0f; // set the mass matrix NewtonBodySetMassMatrix (bar, mass, Ixx, Iyy, Izz); // set the matrix for both the rigid nody and the graphic body NewtonBodySetMatrix (bar, &location[0][0]); PhysicsSetTransform (bar, &location[0][0]); dVector pin (0.0f, 1.0f, 0.0f); dVector pivot (location.m_posit); pivot.m_x -= size.m_x * 0.5f; // connect these two bodies by a ball and sockect joint //joint = NewtonConstraintCreateHinge (nWorld, &pivot.m_x, &pin.m_x, link0, link1); joint = new CustomHinge (pivot, pin, bar, NULL); // no limits //joint->EnableLimits (true); //joint->SetAngleLimis (-30.0f * 3.1416f/180.0f, 30.0f * 3.1416f/180.0f); } { // //////////////////////////////////////////////////////////////////////////////////// // // add a sliding visualObject with limits // NewtonBody* beat; CustomSlider* joint; RenderPrimitive* visualObject; dMatrix beatLocation (location); dVector beatSize (0.5f, 2.0f, 2.0f); beatLocation.m_posit.m_x += size.m_x * 0.25f; // create the a graphic character (use a visualObject as our body visualObject = new BoxPrimitive (beatLocation, beatSize); // create a collision primitive to be shared by all links collision = NewtonCreateBox (nWorld, beatSize.m_x, beatSize.m_y, beatSize.m_z, NULL); beat = NewtonCreateBody(nWorld, collision); NewtonReleaseCollision (nWorld, collision); // attach graphic object to the rigid body NewtonBodySetUserData(beat, visualObject); // set a destyuctor function NewtonBodySetDestructorCallback (beat, PhysicsBodyDestructor); // set the tranform call back function NewtonBodySetTransformCallback (beat, PhysicsSetTransform); // set the force and torque call back funtion NewtonBodySetForceAndTorqueCallback (beat,PhysicsApplyGravityForce); // calculate a acurate momenet of inertia mass = 5.0f; Ixx = 0.7f * mass * (beatSize.m_y * beatSize.m_y + beatSize.m_z * beatSize.m_z) / 12.0f; Iyy = 0.7f * mass * (beatSize.m_x * beatSize.m_x + beatSize.m_z * beatSize.m_z) / 12.0f; Izz = 0.7f * mass * (beatSize.m_x * beatSize.m_x + beatSize.m_y * beatSize.m_y) / 12.0f; // set the mass matrix NewtonBodySetMassMatrix (beat, mass, Ixx, Iyy, Izz); // set the matrix for both the rigid nody and the graphic body NewtonBodySetMatrix (beat, &beatLocation[0][0]); PhysicsSetTransform (beat, &beatLocation[0][0]); // set the pivot relative for the first bar dVector pivot (beatLocation.m_posit); dVector pin (beatLocation.m_front); joint = new CustomSlider (pivot, pin, beat, bar); // claculate the minimum and maximum limit for this joints dFloat minLimits = ((location.m_posit.m_x - beatLocation.m_posit.m_x) - size.m_x * 0.5f); dFloat maxLimits = ((location.m_posit.m_x - beatLocation.m_posit.m_x) + size.m_x * 0.5f); joint->EnableLimits(true); joint->SetLimis (minLimits, maxLimits); } { // //////////////////////////////////////////////////////////////////////////////////// // // add a corkscrew visualObject with limits // // //////////////////////////////////////////////////////////////////////////////////// NewtonBody* beat; CustomCorkScrew* joint; RenderPrimitive* visualObject; dMatrix beatLocation (location); dVector beatSize (0.5f, 1.25f, 1.25f); beatLocation.m_posit.m_x -= size.m_x * 0.25f; // create the a graphic character (use a visualObject as our body //visualObject = new BoxPrimitive (beatLocation, beatSize); visualObject = new ChamferCylinderPrimitive (beatLocation, beatSize.m_y, beatSize.m_x); // create a collision primitive to be shared by all links collision = NewtonCreateChamferCylinder (nWorld, beatSize.m_y, beatSize.m_x, NULL); beat = NewtonCreateBody(nWorld, collision); NewtonReleaseCollision (nWorld, collision); // attach graphic object to the rigid body NewtonBodySetUserData(beat, visualObject); // set a destyuctor function NewtonBodySetDestructorCallback (beat, PhysicsBodyDestructor); // set the tranform call back function NewtonBodySetTransformCallback (beat, PhysicsSetTransform); // set the force and torque call back funtion NewtonBodySetForceAndTorqueCallback (beat,PhysicsApplyGravityForce); // calculate a acurate momenet of inertia mass = 5.0f; Ixx = 0.7f * mass * (beatSize.m_y * beatSize.m_y + beatSize.m_z * beatSize.m_z) / 12.0f; Iyy = 0.7f * mass * (beatSize.m_x * beatSize.m_x + beatSize.m_z * beatSize.m_z) / 12.0f; Izz = 0.7f * mass * (beatSize.m_x * beatSize.m_x + beatSize.m_y * beatSize.m_y) / 12.0f; // set the mass matrix NewtonBodySetMassMatrix (beat, mass, Ixx, Iyy, Izz); // set the matrix for both the rigid nody and the graphic body NewtonBodySetMatrix (beat, &beatLocation[0][0]); PhysicsSetTransform (beat, &beatLocation[0][0]); // set the pivot relative for the first bar dVector pivot (beatLocation.m_posit); dVector pin (beatLocation.m_front); joint = new CustomCorkScrew (pivot, pin, beat, bar); // claculate the minimum and maximum limit for this joints dFloat minLimits = ((location.m_posit.m_x - beatLocation.m_posit.m_x) - size.m_x * 0.5f); dFloat maxLimits = ((location.m_posit.m_x - beatLocation.m_posit.m_x) + size.m_x * 0.5f); joint->EnableLimits(true); joint->SetLimis (minLimits, maxLimits); } { // //////////////////////////////////////////////////////////////////////////////////// // // add a universal joint visualObject with limits // // //////////////////////////////////////////////////////////////////////////////////// NewtonBody* beat; CustomUniversal* joint; RenderPrimitive* visualObject; dMatrix beatLocation (location); dVector beatSize (0.5f, 1.25f, 1.25f); beatLocation.m_posit.m_x -= size.m_x * 0.5f; // create the a graphic character (use a visualObject as our body //visualObject = new BoxPrimitive (beatLocation, beatSize); visualObject = new ChamferCylinderPrimitive (beatLocation, beatSize.m_y, beatSize.m_x); // create a collision primitive to be shared by all links collision = NewtonCreateChamferCylinder (nWorld, beatSize.m_y, beatSize.m_x, NULL); beat = NewtonCreateBody(nWorld, collision); NewtonReleaseCollision (nWorld, collision); // attach graphic object to the rigid body NewtonBodySetUserData(beat, visualObject); // set a destyuctor function NewtonBodySetDestructorCallback (beat, PhysicsBodyDestructor); // set the tranform call back function NewtonBodySetTransformCallback (beat, PhysicsSetTransform); // set the force and torque call back funtion NewtonBodySetForceAndTorqueCallback (beat,PhysicsApplyGravityForce); // calculate a acurate momenet of inertia mass = 5.0f; Ixx = 0.7f * mass * (beatSize.m_y * beatSize.m_y + beatSize.m_z * beatSize.m_z) / 12.0f; Iyy = 0.7f * mass * (beatSize.m_x * beatSize.m_x + beatSize.m_z * beatSize.m_z) / 12.0f; Izz = 0.7f * mass * (beatSize.m_x * beatSize.m_x + beatSize.m_y * beatSize.m_y) / 12.0f; // set the mass matrix NewtonBodySetMassMatrix (beat, mass, Ixx, Iyy, Izz); // set the matrix for both the rigid nody and the graphic body NewtonBodySetMatrix (beat, &beatLocation[0][0]); PhysicsSetTransform (beat, &beatLocation[0][0]); // set the pivot relative for the first bar dVector pivot (beatLocation.m_posit); dVector pin0 (beatLocation.m_front); dVector pin1 (beatLocation.m_up); // tell this joint to destroiy its local private data when destroyed joint = new CustomUniversal (pivot, pin0, pin1, beat, bar); } { // //////////////////////////////////////////////////////////////////////////////////// // // add a universal joint visualObject with limits // // //////////////////////////////////////////////////////////////////////////////////// NewtonBody* beat; CustomUniversal* joint; RenderPrimitive* visualObject; dMatrix beatLocation (location); dVector beatSize (0.5f, 1.25f, 1.25f); beatLocation.m_posit.m_x = size.m_x; // create the a graphic character (use a visualObject as our body //visualObject = new BoxPrimitive (beatLocation, beatSize); visualObject = new ChamferCylinderPrimitive (beatLocation, beatSize.m_y, beatSize.m_x); // create a collision primitive to be shared by all links collision = NewtonCreateChamferCylinder (nWorld, beatSize.m_y, beatSize.m_x, NULL); beat = NewtonCreateBody(nWorld, collision); NewtonReleaseCollision (nWorld, collision); // attach graphic object to the rigid body NewtonBodySetUserData(beat, visualObject); // set a destyuctor function NewtonBodySetDestructorCallback (beat, PhysicsBodyDestructor); // set the tranform call back function NewtonBodySetTransformCallback (beat, PhysicsSetTransform); // set the force and torque call back funtion NewtonBodySetForceAndTorqueCallback (beat,PhysicsApplyGravityForce); // calculate a acurate momenet of inertia mass = 5.0f; Ixx = 0.7f * mass * (beatSize.m_y * beatSize.m_y + beatSize.m_z * beatSize.m_z) / 12.0f; Iyy = 0.7f * mass * (beatSize.m_x * beatSize.m_x + beatSize.m_z * beatSize.m_z) / 12.0f; Izz = 0.7f * mass * (beatSize.m_x * beatSize.m_x + beatSize.m_y * beatSize.m_y) / 12.0f; // set the mass matrix NewtonBodySetMassMatrix (beat, mass, Ixx, Iyy, Izz); // set the matrix for both the rigid nody and the graphic body NewtonBodySetMatrix (beat, &beatLocation[0][0]); PhysicsSetTransform (beat, &beatLocation[0][0]); // set the pivot relative for the first bar dVector pivot (beatLocation.m_posit); dVector pin0 (beatLocation.m_front.Scale(-1.0f)); dVector pin1 (beatLocation.m_up); // tell this joint to destroiy its local private data when destroyed joint = new CustomUniversal (pivot, pin0, pin1, beat, bar); } }
void AddDoubleSwingDoors (NewtonWorld* nWorld) { dFloat mass; dFloat Ixx; dFloat Iyy; dFloat Izz; NewtonBody* link0; NewtonBody* link1; CustomHinge* joint; BoxPrimitive* visualObject; NewtonCollision* collision; dVector size (2.0f, 5.0f, 0.5f); // calculate a acurate momenet of inertia mass = 5.0f; Ixx = 0.7f * mass * (size.m_y * size.m_y + size.m_z * size.m_z) / 12.0f; Iyy = 0.7f * mass * (size.m_x * size.m_x + size.m_z * size.m_z) / 12.0f; Izz = 0.7f * mass * (size.m_x * size.m_x + size.m_y * size.m_y) / 12.0f; // create 100 tack of 10 boxes each dMatrix location (GetIdentityMatrix()); location.m_posit.m_x = -2.0f; location.m_posit.m_y = 3.0f; location.m_posit.m_z = -2.0f; // create a collision primitive to be shared by all links collision = NewtonCreateBox (nWorld, size.m_x, size.m_y, size.m_z, NULL); // make first wing { // create the a graphic character (use a visualObject as our body visualObject = new BoxPrimitive (location, size); //create the rigid body link1 = NewtonCreateBody (nWorld, collision); // Set Material Id for this object NewtonBodySetMaterialGroupID (link1, woodID); // save the pointer to the graphic object with the body. NewtonBodySetUserData (link1, visualObject); // set a destrutor for this rigid body NewtonBodySetDestructorCallback (link1, PhysicsBodyDestructor); // set the tranform call back function NewtonBodySetTransformCallback (link1, PhysicsSetTransform); // set the force and torque call back funtion NewtonBodySetForceAndTorqueCallback (link1,PhysicsApplyGravityForce); // set the mass matrix NewtonBodySetMassMatrix (link1, mass, Ixx, Iyy, Izz); // set the matrix for tboth the rigid nody and the graphic body NewtonBodySetMatrix (link1, &location[0][0]); PhysicsSetTransform (link1, &location[0][0]); dVector pivot (location.m_posit); dVector pin (location.m_up); pivot.m_x += size.m_x * 0.5f; // connect these two bodies by a ball and sockect joint joint = new CustomHinge (pivot, pin, link1, NULL); joint->EnableLimits (true); joint->SetLimis (-30.0f * 3.1416f/180.0f, 30.0f * 3.1416f/180.0f); } // make second wing { location.m_posit.m_x -= size.m_x; // create the a graphic character (use a visualObject as our body visualObject = new BoxPrimitive (location, size); //create the rigid body link0 = NewtonCreateBody (nWorld, collision); // Set Material Id for this object NewtonBodySetMaterialGroupID (link0, woodID); // save the pointer to the graphic object with the body. NewtonBodySetUserData (link0, visualObject); // set a destrutor for this rigid body NewtonBodySetDestructorCallback (link0, PhysicsBodyDestructor); // set the tranform call back function NewtonBodySetTransformCallback (link0, PhysicsSetTransform); // set the force and torque call back funtion NewtonBodySetForceAndTorqueCallback (link0,PhysicsApplyGravityForce); // set the mass matrix NewtonBodySetMassMatrix (link0, mass, Ixx, Iyy, Izz); // set the matrix for tboth the rigid nody and the graphic body NewtonBodySetMatrix (link0, &location[0][0]); PhysicsSetTransform (link0, &location[0][0]); dVector pivot (location.m_posit); dVector pin (location.m_up); pivot.m_x += size.m_x * 0.5f; // connect these two bodies by a ball and sockect joint //joint = NewtonConstraintCreateHinge (nWorld, &pivot.m_x, &pin.m_x, link0, link1); joint = new CustomHinge (pivot, pin, link0, link1); joint->EnableLimits (true); joint->SetLimis (-30.0f * 3.1416f/180.0f, 30.0f * 3.1416f/180.0f); } // release the collision geometry when not need it NewtonReleaseCollision (nWorld, collision); }
// create a rope of boxes void AddRope (NewtonWorld* nWorld) { int i; dFloat mass; dFloat Ixx; dFloat Iyy; dFloat Izz; NewtonBody* link0; NewtonBody* link1; NewtonCustomJoint* joint; NewtonCollision* collision; RenderPrimitive* visualObject; dVector size (2.0f, 0.25f, 0.25f); // calculate a acurate momenet of inertia mass = 2.0f; Ixx = 0.7f * mass * (size.m_y * size.m_y + size.m_z * size.m_z) / 12.0f; Iyy = 0.7f * mass * (size.m_x * size.m_x + size.m_z * size.m_z) / 12.0f; Izz = 0.7f * mass * (size.m_x * size.m_x + size.m_y * size.m_y) / 12.0f; // create 100 tack of 10 boxes each //dMatrix location (GetIdentityMatrix()); dMatrix location (dgRollMatrix(3.1426f * 0.5f)); location.m_posit.m_y = 11.5f; location.m_posit.m_z = -5.0f; // create a collision primitive to be shared by all links collision = NewtonCreateCapsule (nWorld, size.m_y, size.m_x, NULL); link0 = NULL; // create a lon vertical rope with limits for (i = 0; i < 7; i ++) { // create the a graphic character (use a visualObject as our body visualObject = new CapsulePrimitive (location, size.m_y, size.m_x); //create the rigid body link1 = NewtonCreateBody (nWorld, collision); // add some damping to each link NewtonBodySetLinearDamping (link1, 0.2f); dVector angularDamp (0.2f, 0.2f, 0.2f); NewtonBodySetAngularDamping (link1, &angularDamp.m_x); // Set Material Id for this object NewtonBodySetMaterialGroupID (link1, woodID); // save the pointer to the graphic object with the body. NewtonBodySetUserData (link1, visualObject); // set a destrutor for this rigid body NewtonBodySetDestructorCallback (link1, PhysicsBodyDestructor); // set the tranform call back function NewtonBodySetTransformCallback (link1, PhysicsSetTransform); // set the force and torque call back funtion NewtonBodySetForceAndTorqueCallback (link1,PhysicsApplyGravityForce); // set the mass matrix NewtonBodySetMassMatrix (link1, mass, Ixx, Iyy, Izz); // set the matrix for tboth the rigid nody and the graphic body NewtonBodySetMatrix (link1, &location[0][0]); PhysicsSetTransform (link1, &location[0][0]); dVector pivot (location.m_posit); pivot.m_y += (size.m_x - size.m_y) * 0.5f; dFloat coneAngle = 2.0 * 3.1416f / 180.0f; dFloat twistAngle = 2.0 * 3.1416f / 180.0f; dVector pin (location.m_front.Scale (-1.0f)); joint = new CustomConeLimitedBallAndSocket(twistAngle, coneAngle, pin, pivot, link1, link0); link0 = link1; location.m_posit.m_y -= (size.m_x - size.m_y); } // vrete a short horizontal rope with limits location = GetIdentityMatrix(); location.m_posit.m_y = 2.5f; location.m_posit.m_z = -7.0f; link0 = NULL; for (i = 0; i < 3; i ++) { // create the a graphic character (use a visualObject as our body visualObject = new CapsulePrimitive (location, size.m_y, size.m_x); //create the rigid body link1 = NewtonCreateBody (nWorld, collision); // add some damping to each link NewtonBodySetLinearDamping (link1, 0.2f); dVector angularDamp (0.2f, 0.2f, 0.2f); NewtonBodySetAngularDamping (link1, &angularDamp.m_x); // Set Material Id for this object NewtonBodySetMaterialGroupID (link1, woodID); // save the pointer to the graphic object with the body. NewtonBodySetUserData (link1, visualObject); // make sure it is active NewtonWorldUnfreezeBody (nWorld, link1); //NewtonBodySetAutoFreeze (link1, 0); // set a destrutor for this rigid body NewtonBodySetDestructorCallback (link1, PhysicsBodyDestructor); // set the tranform call back function NewtonBodySetTransformCallback (link1, PhysicsSetTransform); // set the force and torque call back funtion NewtonBodySetForceAndTorqueCallback (link1,PhysicsApplyGravityForce); // set the mass matrix NewtonBodySetMassMatrix (link1, mass, Ixx, Iyy, Izz); // set the matrix for tboth the rigid nody and the graphic body NewtonBodySetMatrix (link1, &location[0][0]); PhysicsSetTransform (link1, &location[0][0]); dVector pivot (location.m_posit); pivot.m_x += (size.m_x - size.m_y) * 0.5f; dFloat coneAngle = 10.0 * 3.1416f / 180.0f; dFloat twistAngle = 10.0 * 3.1416f / 180.0f; dVector pin (location.m_front.Scale (-1.0f)); joint = new CustomConeLimitedBallAndSocket(twistAngle, coneAngle, pin, pivot, link1, link0); link0 = link1; location.m_posit.m_x -= (size.m_x - size.m_y); } // release the collision geometry when not need it NewtonReleaseCollision (nWorld, collision); }
// create physics scene void InitScene() { BoxPrimitive* box; BoxPrimitive* floor; NewtonBody* boxBody; NewtonBody* floorBody; NewtonCollision* collision; // create the newton world nWorld = NewtonCreate (PhysicsAlloc, PhysicsFree); // set the linear solver model for faster speed NewtonSetSolverModel (nWorld, 8); // set the adpative friction model for faster speed NewtonSetFrictionModel (nWorld, 1); // Set the termination function atexit(CleanUp); // create the the floor graphic objects dVector size (100.0f, 2.0f, 100.0f); dMatrix location (GetIdentityMatrix()); location.m_posit.m_y = -5.0f; // create a box for floor floor = new BoxPrimitive (location, size, g_floorTexture); // create the the floor collision, and body with default values collision = NewtonCreateBox (nWorld, size.m_x, size.m_y, size.m_z, NULL); floorBody = NewtonCreateBody (nWorld, collision); NewtonReleaseCollision (nWorld, collision); // set the transformation for this rigid body NewtonBodySetMatrix (floorBody, &location[0][0]); // save the pointer to the graphic object with the body. NewtonBodySetUserData (floorBody, floor); // set a destrutor for this rigid body NewtonBodySetDestructorCallback (floorBody, PhysicsBodyDestructor); // set the initial size size = dVector(0.5f, 0.5f, 0.5f); // create the collision collision = NewtonCreateBox (nWorld, size.m_x, size.m_y, size.m_z, NULL); // create 100 stacks of 10 boxes each location.m_posit.m_x = -10.0f; for (int k = 0; k < 10; k ++) { location.m_posit.m_z = 0.0f; for (int j = 0; j < 10; j ++) { location.m_posit.m_y = 2.0f; for (int i = 0; i < 10; i ++) { // create a graphic box box = new BoxPrimitive (location, size); //create the rigid body boxBody = NewtonCreateBody (nWorld, collision); // save the pointer to the graphic object with the body. NewtonBodySetUserData (boxBody, box); // set a destrutor for this rigid body NewtonBodySetDestructorCallback (boxBody, PhysicsBodyDestructor); // set the tranform call back function NewtonBodySetTransformCallback (boxBody, PhysicsSetTransform); // set the force and torque call back funtion NewtonBodySetForceAndTorqueCallback (boxBody, PhysicsApplyForceAndTorque); // set the mass matrix //NewtonBodySetMassMatrix (boxBody, 1.0f, 1.0f / 6.0f, 1.0f / 6.0f, 1.0f / 6.0f); NewtonBodySetMassMatrix (boxBody, 1.0f, 1.0f, 1.0f, 1.0f); // set the matrix for tboth the rigid nody and the graphic body NewtonBodySetMatrix (boxBody, &location[0][0]); PhysicsSetTransform (boxBody, &location[0][0]); location.m_posit.m_y += size.m_y * 2.0f; } location.m_posit.m_z -= size.m_z * 4.0f; } location.m_posit.m_x += size.m_x * 4.0f; } // release the collsion geometry when not need it NewtonReleaseCollision (nWorld, collision); }
void SimulationLister(DemoEntityManager* const scene, DemoEntityManager::dListNode* const mynode, dFloat timeStep) { m_delay --; if (m_delay > 0) { return; } // see if the net force on the body comes fr a high impact collision dFloat maxInternalForce = 0.0f; for (NewtonJoint* joint = NewtonBodyGetFirstContactJoint(m_myBody); joint; joint = NewtonBodyGetNextContactJoint(m_myBody, joint)) { for (void* contact = NewtonContactJointGetFirstContact (joint); contact; contact = NewtonContactJointGetNextContact (joint, contact)) { //dVector point; //dVector normal; dVector contactForce; NewtonMaterial* const material = NewtonContactGetMaterial (contact); //NewtonMaterialGetContactPositionAndNormal (material, &point.m_x, &normal.m_x); NewtonMaterialGetContactForce(material, m_myBody, &contactForce[0]); dFloat forceMag = contactForce % contactForce; if (forceMag > maxInternalForce) { maxInternalForce = forceMag; } } } // if the force is bigger than 4 Gravities, It is considered a collision force dFloat maxForce = BREAK_FORCE_IN_GRAVITIES * m_myweight; if (maxInternalForce > (maxForce * maxForce)) { NewtonWorld* const world = NewtonBodyGetWorld(m_myBody); dFloat Ixx; dFloat Iyy; dFloat Izz; dFloat mass; NewtonBodyGetMassMatrix(m_myBody, &mass, &Ixx, &Iyy, &Izz); dVector com; dVector veloc; dVector omega; dMatrix bodyMatrix; NewtonBodyGetVelocity(m_myBody, &veloc[0]); NewtonBodyGetOmega(m_myBody, &omega[0]); NewtonBodyGetCentreOfMass(m_myBody, &com[0]); NewtonBodyGetMatrix(m_myBody, &bodyMatrix[0][0]); com = bodyMatrix.TransformVector (com); dMatrix matrix (GetCurrentMatrix()); dQuaternion rotation (matrix); for (ShatterEffect::dListNode* node = m_effect.GetFirst(); node; node = node->GetNext()) { ShatterAtom& atom = node->GetInfo(); DemoEntity* const entity = new DemoEntity (NULL); entity->SetMesh (atom.m_mesh); entity->SetMatrix(*scene, rotation, matrix.m_posit); entity->InterpolateMatrix (*scene, 1.0f); scene->Append(entity); int materialId = 0; dFloat debriMass = mass * atom.m_massFraction; dFloat Ixx = debriMass * atom.m_momentOfInirtia.m_x; dFloat Iyy = debriMass * atom.m_momentOfInirtia.m_y; dFloat Izz = debriMass * atom.m_momentOfInirtia.m_z; //create the rigid body NewtonBody* const rigidBody = NewtonCreateBody (world, atom.m_collision, &matrix[0][0]); // set the correct center of gravity for this body NewtonBodySetCentreOfMass (rigidBody, &atom.m_centerOfMass[0]); // calculate the center of mas of the debris dVector center (matrix.TransformVector(atom.m_centerOfMass)); // calculate debris initial velocity dVector v (veloc + omega * (center - com)); // set initial velocity NewtonBodySetVelocity(rigidBody, &v[0]); NewtonBodySetOmega(rigidBody, &omega[0]); // set the debrie center of mass NewtonBodySetCentreOfMass (rigidBody, &atom.m_centerOfMass[0]); // set the mass matrix NewtonBodySetMassMatrix (rigidBody, debriMass, Ixx, Iyy, Izz); // activate // NewtonBodyCoriolisForcesMode (blockBoxBody, 1); // save the pointer to the graphic object with the body. NewtonBodySetUserData (rigidBody, entity); // assign the wood id NewtonBodySetMaterialGroupID (rigidBody, materialId); // set continue collision mode // NewtonBodySetContinuousCollisionMode (rigidBody, continueCollisionMode); // set a destructor for this rigid body NewtonBodySetDestructorCallback (rigidBody, PhysicsBodyDestructor); // set the transform call back function NewtonBodySetTransformCallback (rigidBody, DemoEntity::SetTransformCallback); // set the force and torque call back function NewtonBodySetForceAndTorqueCallback (rigidBody, PhysicsApplyGravityForce); } NewtonDestroyBody(world, m_myBody); scene->RemoveEntity (mynode); } };
static void CreateDebriPiece (const NewtonBody* sourceBody, NewtonMesh* mesh, dFloat volume) { dFloat Ixx; dFloat Iyy; dFloat Izz; dFloat mass; dFloat shapeVolume; NewtonWorld* world; NewtonBody* rigidBody; NewtonCollision* collision; OGLMesh* meshInstance; SceneManager* system; RenderPrimitive* primitive; dVector inertia; dVector origin; dVector veloc; dVector omega; dMatrix matrix; world = NewtonBodyGetWorld (sourceBody); NewtonBodyGetMatrix (sourceBody, &matrix[0][0]); NewtonBodyGetMassMatrix (sourceBody, &mass, &Ixx, &Iyy, &Izz); // make a visual object meshInstance = new OGLMesh(); meshInstance->BuildFromMesh (mesh); // create a visual geometry primitive = new RenderPrimitive (matrix, meshInstance); meshInstance->Release(); // save the graphics system system = (SceneManager*) NewtonWorldGetUserData(world); system->AddModel (primitive); collision = NewtonCreateConvexHullFromMesh (world, mesh, 0.1f, DEBRI_ID); // calculate the moment of inertia and the relative center of mass of the solid shapeVolume = NewtonConvexCollisionCalculateVolume (collision); NewtonConvexCollisionCalculateInertialMatrix (collision, &inertia[0], &origin[0]); mass = mass * shapeVolume / volume; Ixx = mass * inertia[0]; Iyy = mass * inertia[1]; Izz = mass * inertia[2]; //create the rigid body rigidBody = NewtonCreateBody (world, collision); // set the correct center of gravity for this body NewtonBodySetCentreOfMass (rigidBody, &origin[0]); // set the mass matrix NewtonBodySetMassMatrix (rigidBody, mass, Ixx, Iyy, Izz); // save the pointer to the graphic object with the body. NewtonBodySetUserData (rigidBody, primitive); // assign the wood id // NewtonBodySetMaterialGroupID (rigidBody, NewtonBodyGetMaterialGroupID(source)); // 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]); PhysicsSetTransform (rigidBody, &matrix[0][0], 0); NewtonBodyGetVelocity(sourceBody, &veloc[0]); NewtonBodyGetOmega(sourceBody, &omega[0]); veloc += omega * matrix.RotateVector(origin); // for now so that I can see the body veloc = dVector (0, 0, 0, 0); // omega = dVector (0, 0, 0, 0); NewtonBodySetVelocity(rigidBody, &veloc[0]); NewtonBodySetOmega(rigidBody, &omega[0]); NewtonReleaseCollision(world, collision); }
static void BuildFloorAndSceneRoot (SceneManager& system) { NewtonWorld* world; RenderPrimitive* floor; NewtonBody* floorBody; NewtonCollision* floorCollision; OGLMesh* meshInstance; world = system.m_world; // ///////////////////////////////////////////////////////////////////// // // create the sky box, system.AddModel (new SkyBox ()); // create the the floor graphic objects dVector floorSize (100.0f, 2.0f, 100.0f); dMatrix location (GetIdentityMatrix()); location.m_posit.m_y = -5.0f; // create a box for floor floorCollision = NewtonCreateBox (world, floorSize.m_x, floorSize.m_y, floorSize.m_z, 0, NULL); // meshInstance = OGLMesh::MakeBox (world, size.m_x, size.m_y, size.m_z, "GrassAndDirt.tga"); meshInstance = new OGLMesh (floorCollision, "GrassAndDirt.tga", "metal_30.tga", "metal_30.tga"); floor = new RenderPrimitive (location, meshInstance); system.AddModel (floor); meshInstance->Release(); // create the the floor collision, and body with default values floorBody = NewtonCreateBody (world, floorCollision); NewtonReleaseCollision (world, floorCollision); // set the transformation for this rigid body NewtonBodySetMatrix (floorBody, &location[0][0]); // save the pointer to the graphic object with the body. NewtonBodySetUserData (floorBody, floor); // set a destructor for this rigid body NewtonBodySetDestructorCallback (floorBody, PhysicsBodyDestructor); // get the default material ID int defaultID; defaultID = NewtonMaterialGetDefaultGroupID (world); // set default material properties NewtonMaterialSetDefaultSoftness (world, defaultID, defaultID, 0.05f); NewtonMaterialSetDefaultElasticity (world, defaultID, defaultID, 0.4f); NewtonMaterialSetDefaultCollidable (world, defaultID, defaultID, 1); NewtonMaterialSetDefaultFriction (world, defaultID, defaultID, 1.0f, 0.5f); NewtonMaterialSetCollisionCallback (world, defaultID, defaultID, NULL, NULL, GenericContactProcess); // NewtonMaterialSetSurfaceThickness(world, materialID, materialID, 0.1f); NewtonMaterialSetSurfaceThickness(world, defaultID, defaultID, 0.0f); // set the island update callback NewtonSetIslandUpdateEvent (world, PhysicsIslandUpdate); // save the callback SetDemoCallbacks (system); InitEyePoint (dVector (1.0f, 0.0f, 0.0f), dVector (-40.0f, 10.0f, 0.0f)); }
void SimulationPostListener(DemoEntityManager* const scene, DemoEntityManager::dListNode* const mynode, dFloat timeStep) { // see if the net force on the body comes fr a high impact collision dFloat breakImpact = 0.0f; for (NewtonJoint* joint = NewtonBodyGetFirstContactJoint(m_myBody); joint; joint = NewtonBodyGetNextContactJoint(m_myBody, joint)) { for (void* contact = NewtonContactJointGetFirstContact(joint); contact; contact = NewtonContactJointGetNextContact(joint, contact)) { dVector contactForce; NewtonMaterial* const material = NewtonContactGetMaterial(contact); dFloat impulseImpact = NewtonMaterialGetContactMaxNormalImpact(material); if (impulseImpact > breakImpact) { breakImpact = impulseImpact; } } } // if the force is bigger than N time Gravities, It is considered a collision force breakImpact *= m_myMassInverse; // breakImpact = 1000.0f; if (breakImpact > BREAK_IMPACT_IN_METERS_PER_SECONDS) { NewtonWorld* const world = NewtonBodyGetWorld(m_myBody); dMatrix bodyMatrix; dVector com(0.0f); dVector veloc(0.0f); dVector omega(0.0f); dFloat Ixx; dFloat Iyy; dFloat Izz; dFloat mass; NewtonBodyGetVelocity(m_myBody, &veloc[0]); NewtonBodyGetOmega(m_myBody, &omega[0]); NewtonBodyGetCentreOfMass(m_myBody, &com[0]); NewtonBodyGetMatrix(m_myBody, &bodyMatrix[0][0]); NewtonBodyGetMass(m_myBody, &mass, &Ixx, &Iyy, &Izz); com = bodyMatrix.TransformVector(com); dMatrix matrix(GetCurrentMatrix()); dQuaternion rotation(matrix); // we need to lock the world before creation a bunch of bodies scene->Lock(m_lock); for (FractureEffect::dListNode* node = m_effect.GetFirst(); node; node = node->GetNext()) { FractureAtom& atom = node->GetInfo(); DemoEntity* const entity = new DemoEntity(dMatrix(rotation, matrix.m_posit), NULL); entity->SetMesh(atom.m_mesh, dGetIdentityMatrix()); scene->Append(entity); int materialId = 0; dFloat debriMass = mass * atom.m_massFraction; //create the rigid body NewtonBody* const rigidBody = NewtonCreateDynamicBody(world, atom.m_collision, &matrix[0][0]); // calculate debris initial velocity dVector center(matrix.TransformVector(atom.m_centerOfMass)); dVector v(veloc + omega.CrossProduct(center - com)); // set initial velocity NewtonBodySetVelocity(rigidBody, &v[0]); NewtonBodySetOmega(rigidBody, &omega[0]); // set the debris mass properties, mass, center of mass, and inertia NewtonBodySetMassProperties(rigidBody, debriMass, atom.m_collision); // save the pointer to the graphic object with the body. NewtonBodySetUserData(rigidBody, entity); // assign the wood id NewtonBodySetMaterialGroupID(rigidBody, materialId); // set continuous collision mode // NewtonBodySetContinuousCollisionMode (rigidBody, continueCollisionMode); // set a destructor for this rigid body NewtonBodySetDestructorCallback(rigidBody, PhysicsBodyDestructor); // set the transform call back function NewtonBodySetTransformCallback(rigidBody, DemoEntity::TransformCallback); // set the force and torque call back function NewtonBodySetForceAndTorqueCallback(rigidBody, PhysicsApplyGravityForce); } NewtonDestroyBody(m_myBody); scene->RemoveEntity(mynode); // unlock the work after done with the effect scene->Unlock(m_lock); } }