void SetAutoSleepMode (NewtonWorld* const world, int mode) { mode = mode ? 0 : 1; for (const NewtonBody* body = NewtonWorldGetFirstBody (world); body; body = NewtonWorldGetNextBody (world, body)) { NewtonBodySetAutoSleep (body, mode); } }
PuckEntity (DemoEntityManager* const scene, int materialID) :DemoEntity (dGetIdentityMatrix(), NULL) ,m_launched(false) { scene->Append(this); NewtonWorld* const world = scene->GetNewton(); dVector puckSize(WEIGHT_DIAMETER, WEIGHT_HEIGHT, 0.0f, 0.0f); // create the shape and visual mesh as a common data to be re used NewtonCollision* const collision = CreateConvexCollision (world, dGetIdentityMatrix(), puckSize, _CYLINDER_PRIMITIVE, materialID); // correction: make the puck an upright cylinder, this makes everything simpler dMatrix collisionAligmentMatrix (dRollMatrix(3.141592f/2.0f)); NewtonCollisionSetMatrix(collision, &collisionAligmentMatrix[0][0]); DemoMesh* const geometry = new DemoMesh("cylinder_1", collision, "smilli.tga", "smilli.tga", "smilli.tga"); //dMatrix matrix = dRollMatrix(3.141592f/2.0f); dMatrix matrix (dGetIdentityMatrix()); matrix.m_posit.m_x = -TABLE_LENGTH*0.5f+WEIGHT_DIAMETER; matrix.m_posit.m_z = -11.8f; //matrix.m_posit.m_z += 4.0f; matrix.m_posit.m_y = 5.0f; m_puckBody = CreateSimpleSolid (scene, geometry, WEIGHT_MASS, matrix, collision, materialID); // Set moment of inertia // correction: this is deprecated, NewtonBodySetMassProperties produce the exact result //dVector I; //dFloat Mass = WEIGHT_MASS; //dFloat Radius = WEIGHT_RADIUS; //dFloat Height = WEIGHT_HEIGHT; //I.m_x = I.m_z = Mass*(3.0f*Radius*Radius+Height*Height)/12.0f; //I.m_y = Mass*Radius*Radius/2.0f; //NewtonBodySetMassMatrix(gPuckBody,Mass, I.m_x, I.m_y, I.m_z); NewtonBodySetMassProperties(m_puckBody, WEIGHT_MASS, NewtonBodyGetCollision(m_puckBody)); NewtonBodySetMaterialGroupID(m_puckBody, materialID); // remember to make continuous collision work with auto sleep mode, right now this is no working NewtonBodySetContinuousCollisionMode(m_puckBody, 1); NewtonBodySetAutoSleep(m_puckBody, 1); // Set callbacks NewtonBodySetForceAndTorqueCallback(m_puckBody, NewtonRigidBodySetForceCB); // do not forget to release the assets geometry->Release(); NewtonDestroyCollision (collision); }
CustomKinematicController::CustomKinematicController(NewtonBody* const body, const dVector& handleInGlobalSpace) :CustomJoint(6, body, NULL) { dMatrix matrix; // get the initial position and orientation NewtonBodyGetMatrix (body, &matrix[0][0]); m_autoSlepState = NewtonBodyGetSleepState (body); NewtonBodySetAutoSleep (body, 0); m_localHandle = matrix.UntransformVector (handleInGlobalSpace); matrix.m_posit = handleInGlobalSpace; SetPickMode (1); SetTargetMatrix (matrix); SetMaxLinearFriction(1.0f); SetMaxAngularFriction(1.0f); }
// create physics scene void PrimitiveCollision (DemoEntityManager* const scene) { // load the skybox scene->CreateSkyBox(); // customize the scene after loading // set a user friction variable in the body for variable friction demos // later this will be done using LUA script NewtonWorld* const world = scene->GetNewton(); dMatrix offsetMatrix (dGetIdentityMatrix()); int materialID = NewtonMaterialGetDefaultGroupID (world); // disable collision NewtonMaterialSetDefaultCollidable (world, materialID, materialID, 0); AddSinglePrimitive (scene, -10.0f, _SPHERE_PRIMITIVE, materialID); AddSinglePrimitive (scene, -8.0f, _BOX_PRIMITIVE, materialID); AddSinglePrimitive (scene, -6.0f, _CAPSULE_PRIMITIVE, materialID); AddSinglePrimitive (scene, -4.0f, _CYLINDER_PRIMITIVE, materialID); AddSinglePrimitive (scene, -2.0f, _CONE_PRIMITIVE, materialID); AddSinglePrimitive (scene, 4.0f, _CHAMFER_CYLINDER_PRIMITIVE, materialID); AddSinglePrimitive (scene, 6.0f, _RANDOM_CONVEX_HULL_PRIMITIVE, materialID); AddSinglePrimitive (scene, 8.0f, _REGULAR_CONVEX_HULL_PRIMITIVE, materialID); // here we will change the standard gravity force callback and apply null and add a for (NewtonBody* body = NewtonWorldGetFirstBody(world); body; body = NewtonWorldGetNextBody(world, body)) { DemoEntity* const entity = (DemoEntity*) NewtonBodyGetUserData(body); entity->SetUserData (new ShowCollisionCollide(body)); NewtonBodySetForceAndTorqueCallback(body, PhysicsSpinBody); NewtonBodySetAutoSleep (body, 0); } // place camera into position //dMatrix camMatrix (dYawMatrix(90.0f * dDegreeToRad)); dMatrix camMatrix (dGetIdentityMatrix()); dQuaternion rot (camMatrix); dVector origin (-15.0f, 0.0f, 0.0f, 0.0f); scene->SetCameraMatrix(rot, origin); }
void makeFence(Level* level, const NewtonWorld* newtonWorld) { return; CollisionSet fencePartsCollisions; fencePartsCollisions.insert(level->getCollision("fence")); //fencePartsCollisions.insert(level->getCollision("fenceClip1")); //fencePartsCollisions.insert(level->getCollision("fenceClip2")); fencePartsCollisions.insert(level->getCollision("fenceTop")); Collision* heightMap = level->getCollision("level"); for (size_t fencesVectorIdx = 0; fencesVectorIdx < level->m_fences.size(); fencesVectorIdx++) { const vector<Vector>& fence = level->m_fences[fencesVectorIdx]; for (size_t i = 0; i < fence.size() - 1; i++) { const Vector startPoint = fence[i]; const Vector endPoint = fence[i + 1]; Vector delta(endPoint - startPoint); const Vector rotation(0, - delta.getRotationY(), 0); float howMany = delta.magnitude() / fenceSpacing; delta /= howMany; for (int j = 0; j < howMany; j++) { const string bodyID = "fence" + cast<string>(fencesVectorIdx) + "_" + cast<string>(i) + "_" + cast<string>(j); Body* body = new Body(bodyID, level, fencePartsCollisions); body->m_soundable = true; Vector position = Vector(startPoint + delta * static_cast<float>(j)); position.y = fenceHeight / 2 + heightMap->getHeight(position.x, position.z) + 0.05f; body->setTransform(position, rotation); NewtonBodySetAutoSleep(body->m_newtonBody, 1); NewtonBodySetFreezeState(body->m_newtonBody, 1); //NewtonBodySetMassMatrix(body->m_newtonBody, 0, 0, 0, 0); level->m_bodies[bodyID] = body; } } } }
NewtonBody* AddBox(CScene *pScene, NewtonWorld *pWorld, Vector3 pos, Vector3 size, Vector3 rot, float mass) { static map<string, CGeometry*> geometries; static CMaterial *material = NULL; if (!material) { material = new CMaterial(); //material->features = EShaderFeature::LIGHT | EShaderFeature::FOG;// | EShaderFeature::SHADOW; //material->transparent = true; } string name = str("%f_%f_%f", size.x, size.y, size.z); if (geometries.find(name) == geometries.end()) { CGeometry *g = new CCubeGeometry(size.x, size.y, size.z); g->materials.AddToTail(material); geometries[name] = g; } CMesh *box = new CMesh( geometries[name] ); box->SetPosition(pos); box->SetRotation(rot); //box->matrixModel.SetEulerAngles(box->rotation); NewtonBody *body = CPhysics::CreateBox(pWorld, box, size.x, size.y, size.z, mass); box->color = SRGB(clamp(pos.x*255.0f/32.0f,0,255)*0.9f, clamp(pos.y*255.0f/32.0f,0,255)*0.9f, clamp(pos.z*255.0f/32.0f,0,255)*0.9f); //NewtonBodySetMaterialGroupID(body, gLevelBlocksMaterialID); // ENABLED COLLISION pScene->Add(box); NewtonBodySetForceAndTorqueCallback(body, BoxGravityCallback); NewtonBodySetAutoSleep(body, 1); // make boxes go to sleep automatically (default) return body; }
void NzPhysObject::EnableAutoSleep(bool autoSleep) { NewtonBodySetAutoSleep(m_body, autoSleep); }
CustomKinematicController::~CustomKinematicController() { NewtonBodySetAutoSleep (m_body0, m_autoSlepState); }
void cPhysicsBodyNewton::SetAutoDisable(bool a_bEnabled) { NewtonBodySetAutoSleep(m_pNewtonBody, a_bEnabled); }
void cPhysicsBodyNewton::SetAutoDisable(bool abEnabled) { NewtonBodySetAutoSleep(mpNewtonBody, abEnabled ? 1 : 0); }
CustomPickBody::~CustomPickBody() { NewtonBodySetAutoSleep (m_body0, m_autoSlepState); }
void PrecessingTops (DemoEntityManager* const scene) { scene->CreateSkyBox(); // customize the scene after loading // set a user friction variable in the body for variable friction demos // later this will be done using LUA script dMatrix offsetMatrix (dGetIdentityMatrix()); CreateLevelMesh (scene, "flatPlane.ngd", 1); dVector location (0.0f, 0.0f, 0.0f, 0.0f); dVector size (3.0f, 2.0f, 0.0f, 0.0f); // create an array of cones const int count = 10; // all shapes use the x axis as the axis of symmetry, to make an upright cone we apply a 90 degree rotation local matrix dMatrix shapeOffsetMatrix (dRollMatrix(-3.141592f/2.0f)); AddPrimitiveArray(scene, 50.0f, location, size, count, count, 5.0f, _CONE_PRIMITIVE, 0, shapeOffsetMatrix); // till the cont 30 degrees, and apply a local high angular velocity dMatrix matrix (dRollMatrix (-25.0f * 3.141592f / 180.0f)); dVector omega (0.0f, 50.0f, 0.0f); omega = matrix.RotateVector (omega); dVector damp (0.0f, 0.0f, 0.0f, 0.0f); int topscount = 0; NewtonBody* array[count * count]; NewtonWorld* const world = scene->GetNewton(); for (NewtonBody* body = NewtonWorldGetFirstBody(world); body; body = NewtonWorldGetNextBody(world, body)) { NewtonCollision* const collision = NewtonBodyGetCollision(body); dVector com; if (NewtonCollisionGetType (collision) == SERIALIZE_ID_CONE) { array[topscount] = body; topscount ++; } } for (int i = 0; i < topscount ; i ++) { dMatrix bodyMatrix; NewtonBody* const body = array[i]; NewtonBodyGetMatrix(body, &bodyMatrix[0][0]); matrix.m_posit = bodyMatrix.m_posit; matrix.m_posit.m_y += 1.0f; NewtonBodySetMatrix(body, &matrix[0][0]); dFloat Ixx; dFloat Iyy; dFloat Izz; dFloat mass; NewtonBodyGetMassMatrix(body, &mass, &Ixx, &Iyy, &Izz); NewtonBodySetMassMatrix(body, mass, Ixx, Iyy * 8.0f, Izz); NewtonBodySetOmega (body, &omega[0]); NewtonBodySetAutoSleep (body, 0); NewtonBodySetLinearDamping(body, 0.0f); NewtonBodySetAngularDamping (body, &damp[0]); } // place camera into position dMatrix camMatrix (dGetIdentityMatrix()); dQuaternion rot (camMatrix); dVector origin (-40.0f, 5.0f, 0.0f, 0.0f); scene->SetCameraMatrix(rot, origin); }
void CreateScene (NewtonWorld* world, SceneManager* sceneManager) { Entity* floor; NewtonBody* floorBody; NewtonCollision* shape; /* void* materialManager; SoundManager* sndManager; PhysicsMaterialInteration matInterations; sndManager = sceneManager->GetSoundManager(); // Create the material for this scene, and attach it to the Newton World materialManager = CreateMaterialManager (world, sndManager); // add the Material table matInterations.m_restitution = 0.6f; matInterations.m_staticFriction = 0.6f; matInterations.m_kineticFriction = 0.3f; matInterations.m_scrapingSound = NULL; matInterations.m_impactSound = sndManager->LoadSound ("metalMetal.wav"); AddMaterilInteraction (materialManager, m_metal, m_metal, &matInterations); matInterations.m_impactSound = sndManager->LoadSound ("boxBox.wav"); AddMaterilInteraction (materialManager, m_wood, m_wood, &matInterations); matInterations.m_impactSound = sndManager->LoadSound ("metalBox.wav"); AddMaterilInteraction (materialManager, m_metal, m_wood, &matInterations); matInterations.m_impactSound = sndManager->LoadSound ("grass0.wav"); AddMaterilInteraction (materialManager, m_wood, m_grass, &matInterations); matInterations.m_impactSound = sndManager->LoadSound ("boxHit.wav"); AddMaterilInteraction (materialManager, m_wood, m_bricks, &matInterations); matInterations.m_impactSound = sndManager->LoadSound ("grass1.wav"); AddMaterilInteraction (materialManager, m_metal, m_grass, &matInterations); matInterations.m_impactSound = sndManager->LoadSound ("metal.wav"); AddMaterilInteraction (materialManager, m_metal, m_bricks, &matInterations); */ // Create a large body to be the floor floor = sceneManager->CreateEntity(); int materialMap[] = {m_bricks, m_grass, m_wood, m_metal}; #ifdef USE_HEIGHT_FIELD_LEVEL // add scene collision from a level m*esh shape = CreateHeightFieldCollision (world, "h2.raw", materialMap); floorBody = CreateRigidBody (world, floor, shape, 0.0f); NewtonDestroyCollision(shape); // make a visual mesh for the collision data CreateHeightFieldMesh (shape, floor); // set the matrix at the origin dVector boxP0; dVector boxP1; dMatrix matrix (floor->m_curRotation, floor->m_curPosition); NewtonCollisionCalculateAABB (shape, &matrix[0][0], &boxP0.m_x, &boxP1.m_x); // place the origin of the visual mesh at the center of the height field matrix.m_posit = (boxP0 + boxP1).Scale (-0.5f); matrix.m_posit.m_w = 1.0f; floor->m_curPosition = matrix.m_posit; floor->m_prevPosition = matrix.m_posit; // relocate the body; NewtonBodySetMatrix (floorBody, &matrix[0][0]); #else floor->LoadMesh ("LevelMesh.dat"); // add static floor Physics shape = CreateMeshCollision (world, floor, materialMap); floorBody = CreateRigidBody (world, floor, shape, 0.0f); NewtonDestroyCollision(shape); // set the Transformation Matrix for this rigid body dMatrix matrix (floor->m_curRotation, floor->m_curPosition); NewtonBodySetMatrix (floorBody, &matrix[0][0]); #endif // now we will use the properties of this body to set a proper world size. dVector minBox; dVector maxBox; NewtonCollisionCalculateAABB (shape, &matrix[0][0], &minBox[0], &maxBox[0]); // add some extra padding minBox.m_x -= 50.0f; minBox.m_y -= 500.0f; minBox.m_z -= 50.0f; maxBox.m_x += 50.0f; maxBox.m_y += 500.0f; maxBox.m_z += 50.0f; // set the new world size NewtonSetWorldSize (world, &minBox[0], &maxBox[0]); // Create a Body and attach a player controller joint { dFloat y0; Entity* player; NewtonBody* playerBody; NewtonCollision* shape; // find the a floor to place the player y0 = FindFloor (world, 0.0f, 0.0f) + 1.0f; // load the player mesh player = sceneManager->CreateEntity(); player->LoadMesh ("gymnast.dat"); player->m_curPosition.m_y = y0; player->m_prevPosition = player->m_curPosition; // get the bounding Box of the player to get the collision shape dimensions dVector minBox; dVector maxBox; player->GetBBox (minBox, maxBox); // calculate player high and width dFloat padding = 1.0f / 64.0f; // this si the default padding, for teh palye joint, we must subtract it from the shape dFloat playerHigh = (maxBox.m_y - minBox.m_y) - padding; dFloat playerRadius0 = (maxBox.m_z - minBox.m_z) * 0.5f; dFloat playerRadius1 = (maxBox.m_x - minBox.m_x) * 0.5f; dFloat playerRadius = (playerRadius0 > playerRadius1 ? playerRadius0 : playerRadius1) - padding; // No we make and make a upright capsule for the collision mesh dMatrix orientation; orientation.m_front = dVector (0.0f, 1.0f, 0.0f, 0.0f); // this is the player up direction orientation.m_up = dVector (1.0f, 0.0f, 0.0f, 0.0f); // this is the player front direction orientation.m_right = orientation.m_front * orientation.m_up; // this is the player sideway direction orientation.m_posit = dVector (0.0f, 0.0f, 0.0f, 1.0f); // add a body with a box shape //shape = CreateNewtonCapsule (world, player, playerHigh, playerRadius, m_wood, orientation); shape = CreateNewtonCylinder (world, player, playerHigh, playerRadius, m_wood, orientation); playerBody = CreateRigidBody (world, player, shape, 10.0f); NewtonDestroyCollision(shape); // make sure the player does not go to sleep NewtonBodySetAutoSleep (playerBody, 0); // now we will attach a player controller to the body NewtonUserJoint* playerController; // the player can take step up to 0.7 units; dFloat maxStairStepFactor = 0.7f / playerHigh; playerController = CreateCustomPlayerController (&orientation[0][0], playerBody, maxStairStepFactor, padding); // set the Max Slope the player can climb to PLAYER_MAX_SLOPE degree CustomPlayerControllerSetMaxSlope (playerController, PLAYER_MAX_SLOPE * 3.1416f / 180.0f); // now we will append some application data for the application to control the player PlayerController* userControl = (PlayerController*) malloc (sizeof (PlayerController)); userControl->m_isThirdView = 1; userControl->m_point = dVector (0.0f, playerHigh, 0.0f,0.0f); // set the user data for the application to control the player CustomSetUserData (playerController, userControl); // set the destruction call back so that the application can destroy local used data CustomSetDestructorCallback (playerController, PlayerController::Destroy); // set a call back to control the player CustomSetSubmitContraintCallback (playerController, PlayerController::ApplyPlayerInput); // we also need to set override the transform call back so the we can set the Camera userControl->m_setTransformOriginal = NewtonBodyGetTransformCallback(playerBody); NewtonBodySetTransformCallback (playerBody, PlayerController::SetTransform); // we will need some ID to fin this joint in the transform Callback CustomSetJointID (playerController, PLAYER_JOINT_ID); } /* { // add some visual entities. dFloat y0; y0 = FindFloor (world, 0.0f, 0.4f) + 10.5f; for (int i = 0; i < 5; i ++) { Entity* frowny; NewtonBody* frownyBody; NewtonCollision* shape; frowny = sceneManager->CreateEntity(); frowny->LoadMesh ("Frowny.dat"); frowny->m_curPosition.m_z = 0.4f; frowny->m_curPosition.m_y = y0; y0 += 2.0f; frowny->m_prevPosition = frowny->m_curPosition; // add a body with a Convex hull shape shape = CreateNewtonConvex (world, frowny, m_wood); frownyBody = CreateRigidBody (world, frowny, shape, 10.0f); NewtonDestroyCollision(shape); } } */ // set the Camera EyePoint close to the scene action SetCameraEyePoint (dVector (-15.0f, FindFloor (world, -15.0f, 0.0f) + 5.0f, 0.0f)); }
void dCustomKinematicController::ResetAutoSleep () { NewtonBodySetAutoSleep(GetBody0(), 0); }