void CreatCasterBody(dFloat location_x, dFloat location_z, PrimitiveType shapeType, int materialID) { NewtonWorld* const world = ((CustomControllerManager<dClosestDistanceRecord>*)GetManager())->GetWorld(); DemoEntityManager* const scene = (DemoEntityManager*)NewtonWorldGetUserData(world); //dMatrix matrix (GetIdentityMatrix()); dMatrix matrix (dRollMatrix(3.141592f/2.0f)); matrix.m_posit.m_x = location_x; matrix.m_posit.m_y = 2.0f; matrix.m_posit.m_z = location_z; // create the shape and visual mesh as a common data to be re used dVector size(0.5f, 0.5f, 0.75f, 0.0f); NewtonCollision* const collision = CreateConvexCollision (world, dGetIdentityMatrix(), size, shapeType, materialID); // DemoMesh____* const geometry = new DemoMesh____("cylinder_1", collision, "wood_0.tga", "wood_0.tga", "wood_1.tga"); DemoMesh* const geometry = new DemoMesh("convexShape", collision, "smilli.tga", "smilli.tga", "smilli.tga"); m_body = CreateSimpleSolid (scene, geometry, 1.0f, matrix, collision, materialID); // disable gravity and apply a force that only spin the body //NewtonBodySetForceAndTorqueCallback(m_myBody, PhysicsSpinBody); //NewtonBodySetAutoSleep (m_myBody, 0); geometry->Release(); NewtonDestroyCollision (collision); }
void AddPrimitiveArray (DemoEntityManager* const scene, dFloat mass, const dVector& origin, const dVector& size, int xCount, int zCount, dFloat spacing, PrimitiveType type, int materialID, const dMatrix& shapeOffsetMatrix, dFloat startElevation) { // create the shape and visual mesh as a common data to be re used NewtonWorld* const world = scene->GetNewton(); NewtonCollision* const collision = CreateConvexCollision (world, shapeOffsetMatrix, size, type, materialID); // test collision mode //NewtonCollisionSetCollisonMode(collision, 0); DemoMesh* const geometry = new DemoMesh("primitive", collision, "smilli.tga", "smilli.tga", "smilli.tga"); //dFloat startElevation = 1000.0f; //dFloat startElevation = 20.0f; dMatrix matrix (dGetIdentityMatrix()); for (int i = 0; i < xCount; i ++) { dFloat x = origin.m_x + (i - xCount / 2) * spacing; for (int j = 0; j < zCount; j ++) { dFloat z = origin.m_z + (j - zCount / 2) * spacing; matrix.m_posit.m_x = x; matrix.m_posit.m_z = z; dVector floor (FindFloor (world, dVector (matrix.m_posit.m_x, startElevation, matrix.m_posit.m_z, 0.0f), 2.0f * startElevation)); matrix.m_posit.m_y = floor.m_y + size.m_y * 0.5f; CreateSimpleSolid (scene, geometry, mass, matrix, collision, materialID); } } // do not forget to release the assets geometry->Release(); NewtonDestroyCollision (collision); }
static void AddUniformScaledPrimitives (DemoEntityManager* const scene, dFloat mass, const dVector& origin, const dVector& size, int xCount, int zCount, dFloat spacing, PrimitiveType type, int materialID, const dMatrix& shapeOffsetMatrix) { // create the shape and visual mesh as a common data to be re used NewtonWorld* const world = scene->GetNewton(); NewtonCollision* const collision = CreateConvexCollision (world, &shapeOffsetMatrix[0][0], size, type, materialID); dFloat startElevation = 1000.0f; dMatrix matrix (dRollMatrix(-3.141592f/2.0f)); for (int i = 0; i < xCount; i ++) { dFloat x = origin.m_x + (i - xCount / 2) * spacing; for (int j = 0; j < zCount; j ++) { dFloat scale = 0.75f + 1.0f * (dFloat (dRand()) / dFloat(dRAND_MAX) - 0.5f); //scale = 1.0f; NewtonCollisionSetScale (collision, scale, scale, scale); DemoMesh* const geometry = new DemoMesh("cylinder_1", collision, "smilli.tga", "smilli.tga", "smilli.tga"); dFloat z = origin.m_z + (j - zCount / 2) * spacing; matrix.m_posit.m_x = x; matrix.m_posit.m_z = z; dVector floor (FindFloor (world, dVector (matrix.m_posit.m_x, startElevation, matrix.m_posit.m_z, 0.0f), 2.0f * startElevation)); matrix.m_posit.m_y = floor.m_y + 4.f; // create a solid CreateSimpleSolid (scene, geometry, mass, matrix, collision, materialID); // release the mesh geometry->Release(); } } // do not forget to delete the collision NewtonDestroyCollision (collision); }
void SpawnRandomProp(const dMatrix& location) const { NewtonWorld* const world = GetWorld(); DemoEntityManager* const scene = (DemoEntityManager*) NewtonWorldGetUserData(world); //scene->SetCurrent(); static PrimitiveType proSelection[] = {_SPHERE_PRIMITIVE, _BOX_PRIMITIVE, _CAPSULE_PRIMITIVE, _CYLINDER_PRIMITIVE, _CONE_PRIMITIVE, _CHAMFER_CYLINDER_PRIMITIVE, _RANDOM_CONVEX_HULL_PRIMITIVE, _REGULAR_CONVEX_HULL_PRIMITIVE}; PrimitiveType type = PrimitiveType (dRand() % (sizeof (proSelection) / sizeof (proSelection[0]))); dVector size (0.35f, 0.25f, 0.25f, 0.0f); NewtonCollision* const collision = CreateConvexCollision (world, dGetIdentityMatrix(), size, type, 0); DemoMesh* const geometry = new DemoMesh("prop", collision, "smilli.tga", "smilli.tga", "smilli.tga"); dMatrix matrix (location); matrix.m_posit.m_y += 0.5f; NewtonBody* const prop = CreateSimpleSolid (scene, geometry, 30.0f, matrix, collision, 0); NewtonDestroyCollision(collision); geometry->Release(); dFloat initialSpeed = 20.0f; dVector veloc (matrix.m_front.Scale (initialSpeed)); NewtonBodySetVelocity(prop, &veloc[0]); NewtonBodySetLinearDamping(prop, 0); }
void LoadLumberYardMesh(DemoEntityManager* const scene, const dVector& location, int shapeid) { DemoEntity* const entity = DemoEntity::LoadNGD_mesh ("lumber.ngd", scene->GetNewton(), scene->GetShaderCache()); dTree<NewtonCollision*, DemoMesh*> filter; NewtonWorld* const world = scene->GetNewton(); dFloat density = 15.0f; int defaultMaterialID = NewtonMaterialGetDefaultGroupID(scene->GetNewton()); for (DemoEntity* child = entity->GetFirst(); child; child = child->GetNext()) { DemoMesh* const mesh = (DemoMesh*)child->GetMesh(); if (mesh) { dAssert(mesh->IsType(DemoMesh::GetRttiType())); dTree<NewtonCollision*, DemoMesh*>::dTreeNode* node = filter.Find(mesh); if (!node) { // make a collision shape only for and instance dFloat* const array = mesh->m_vertex; dVector minBox(1.0e10f, 1.0e10f, 1.0e10f, 1.0f); dVector maxBox(-1.0e10f, -1.0e10f, -1.0e10f, 1.0f); for (int i = 0; i < mesh->m_vertexCount; i++) { dVector p(array[i * 3 + 0], array[i * 3 + 1], array[i * 3 + 2], 1.0f); minBox.m_x = dMin(p.m_x, minBox.m_x); minBox.m_y = dMin(p.m_y, minBox.m_y); minBox.m_z = dMin(p.m_z, minBox.m_z); maxBox.m_x = dMax(p.m_x, maxBox.m_x); maxBox.m_y = dMax(p.m_y, maxBox.m_y); maxBox.m_z = dMax(p.m_z, maxBox.m_z); } dVector size(maxBox - minBox); dMatrix offset(dGetIdentityMatrix()); offset.m_posit = (maxBox + minBox).Scale(0.5f); NewtonCollision* const shape = NewtonCreateBox(world, size.m_x, size.m_y, size.m_z, shapeid, &offset[0][0]); node = filter.Insert(shape, mesh); } // create a body and add to the world NewtonCollision* const shape = node->GetInfo(); dMatrix matrix(child->GetMeshMatrix() * child->CalculateGlobalMatrix()); matrix.m_posit += location; dFloat mass = density * NewtonConvexCollisionCalculateVolume(shape); CreateSimpleSolid(scene, mesh, mass, matrix, shape, defaultMaterialID); } } // destroy all shapes while (filter.GetRoot()) { NewtonCollision* const shape = filter.GetRoot()->GetInfo(); NewtonDestroyCollision(shape); filter.Remove(filter.GetRoot()); } delete entity; }
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); }
static void BuildJenga (DemoEntityManager* const scene, dFloat mass, const dVector& origin, const dVector& size, int count) { // build a standard block stack of 20 * 3 boxes for a total of 60 NewtonWorld* const world = scene->GetNewton(); dVector blockBoxSize (0.8f, 0.5f, 0.8f * 3.0f); blockBoxSize = blockBoxSize.Scale (1.0f); // create the stack dMatrix baseMatrix (dGetIdentityMatrix()); // for the elevation of the floor at the stack position baseMatrix.m_posit.m_x = origin.m_x; baseMatrix.m_posit.m_z = origin.m_z; dFloat startElevation = 100.0f; dVector floor (FindFloor (world, dVector (baseMatrix.m_posit.m_x, startElevation, baseMatrix.m_posit.m_z, 0.0f), 2.0f * startElevation)); baseMatrix.m_posit.m_y = floor.m_y + blockBoxSize.m_y / 2.0f; // set realistic mass and inertia matrix for each block mass = 5.0f; // create a 90 degree rotation matrix dMatrix rotMatrix (dYawMatrix (3.141592f * 0.5f)); // create a material to control collision with this objects int defaultMaterialID; defaultMaterialID = NewtonMaterialGetDefaultGroupID (world); // separate a bit the block alone the horizontal direction dFloat gap = 0.01f; // create the shape and visual mesh as a common data to be re used NewtonCollision* const collision = CreateConvexCollision (world, dGetIdentityMatrix(), blockBoxSize, _BOX_PRIMITIVE, defaultMaterialID); DemoMesh* const geometry = new DemoMesh("box", collision, "wood_0.tga", "wood_0.tga", "wood_0.tga"); for (int i = 0; i < count; i ++) { dMatrix matrix(baseMatrix); matrix.m_posit -= matrix.m_front.Scale (blockBoxSize.m_x - gap); for (int j = 0; j < 3; j ++) { CreateSimpleSolid (scene, geometry, mass, matrix, collision, defaultMaterialID); matrix.m_posit += matrix.m_front.Scale (blockBoxSize.m_x + gap); } baseMatrix = rotMatrix * baseMatrix; baseMatrix.m_posit += matrix.m_up.Scale (blockBoxSize.m_y * 0.99f); } // do not forget to release the assets geometry->Release(); NewtonDestroyCollision (collision); }
// create a mesh using the NewtonMesh low lever interface static NewtonBody* CreateSimpleBox_NewtonMesh (DemoEntityManager* const scene, const dVector& origin, const dVector& scale, dFloat mass) { dBigVector array[8]; dBigVector scale1 (scale); for (int i = 0; i < 8; i ++) { dBigVector p(&BoxPoints[i * 4]); array[i] = scale1 * p; } NewtonMeshVertexFormat vertexFormat; NewtonMeshClearVertexFormat(&vertexFormat); vertexFormat.m_faceCount = 10; vertexFormat.m_faceIndexCount = faceIndexList; vertexFormat.m_faceMaterial = faceMateriaIndexList; vertexFormat.m_vertex.m_data = &array[0][0]; vertexFormat.m_vertex.m_indexList = BoxIndices; vertexFormat.m_vertex.m_strideInBytes = sizeof (dBigVector); vertexFormat.m_normal.m_data = normal; vertexFormat.m_normal.m_indexList = faceNormalIndex; vertexFormat.m_normal.m_strideInBytes = 3 * sizeof (dFloat); // all channel are now optionals so we not longer has to pass default values // vertexFormat.m_uv0.m_data = uv0; // vertexFormat.m_uv0.m_indexList = uv0_indexList; // vertexFormat.m_uv0.m_strideInBytes = 2 * sizeof (dFloat); // now we create and empty mesh NewtonMesh* const newtonMesh = NewtonMeshCreate(scene->GetNewton()); NewtonMeshBuildFromVertexListIndexList(newtonMesh, &vertexFormat); // now we can use this mesh for lot of stuff, we can apply UV, we can decompose into convex, NewtonCollision* const collision = NewtonCreateConvexHullFromMesh(scene->GetNewton(), newtonMesh, 0.001f, 0); // for now we will simple make simple Box, make a visual Mesh DemoMesh* const visualMesh = new DemoMesh (newtonMesh); dMatrix matrix (dGetIdentityMatrix()); matrix.m_posit = origin; matrix.m_posit.m_w = 1.0f; NewtonBody* const body = CreateSimpleSolid(scene, visualMesh, mass, matrix, collision, 0); dVector veloc(1, 0, 2, 0); NewtonBodySetVelocity(body, &veloc[0]); visualMesh->Release(); NewtonDestroyCollision(collision); NewtonMeshDestroy (newtonMesh); return body; }
static void CreateResititionBody (DemoEntityManager* const scene, DemoMesh* const mesh, dFloat mass, const dMatrix& matrix, NewtonCollision* const collision, int materialId, dFloat restCoef) { NewtonCollisionMaterial material; NewtonCollisionGetMaterial(collision, &material); material.m_userId = 1; // save restitution coefficient in param[0] material.m_userParam[0] = restCoef; NewtonCollisionSetMaterial(collision, &material); NewtonBody* const body = CreateSimpleSolid(scene, mesh, mass, matrix, collision, materialId); NewtonBodySetLinearDamping(body, 0.0f); }
NewtonBody* CreateBox(DemoEntityManager* const scene, const dMatrix& location, const dVector& size, dFloat mass, dFloat inertiaScale) const { NewtonWorld* const world = scene->GetNewton(); int materialID = NewtonMaterialGetDefaultGroupID(world); NewtonCollision* const collision = CreateConvexCollision(world, dGetIdentityMatrix(), size, _BOX_PRIMITIVE, 0); DemoMesh* const geometry = new DemoMesh("primitive", scene->GetShaderCache(), collision, "smilli.tga", "smilli.tga", "smilli.tga"); NewtonBody* const body = CreateSimpleSolid(scene, geometry, mass, location, collision, materialID); ScaleIntertia(body, inertiaScale); geometry->Release(); NewtonDestroyCollision(collision); return body; }
static void BuildPyramid (DemoEntityManager* const scene, dFloat mass, const dVector& origin, const dVector& size, int count, PrimitiveType type, const dMatrix& shapeMatrix = dGetIdentityMatrix()) { dMatrix matrix (dGetIdentityMatrix()); matrix.m_posit = origin; matrix.m_posit.m_w = 1.0f; // create the shape and visual mesh as a common data to be re used NewtonWorld* const world = scene->GetNewton(); int defaultMaterialID; defaultMaterialID = NewtonMaterialGetDefaultGroupID (world); //dMatrix shapeMatrix (dRollMatrix(3.141692f * 0.5f)); NewtonCollision* const collision = CreateConvexCollision (world, shapeMatrix, size, type, defaultMaterialID); DemoMesh* const geometry = new DemoMesh("cylinder_1", collision, "wood_4.tga", "wood_4.tga", "wood_1.tga"); //DemoMesh____* const geometry = new DemoMesh____("cylinder_1", collision, "smilli.tga", "smilli.tga", "smilli.tga"); // matrix = dRollMatrix(3.141592f/2.0f); dFloat startElevation = 100.0f; dVector floor (FindFloor (world, dVector (matrix.m_posit.m_x, startElevation, matrix.m_posit.m_z, 0.0f), 2.0f * startElevation)); matrix.m_posit.m_y = floor.m_y + size.m_y / 2.0f; // get the dimension from shape itself dVector minP; dVector maxP; CalculateAABB (collision, dGetIdentityMatrix(), minP, maxP); //dFloat stepz = size.m_z + 0.01f; dFloat stepz = maxP.m_z - minP.m_z + 0.01f; dFloat stepy = maxP.m_y - minP.m_y; float y0 = matrix.m_posit.m_y + stepy / 2.0f; float z0 = matrix.m_posit.m_z - stepz * count / 2; matrix.m_posit.m_y = y0; for (int j = 0; j < count; j ++) { matrix.m_posit.m_z = z0; for (int i = 0; i < (count - j) ; i ++) { CreateSimpleSolid (scene, geometry, mass, matrix, collision, defaultMaterialID); matrix.m_posit.m_z += stepz; } z0 += stepz * 0.5f; matrix.m_posit.m_y += stepy; } // do not forget to release the assets geometry->Release(); NewtonDestroyCollision (collision); }
NewtonBody* CreateCapsule(DemoEntityManager* const scene, const dMatrix& location, dFloat mass, dFloat inertiaScale, dFloat radius, dFloat height) const { NewtonWorld* const world = scene->GetNewton(); int materialID = NewtonMaterialGetDefaultGroupID(world); dVector size(radius, height, radius, 0.0f); dMatrix align(dYawMatrix(dPi * 0.5f)); NewtonCollision* const collision = CreateConvexCollision(world, align, size, _CAPSULE_PRIMITIVE, 0); DemoMesh* const geometry = new DemoMesh("primitive", scene->GetShaderCache(), collision, "smilli.tga", "smilli.tga", "smilli.tga"); NewtonBody* const body = CreateSimpleSolid(scene, geometry, mass, location, collision, materialID); ScaleIntertia(body, inertiaScale); geometry->Release(); NewtonDestroyCollision(collision); return body; }
static NewtonBody* CreateBox (DemoEntityManager* const scene, const dVector& location, const dVector& size) { NewtonWorld* const world = scene->GetNewton(); int materialID = NewtonMaterialGetDefaultGroupID (world); NewtonCollision* const collision = CreateConvexCollision (world, dGetIdentityMatrix(), size, _BOX_PRIMITIVE, 0); DemoMesh* const geometry = new DemoMesh("primitive", collision, "smilli.tga", "smilli.tga", "smilli.tga"); dFloat mass = 1.0f; dMatrix matrix (dGetIdentityMatrix()); matrix.m_posit = location; matrix.m_posit.m_w = 1.0f; NewtonBody* const body = CreateSimpleSolid (scene, geometry, mass, matrix, collision, materialID); geometry->Release(); NewtonDestroyCollision(collision); return body; }
static NewtonBody* CreateCylinder(DemoEntityManager* const scene, const dVector& location, dFloat mass, dFloat radius, dFloat height) { NewtonWorld* const world = scene->GetNewton(); int materialID = NewtonMaterialGetDefaultGroupID(world); dVector size(radius, height, radius, 0.0f); NewtonCollision* const collision = CreateConvexCollision(world, dGetIdentityMatrix(), size, _CYLINDER_PRIMITIVE, 0); DemoMesh* const geometry = new DemoMesh("primitive", scene->GetShaderCache(), collision, "smilli.tga", "smilli.tga", "smilli.tga"); dMatrix matrix(dRollMatrix (90.0f * dDegreeToRad)); matrix.m_posit = location; matrix.m_posit.m_w = 1.0f; NewtonBody* const body = CreateSimpleSolid(scene, geometry, mass, matrix, collision, materialID); geometry->Release(); NewtonDestroyCollision(collision); return body; }
static void AddNonUniformScaledPrimitives(DemoEntityManager* const scene, dFloat mass, const dVector& origin, const dVector& size, int xCount, int zCount, dFloat spacing, PrimitiveType type, int materialID, const dMatrix& shapeOffsetMatrix) { // create the shape and visual mesh as a common data to be re used NewtonWorld* const world = scene->GetNewton(); // NewtonCollision* const collision = CreateConvexCollision (world, &shapeOffsetMatrix[0][0], size, type, materialID); NewtonCollision* const collision = CreateConvexCollision(world, dGetIdentityMatrix(), size, type, materialID); dFloat startElevation = 1000.0f; dMatrix matrix(dGetIdentityMatrix()); //matrix = dPitchMatrix(-45.0f * 3.141592f/180.0f); for (int i = 0; i < xCount; i++) { dFloat x = origin.m_x + (i - xCount / 2) * spacing; for (int j = 0; j < zCount; j++) { dFloat scalex = 1.0f + 1.5f * (dFloat(dRand()) / dFloat(dRAND_MAX) - 0.5f); dFloat scaley = 1.0f + 1.5f * (dFloat(dRand()) / dFloat(dRAND_MAX) - 0.5f); dFloat scalez = 1.0f + 1.5f * (dFloat(dRand()) / dFloat(dRAND_MAX) - 0.5f); dFloat z = origin.m_z + (j - zCount / 2) * spacing; matrix.m_posit.m_x = x; matrix.m_posit.m_z = z; dVector floor(FindFloor(world, dVector(matrix.m_posit.m_x, startElevation, matrix.m_posit.m_z, 0.0f), 2.0f * startElevation)); matrix.m_posit.m_y = floor.m_y + 8.0f; // create a solid //NewtonBody* const body = CreateSimpleSolid (scene, geometry, mass, matrix, collision, materialID); NewtonBody* const body = CreateSimpleSolid(scene, NULL, mass, matrix, collision, materialID); DemoEntity* entity = (DemoEntity*)NewtonBodyGetUserData(body); NewtonCollisionSetScale(collision, scalex, scaley, scalez); DemoMesh* const geometry = new DemoMesh("cylinder_1", collision, "smilli.tga", "smilli.tga", "smilli.tga"); entity->SetMesh(geometry, dGetIdentityMatrix()); NewtonBodySetCollisionScale(body, scalex, scaley, scalez); dVector omega(0.0f); NewtonBodySetOmega(body, &omega[0]); // release the mesh geometry->Release(); } } // do not forget to delete the collision NewtonDestroyCollision(collision); }
NewtonBody* AddFloorBox(DemoEntityManager* const scene, const dVector& origin, const dVector& size) { // create the shape and visual mesh as a common data to be re used NewtonWorld* const world = scene->GetNewton(); const int materialID = NewtonMaterialGetDefaultGroupID(scene->GetNewton()); NewtonCollision* const collision = CreateConvexCollision(world, dGetIdentityMatrix(), size, _BOX_PRIMITIVE, materialID); // test collision mode //NewtonCollisionSetCollisonMode(collision, 0); DemoMesh* const geometry = new DemoMesh("primitive", scene->GetShaderCache(), collision, "wood_3.tga", "wood_3.tga", "wood_3.tga"); dMatrix matrix(dGetIdentityMatrix()); matrix.m_posit = origin; matrix.m_posit.m_w = 1.0f; NewtonBody* const body = CreateSimpleSolid(scene, geometry, 0.0f, matrix, collision, materialID); // do not forget to release the assets geometry->Release(); NewtonDestroyCollision(collision); return body; }
void CreatCasterBody(dFloat location_x, dFloat location_z, PrimitiveType shapeType, int materialID) { NewtonWorld* const world = ((dCustomControllerManager<dClosestDistanceRecord>*)GetManager())->GetWorld(); DemoEntityManager* const scene = (DemoEntityManager*)NewtonWorldGetUserData(world); //dMatrix matrix (GetIdentityMatrix()); dMatrix matrix (dRollMatrix(3.141592f/2.0f)); matrix.m_posit.m_x = location_x; matrix.m_posit.m_y = 2.0f; matrix.m_posit.m_z = location_z; // create the shape and visual mesh as a common data to be re used dVector size(0.5f, 0.5f, 0.75f, 0.0f); NewtonCollision* const collision = CreateConvexCollision (world, dGetIdentityMatrix(), size, shapeType, materialID); DemoMesh* const geometry = new DemoMesh("convexShape", collision, "smilli.tga", "smilli.tga", "smilli.tga"); m_body = CreateSimpleSolid (scene, geometry, 1.0f, matrix, collision, materialID); geometry->Release(); NewtonDestroyCollision (collision); }
static void AddSinglePrimitive (DemoEntityManager* const scene, dFloat x, PrimitiveType type, int materialID) { dVector size(0.5f, 0.5f, 0.75f, 0.0f); dMatrix matrix (dGetIdentityMatrix()); // create the shape and visual mesh as a common data to be re used NewtonWorld* const world = scene->GetNewton(); NewtonCollision* const collision = CreateConvexCollision (world, dGetIdentityMatrix(), size, type, materialID); // DemoMesh____* const geometry = new DemoMesh____("cylinder_1", collision, "wood_0.tga", "wood_0.tga", "wood_1.tga"); DemoMesh* const geometry = new DemoMesh("cylinder_1", scene->GetShaderCache(), collision, "smilli.tga", "smilli.tga", "smilli.tga"); matrix = dRollMatrix(dPi/2.0f); matrix.m_posit.m_x = 0; matrix.m_posit.m_z = x; matrix.m_posit.m_y = 0; CreateSimpleSolid (scene, geometry, 1.0, matrix, collision, materialID); // do not forget to release the assets geometry->Release(); NewtonDestroyCollision (collision); }
void Restitution (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 NewtonWorld* const world = scene->GetNewton(); dMatrix offsetMatrix (dGetIdentityMatrix()); int defaultMaterialID = NewtonMaterialGetDefaultGroupID (world); NewtonMaterialSetCollisionCallback (world, defaultMaterialID, defaultMaterialID, NULL, UserContactRestitution); CreateLevelMesh (scene, "flatPlane.ngd", 0); dVector location (0.0f, 0.0f, 0.0f, 0.0f); dVector size (0.5f, 0.5f, 1.0f, 0.0f); // create some spheres dVector sphSize (1.0f, 1.0f, 1.0f, 0.0f); NewtonCollision* const sphereCollision = CreateConvexCollision (world, offsetMatrix, sphSize, _SPHERE_PRIMITIVE, 0); DemoMesh* const sphereMesh = new DemoMesh("sphere", sphereCollision, "smilli.tga", "smilli.tga", "smilli.tga"); // create some boxes too dVector boxSize (1.0f, 0.5f, 2.0f, 0.0f); NewtonCollision* const boxCollision = CreateConvexCollision (world, offsetMatrix, boxSize, _BOX_PRIMITIVE, 0); DemoMesh* const boxMesh = new DemoMesh("box", boxCollision, "smilli.tga", "smilli.tga", "smilli.tga"); int zCount = 10; dFloat spacing = 4.0f; dMatrix matrix (dGetIdentityMatrix()); dVector origin (matrix.m_posit); origin.m_x -= 0.0f; // create a simple scene for (int i = 0; i < zCount; i ++) { dFloat z; dFloat x; dFloat mass; dVector size (1.0f, 0.5f, 2.0f, 0.0f); x = origin.m_x; z = origin.m_z + (i - zCount / 2) * spacing; mass = 1.0f; matrix.m_posit = FindFloor (world, dVector (x, 100.0f, z), 200.0f); matrix.m_posit.m_w = 1.0f; float restitution; NewtonBody* body; NewtonCollision* collision; matrix.m_posit.m_y += 4.0f; body = CreateSimpleSolid (scene, sphereMesh, mass, matrix, sphereCollision, defaultMaterialID); NewtonBodySetLinearDamping (body, 0.0f); collision = NewtonBodyGetCollision(body); restitution = i * 0.1f + 0.083f; NewtonCollisionSetUserData (collision, *((void**)&restitution)); matrix.m_posit.m_y += 4.0f; //body = CreateSimpleSolid (scene, sphereMesh, mass, matrix, sphereCollision, defaultMaterialID, shapeOffsetMatrix); body = CreateSimpleSolid (scene, boxMesh, mass, matrix, boxCollision, defaultMaterialID); NewtonBodySetLinearDamping (body, 0.0f); collision = NewtonBodyGetCollision(body); restitution = i * 0.1f + 0.083f; NewtonCollisionSetUserData (collision, *((void**)&restitution)); matrix.m_posit.m_y += 4.0f; body = CreateSimpleSolid (scene, sphereMesh, mass, matrix, sphereCollision, defaultMaterialID); NewtonBodySetLinearDamping (body, 0.0f); collision = NewtonBodyGetCollision(body); restitution = i * 0.1f + 0.083f; NewtonCollisionSetUserData (collision, *((void**)&restitution)); matrix.m_posit.m_y += 4.0f; dVector boxSize (1.0f, 0.5f, 2.0f, 0.0f); body = CreateSimpleSolid (scene, boxMesh, mass, matrix, boxCollision, defaultMaterialID); NewtonBodySetLinearDamping (body, 0.0f); collision = NewtonBodyGetCollision(body); restitution = i * 0.1f + 0.083f; NewtonCollisionSetUserData (collision, *((void**)&restitution)); } boxMesh->Release(); sphereMesh->Release(); NewtonDestroyCollision(boxCollision); NewtonDestroyCollision(sphereCollision); dMatrix camMatrix (dGetIdentityMatrix()); dQuaternion rot (camMatrix); origin = dVector (-25.0f, 5.0f, 0.0f, 0.0f); scene->SetCameraMatrix(rot, origin); }
// create a mesh using the dNewtonMesh wrapper static NewtonBody* CreateSimpledBox_dNetwonMesh(DemoEntityManager* const scene, const dVector& origin, const dVector& scale, dFloat mass) { dVector array[8]; dVector scale1(scale); for (int i = 0; i < 8; i++) { dVector p(&BoxPoints[i * 4]); array[i] = scale1 * p; } dNewtonMesh buildMesh(scene->GetNewton()); // start build a mesh buildMesh.BeginBuild(); // add faces one at a time int index = 0; const int faceCount = sizeof (faceIndexList)/sizeof (faceIndexList[0]); for (int i = 0; i < faceCount; i ++) { const int indexCount = faceIndexList[i]; const int faceMaterail = faceMateriaIndexList[i]; buildMesh.BeginPolygon(); for (int j = 0; j < indexCount; j ++) { // add a mesh point int k = BoxIndices[index + j]; buildMesh.AddPoint(array[k].m_x, array[k].m_y, array[k].m_z); // add vertex normal int n = faceNormalIndex[index + j]; buildMesh.AddNormal (dFloat32 (normal[n * 3 + 0]), dFloat32 (normal[n * 3 + 1]), dFloat32 (normal[n * 3 + 2])); // add face material index buildMesh.AddMaterial(faceMaterail); // continue adding more vertex attributes of you want, uv, binormal, weights, etc } buildMesh.EndPolygon(); index += indexCount; } buildMesh.EndBuild(); // get the newtonMesh from the dNewtonMesh class and use it to build a render mesh, collision or anything NewtonMesh* const newtonMesh = buildMesh.GetMesh(); // test collision tree //NewtonCollision* const collisionTree = NewtonCreateTreeCollisionFromMesh (scene->GetNewton(), newtonMesh, 0); //NewtonDestroyCollision(collisionTree); // now we can use this mesh for lot of stuff, we can apply UV, we can decompose into convex, NewtonCollision* const collision = NewtonCreateConvexHullFromMesh(scene->GetNewton(), newtonMesh, 0.001f, 0); // for now we will simple make simple Box, make a visual Mesh DemoMesh* const visualMesh = new DemoMesh(newtonMesh); dMatrix matrix(dGetIdentityMatrix()); matrix.m_posit = origin; matrix.m_posit.m_w = 1.0f; NewtonBody* const body = CreateSimpleSolid(scene, visualMesh, mass, matrix, collision, 0); dVector veloc (1, 0, 2, 0); NewtonBodySetVelocity(body, &veloc[0]); visualMesh->Release(); NewtonDestroyCollision(collision); return body; }
// create physics scene void PuckSlide (DemoEntityManager* const scene) { scene->CreateSkyBox(); NewtonWorld* const world = scene->GetNewton(); int materialGroupIDs[SB_NUM_MATERIALS]; // Create groups for (int i = 0; i < SB_NUM_MATERIALS; i++) { materialGroupIDs[i] = NewtonMaterialCreateGroupID(world); } // Setup the material data NewtonMaterialSetDefaultSoftness(world, materialGroupIDs[SBMaterial_WEIGHT], materialGroupIDs[SBMaterial_SURFACE], 0.15f); NewtonMaterialSetDefaultElasticity(world, materialGroupIDs[SBMaterial_WEIGHT], materialGroupIDs[SBMaterial_SURFACE], 0.30f); NewtonMaterialSetDefaultFriction(world, materialGroupIDs[SBMaterial_WEIGHT], materialGroupIDs[SBMaterial_SURFACE], 0.05f, 0.04f); // setup callbacks for collisions between two material groups NewtonMaterialSetCollisionCallback(world,materialGroupIDs[SBMaterial_WEIGHT],materialGroupIDs[SBMaterial_SURFACE],NULL,PhysicsNewton_CollisionPuckSurfaceCB); /////// // Add table { dVector tableSize(TABLE_LENGTH, TABLE_HEIGHT, TABLE_WIDTH, 0.0f); // create the shape and visual mesh as a common data to be re used NewtonCollision* const collision = CreateConvexCollision (world, dGetIdentityMatrix(), tableSize, _BOX_PRIMITIVE, materialGroupIDs[SBMaterial_SURFACE]); DemoMesh* const geometry = new DemoMesh("cylinder_1", collision, "wood_3.tga", "wood_3.tga", "wood_3.tga"); dMatrix matrix = dGetIdentityMatrix(); matrix.m_posit.m_x = 0.0f; matrix.m_posit.m_z = 0.0f; matrix.m_posit.m_y = 0.0f; NewtonBody* const tableBody = CreateSimpleSolid (scene, geometry, 0.0, matrix, collision, materialGroupIDs[SBMaterial_SURFACE]); // this is deprecated, use NewtonBodySetMassProperties //NewtonBodySetMassMatrix(tableBody, 0.0f, 1.0f, 1.0f, 1.0f); NewtonBodySetMassProperties(tableBody, 0.0f, NewtonBodyGetCollision(tableBody)); NewtonBodySetMaterialGroupID(tableBody, materialGroupIDs[SBMaterial_SURFACE]); // it is not wise to se static body to continuous collision mode //NewtonBodySetContinuousCollisionMode(tableBody, 1); // do not forget to release the assets geometry->Release(); // the collision need to be destroy, the body is using an instance no a reference NewtonDestroyCollision (collision); } /////// // Add puck { new PuckEntity (scene, materialGroupIDs[SBMaterial_WEIGHT]); } // place camera into position dMatrix camMatrix (dPitchMatrix(20.0f * 3.1416f /180.0f)); dQuaternion rot (camMatrix); dVector origin (CAMERA_Z, CAMERA_Y, CAMERA_X, 0.0f); scene->SetCameraMatrix(rot, origin); }
static void MakeFunnyCompound (DemoEntityManager* const scene, const dVector& origin) { NewtonWorld* const world = scene->GetNewton(); // create an empty compound collision NewtonCollision* const compound = NewtonCreateCompoundCollision (world, 0); #if 1 NewtonCompoundCollisionBeginAddRemove(compound); // add a bunch of convex collision at random position and orientation over the surface of a big sphere float radio = 5.0f; for (int i = 0 ; i < 300; i ++) { NewtonCollision* collision = NULL; float pitch = RandomVariable (1.0f) * 2.0f * 3.1416f; float yaw = RandomVariable (1.0f) * 2.0f * 3.1416f; float roll = RandomVariable (1.0f) * 2.0f * 3.1416f; float x = RandomVariable (0.5f); float y = RandomVariable (0.5f); float z = RandomVariable (0.5f); if ((x == 0.0f) && (y == 0.0f) && (z == 0.0f)){ x = 0.1f; } dVector p (x, y, z, 1.0f) ; p = p.Scale (radio / dSqrt (p % p)); dMatrix matrix (dPitchMatrix (pitch) * dYawMatrix (yaw) * dRollMatrix (roll)); matrix.m_posit = p; int r = dRand(); switch ((r >>2) & 3) { case 0: { collision = NewtonCreateSphere(world, 0.5, 0, &matrix[0][0]) ; break; } case 1: { collision = NewtonCreateCapsule(world, 0.3f, 0.2f, 0.5f, 0, &matrix[0][0]) ; break; } case 2: { collision = NewtonCreateCylinder(world, 0.25, 0.5, 0.25, 0, &matrix[0][0]) ; break; } case 3: { collision = NewtonCreateCone(world, 0.25, 0.25, 0, &matrix[0][0]) ; break; } } dAssert (collision); // we can set a collision id, and use data per sub collision NewtonCollisionSetUserID(collision, i); NewtonCollisionSetUserData(collision, (void*) i); // add this new collision NewtonCompoundCollisionAddSubCollision (compound, collision); NewtonDestroyCollision(collision); } // finish adding shapes NewtonCompoundCollisionEndAddRemove(compound); { // remove the first 10 shapes // test remove shape form a compound NewtonCompoundCollisionBeginAddRemove(compound); void* node = NewtonCompoundCollisionGetFirstNode(compound); for (int i = 0; i < 10; i ++) { //NewtonCollision* const collision = NewtonCompoundCollisionGetCollisionFromNode(compound, node); void* const nextNode = NewtonCompoundCollisionGetNextNode(compound, node); NewtonCompoundCollisionRemoveSubCollision(compound, node); node = nextNode; } // finish remove void* handle1 = NewtonCompoundCollisionGetNodeByIndex (compound, 30); void* handle2 = NewtonCompoundCollisionGetNodeByIndex (compound, 100); NewtonCollision* const shape1 = NewtonCompoundCollisionGetCollisionFromNode (compound, handle1); NewtonCollision* const shape2 = NewtonCompoundCollisionGetCollisionFromNode (compound, handle2); NewtonCollision* const copyShape1 = NewtonCollisionCreateInstance (shape1); NewtonCollision* const copyShape2 = NewtonCollisionCreateInstance (shape2); // you can also remove shape by their index NewtonCompoundCollisionRemoveSubCollisionByIndex (compound, 30); NewtonCompoundCollisionRemoveSubCollisionByIndex (compound, 100); handle1 = NewtonCompoundCollisionAddSubCollision (compound, copyShape1); handle2 = NewtonCompoundCollisionAddSubCollision (compound, copyShape2); NewtonDestroyCollision(copyShape1); NewtonDestroyCollision(copyShape2); NewtonCompoundCollisionEndAddRemove(compound); } { // show how to modify the children of a compound collision NewtonCompoundCollisionBeginAddRemove(compound); for (void* node = NewtonCompoundCollisionGetFirstNode(compound); node; node = NewtonCompoundCollisionGetNextNode(compound, node)) { NewtonCollision* const collision = NewtonCompoundCollisionGetCollisionFromNode(compound, node); // you can scale, change the matrix, change the inertia, do anything you want with the change NewtonCollisionSetUserData(collision, NULL); } NewtonCompoundCollisionEndAddRemove(compound); } // NewtonCollisionSetScale(compound, 0.5f, 0.25f, 0.125f); #else //test Yeside compound shape shape // - Rotation="1.5708 -0 0" Translation="0 0 0.024399" Size="0.021 0.096" Pos="0 0 0.115947" // - Rotation="1.5708 -0 0" Translation="0 0 0.056366" Size="0.195 0.024" Pos="0 0 0.147914" // - Rotation="1.5708 -0 0" Translation="0 0 -0.056366" Size="0.0065 0.07 Pos="0 0 0.035182" NewtonCompoundCollisionBeginAddRemove(compound); NewtonCollision* collision; dMatrix offsetMatrix (dPitchMatrix(1.5708f)); offsetMatrix.m_posit.m_z = 0.115947f; collision = NewtonCreateCylinder (world, 0.021f, 0.096f, 0, &offsetMatrix[0][0]) ; NewtonCompoundCollisionAddSubCollision (compound, collision); NewtonDestroyCollision(collision); offsetMatrix.m_posit.m_z = 0.035182f; collision = NewtonCreateCylinder (world, 0.0065f, 0.07f, 0, &offsetMatrix[0][0]) ; NewtonCompoundCollisionAddSubCollision (compound, collision); NewtonDestroyCollision(collision); offsetMatrix.m_posit.m_z = 0.147914f; collision = NewtonCreateCylinder (world, 0.195f, 0.024f, 0, &offsetMatrix[0][0]) ; NewtonCompoundCollisionAddSubCollision (compound, collision); NewtonDestroyCollision(collision); NewtonCompoundCollisionEndAddRemove(compound); #endif // for now we will simple make simple Box, make a visual Mesh DemoMesh* const visualMesh = new DemoMesh ("big ball", compound, "metal_30.tga", "metal_30.tga", "metal_30.tga"); int instaceCount = 2; dMatrix matrix (dGetIdentityMatrix()); matrix.m_posit = origin; for (int ix = 0; ix < instaceCount; ix ++) { for (int iz = 0; iz < instaceCount; iz ++) { dFloat y = origin.m_y; dFloat x = origin.m_x + (ix - instaceCount/2) * 15.0f; dFloat z = origin.m_z + (iz - instaceCount/2) * 15.0f; matrix.m_posit = FindFloor (world, dVector (x, y + 10.0f, z, 0.0f), 20.0f); ; matrix.m_posit.m_y += 15.0f; CreateSimpleSolid (scene, visualMesh, 10.0f, matrix, compound, 0); } } visualMesh->Release(); NewtonDestroyCollision(compound); }
static NewtonBody* CreateSimpleNewtonMeshBox (DemoEntityManager* const scene, const dVector& origin, const dVector& scale, dFloat mass) { // the vertex array, vertices's has for values, x, y, z, w // w is use as a id to have multiple copy of the same very, like for example mesh that share more than two edges. // in most case w can be set to 0.0 static dFloat64 BoxPoints[] = { -1.0, -1.0, -1.0, 0.0, -1.0, -1.0, 1.0, 0.0, -1.0, 1.0, 1.0, 0.0, -1.0, 1.0, -1.0, 0.0, 1.0, -1.0, -1.0, 0.0, 1.0, -1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, -1.0, 0.0, }; // the vertex index list is an array of all the face, in any order, the can be convex or concave, // and has and variable umber of indices static int BoxIndices[] = { 2,3,0,1, // this is quad 5,2,1, // triangle 6,2,5, // another triangle 5,1,0,4, // another quad 2,7,3, // and so on 6,7,2, 3,4,0, 7,4,3, 7,5,4, 6,5,7 }; // the number of index for each face is specified by an array of consecutive face index static int faceIndexList [] = {4, 3, 3, 4, 3, 3, 3, 3, 3, 3}; // each face can have an arbitrary index that the application can use as a material index // for example the index point to a texture, we can have the each face of the cube with a different texture static int faceMateriaIndexList [] = {0, 4, 4, 2, 3, 3, 3, 3, 3, 3}; // the normal is specified per vertex and each vertex can have a unique normal or a duplicated // for example a cube has 6 normals static dFloat normal[] = { 1.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, -1.0, }; static int faceNormalIndex [] = { 0, 0, 0, 0, // first face uses the first normal of each vertex 3, 3, 3, // second face uses the third normal 3, 3, 3, // third face uses the fifth normal 1, 1, 1, 1, // third face use the second normal 2, 2, 2, // and so on 2, 2, 2, 4, 2, 1, // a face can have per vertex normals 4, 4, 4, 5, 5, 5, // two coplanar face can even has different normals 3, 2, 0, }; /* // the UV are encode the same way as the vertex an the normals, a UV list and an index list // since we do not have UV we can assign the all to zero static dFloat uv0[] = { 0, 0}; static int uv0_indexList [] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }; */ dBigVector array[8]; dBigVector scale1 (scale); for (int i = 0; i < 8; i ++) { dBigVector p(&BoxPoints[i * 4]); array[i] = scale1 * p; } NewtonMeshVertexFormat vertexFormat; NewtonMeshClearVertexFormat(&vertexFormat); vertexFormat.m_faceCount = 10; vertexFormat.m_faceIndexCount = faceIndexList; vertexFormat.m_faceMaterial = faceMateriaIndexList; vertexFormat.m_vertex.m_data = &array[0][0]; vertexFormat.m_vertex.m_indexList = BoxIndices; vertexFormat.m_vertex.m_strideInBytes = sizeof (dBigVector); vertexFormat.m_normal.m_data = normal; vertexFormat.m_normal.m_indexList = faceNormalIndex; vertexFormat.m_normal.m_strideInBytes = 3 * sizeof (dFloat); // all channel are now optionals so we not longer has to pass default values // vertexFormat.m_uv0.m_data = uv0; // vertexFormat.m_uv0.m_indexList = uv0_indexList; // vertexFormat.m_uv0.m_strideInBytes = 2 * sizeof (dFloat); // now we create and empty mesh NewtonMesh* const newtonMesh = NewtonMeshCreate(scene->GetNewton()); NewtonMeshBuildFromVertexListIndexList(newtonMesh, &vertexFormat); // now we can use this mesh for lot of stuff, we can apply UV, we can decompose into convex, NewtonCollision* const collision = NewtonCreateConvexHullFromMesh(scene->GetNewton(), newtonMesh, 0.001f, 0); // for now we will simple make simple Box, make a visual Mesh DemoMesh* const visualMesh = new DemoMesh (newtonMesh); dMatrix matrix (dGetIdentityMatrix()); matrix.m_posit = origin; matrix.m_posit.m_w = 1.0f; NewtonBody* const body = CreateSimpleSolid (scene, visualMesh, mass, matrix, collision, 0); visualMesh->Release(); NewtonDestroyCollision(collision); NewtonMeshDestroy (newtonMesh); return body; }
static void CreateConvexAproximation (const char* const name, DemoEntityManager* const scene, const dVector& origin, int instaceCount, const char* const texture) { char fileName[2048]; dGetWorkingFileName (name, fileName); NewtonWorld* const world = scene->GetNewton(); dScene compoundTestMesh (world); compoundTestMesh.Deserialize(fileName); // freeze the scale and pivot on the model compoundTestMesh.FreezeScale(); // compoundTestMesh.FreezeGeometryPivot (); dMeshNodeInfo* meshInfo = NULL; dMatrix scale (dGetIdentityMatrix()); for (dScene::dTreeNode* node = compoundTestMesh.GetFirstNode (); node; node = compoundTestMesh.GetNextNode (node)) { dNodeInfo* info = compoundTestMesh.GetInfoFromNode(node); if (info->GetTypeId() == dMeshNodeInfo::GetRttiType()) { for (void* link = compoundTestMesh.GetFirstParentLink(node); link; link = compoundTestMesh.GetNextParentLink (node, link)) { dScene::dTreeNode* const node = compoundTestMesh.GetNodeFromLink(link); dNodeInfo* const info = compoundTestMesh.GetInfoFromNode(node); if (info->GetTypeId() == dSceneNodeInfo::GetRttiType()) { scale = ((dSceneNodeInfo*)info)->GetGeometryTransform(); break; } } meshInfo = (dMeshNodeInfo*) info; break; } } NewtonMeshApplyTransform(meshInfo->GetMesh(), &scale[0][0]); dAssert (meshInfo); dAssert (meshInfo->GetMesh()); #if 1 NewtonMesh* const mesh = meshInfo->GetMesh(); #else dGetWorkingFileName ("mesh.off", fileName); NewtonMesh* const mesh = NewtonMeshLoadOFF(world, fileName); // dMatrix scale (GetIdentityMatrix()); // //dFloat scaleMag = 0.05f; // dFloat scaleMag = 1.0f; // scale[0][0] = scaleMag; // scale[1][1] = scaleMag; // scale[2][2] = scaleMag; // NewtonMesApplyTransform (mesh, &scale[0][0]); #endif //NewtonMesh* const newtonMesh = NewtonMeshSimplify(mesh, 500, ReportProgress); // create a convex approximation form the original mesh, 32 convex max and no more than 100 vertex convex hulls // NewtonMesh* const convexApproximation = NewtonMeshApproximateConvexDecomposition (mesh, 0.01f, 0.2f, 32, 100, ReportProgress, scene); NewtonMesh* const convexApproximation = NewtonMeshApproximateConvexDecomposition (mesh, 0.01f, 0.2f, 256, 100, ReportProgress, scene); // NewtonMesh* const convexApproximation = NewtonMeshApproximateConvexDecomposition (mesh, 0.00001f, 0.0f, 256, 100, ReportProgress, scene); // create a compound collision by creation a convex hull of each segment of the source mesh NewtonCollision* const compound = NewtonCreateCompoundCollisionFromMesh (world, convexApproximation, 0.001f, 0, 0); // test collision mode // NewtonCollisionSetCollisonMode(compound, 0); // make a visual Mesh int tex = LoadTexture(texture); dMatrix aligmentUV(dGetIdentityMatrix()); NewtonMeshApplyBoxMapping(mesh, tex, tex, tex, &aligmentUV[0][0]); DemoMesh* const visualMesh = new DemoMesh (mesh); dMatrix matrix (dGetIdentityMatrix()); matrix.m_posit = origin; for (int ix = 0; ix < instaceCount; ix ++) { for (int iz = 0; iz < instaceCount; iz ++) { dFloat y = origin.m_y; dFloat x = origin.m_x + (ix - instaceCount/2) * 10.0f; dFloat z = origin.m_z + (iz - instaceCount/2) * 10.0f; matrix.m_posit = FindFloor (world, dVector (x, y + 10.0f, z, 0.0f), 20.0f); ; matrix.m_posit.m_y += 2.0f; CreateSimpleSolid (scene, visualMesh, 10.0f, matrix, compound, 0); } } visualMesh->Release(); NewtonDestroyCollision(compound); NewtonMeshDestroy (convexApproximation); }