bool Physics::serialize( const std::string& meshFile, const std::string& outputFile ) { log_debug << "Physics: serializing physics static geometry '" << meshFile << "'" << std::endl; log_debug << "Physics: unloading existing level" << std::endl; // first unload existing level LevelManager::get()->unloadLevel( true, true ); osg::ref_ptr< osg::Node > meshnode = LevelManager::get()->loadMesh( meshFile, false ); if ( !meshnode.valid() ) return false; osg::ref_ptr< osg::Group > root = new osg::Group; root->addChild( meshnode.get() ); // begin build the collision faces //-------------------------- NewtonCollision* p_collision = NewtonCreateTreeCollision( _p_world, levelCollisionCallback ); NewtonTreeCollisionBeginBuild( p_collision ); // start timer osg::Timer_t start_tick = osg::Timer::instance()->tick(); //! iterate through all geometries and create their collision faces PhysicsVisitor physVisitor( osg::NodeVisitor::TRAVERSE_ALL_CHILDREN, p_collision ); root->accept( physVisitor ); // stop timer and give out the time messure osg::Timer_t end_tick = osg::Timer::instance()->tick(); log_debug << "Physics: elapsed time for building physics collision faces = "<< osg::Timer::instance()->delta_s( start_tick, end_tick ) << std::endl; log_debug << "Physics: total num of evaluated primitives: " << physVisitor.getNumPrimitives() << std::endl; log_debug << "Physics: total num of vertices: " << physVisitor.getNumVertices() << std::endl; //-------------------------- // finalize tree building NewtonTreeCollisionEndBuild( p_collision, 0 ); // write out the serialization data std::string file( yaf3d::Application::get()->getMediaPath() + outputFile + YAF3DPHYSICS_SERIALIZE_POSTFIX ); log_debug << "Physics: write to serialization file '" << file << "'" << std::endl; std::ofstream serializationoutput; serializationoutput.open( file.c_str(), std::ios_base::binary | std::ios_base::out ); if ( !serializationoutput ) { log_error << "Physics: cannot write to serialization file '" << file << "'" << std::endl; serializationoutput.close(); NewtonReleaseCollision( _p_world, p_collision ); return false; } NewtonTreeCollisionSerialize( p_collision, serializationCallback, &serializationoutput ); serializationoutput.close(); NewtonReleaseCollision( _p_world, p_collision ); return true; }
void dCollisionConeNodeInfo::CalculateInertiaGeometry (dScene* world, dVector& inertia, dVector& centerOfMass) const { NewtonWorld* newton = world->GetNewtonWorld(); NewtonCollision* box = NewtonCreateCylinder(newton, m_radius, m_height, 0, &m_matrix[0][0]); CalculateGeometryProperies (box, inertia, centerOfMass); NewtonReleaseCollision (newton, box); }
NewtonBaseObject::~NewtonBaseObject() { if (NtCollision_) NewtonReleaseCollision(NewtonSimulator::getNewtonWorld(), NtCollision_); if (NtBody_) NewtonDestroyBody(NewtonSimulator::getNewtonWorld(), NtBody_); }
void CCamera::Initialiser(NewtonWorld *nWorld,CVector3 taille) { // On initialise le vecteur de dimensions m_vTailleCollisionCam.x = taille.x; m_vTailleCollisionCam.y = taille.y; m_vTailleCollisionCam.z = taille.z; CMatrix matrice; // On créé une matrice matrice.setIdentite(); // On définit la matrice de manière à ce que l'objet soit placé aux positions // spécifiées en utilisant la dernière colonne de la matrice matrice.m_Mat[3][0] = 0; matrice.m_Mat [3][1] = 0; matrice.m_Mat [3][2] = 0; // On initialise la boîte de collision NewtonCollision * collision = NULL; // On créé la boite de collision aux dimensions de l'objet collision = NewtonCreateBox (nWorld, m_vTailleCollisionCam.x, m_vTailleCollisionCam.y, m_vTailleCollisionCam.z, NULL); // On initialise le corps avec la boite de collision m_pBody = NewtonCreateBody (nWorld, collision); if (m_pBody == NULL) std::cerr << "Impossible d'initialiser le corps."; // On détruit la boite de collision, on n'en a plus besoin NewtonReleaseCollision (nWorld, collision); // Enfin, on affecte notre matrice (qui représente donc sa position dans l'espace) // à notre corps grâce à la fonction NewtonBodySetMatrix NewtonBodySetMatrix (m_pBody, &matrice.m_Mat [0][0]); }
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; }
/** * @brief * Constructor */ BodyEllipsoid::BodyEllipsoid(PLPhysics::World &cWorld, const Vector3 &vRadius) : PLPhysics::BodyEllipsoid(cWorld, static_cast<World&>(cWorld).CreateBodyImpl(), vRadius) { // Deactivate the physics simulation if required const bool bSimulationActive = cWorld.IsSimulationActive(); if (bSimulationActive) cWorld.SetSimulationActive(false); // Get the Newton physics world Newton::NewtonWorld *pNewtonWorld = static_cast<World&>(cWorld).GetNewtonWorld(); // Create collision primitive Newton::NewtonCollision *pCollision = NewtonCreateSphere(pNewtonWorld, m_vRadius.x, m_vRadius.y, m_vRadius.z, 0, nullptr); // Create the rigid body // [TODO] Remove this as soon as there's an up-to-date Linux version of Newton Game Dynamics available! #if (NEWTON_MAJOR_VERSION == 2) && (NEWTON_MINOR_VERSION >= 28) Newton::NewtonBody *pNewtonBody = NewtonCreateBody(pNewtonWorld, pCollision, Matrix4x4::Identity); #else Newton::NewtonBody *pNewtonBody = NewtonCreateBody(pNewtonWorld, pCollision); #endif NewtonReleaseCollision(pNewtonWorld, pCollision); // Calculate the collision volume const float fCollisionVolume = static_cast<float>((4/3)*Math::Pi*m_vRadius.x*m_vRadius.y*m_vRadius.z); // Initialize the Newton physics body static_cast<BodyImpl&>(GetBodyImpl()).InitializeNewtonBody(*this, *pNewtonBody, fCollisionVolume); // Reactivate the physics simulation if required if (bSimulationActive) cWorld.SetSimulationActive(bSimulationActive); }
Roket_PhysicsBody::Roket_PhysicsBody(NewtonCollision* collision, NewtonWorld* physworld, ePhysicsType type, int materialid) { body = NewtonCreateBody(physworld,collision); NewtonReleaseCollision(physworld,collision); NewtonBodySetUserData(body,this); NewtonBodySetTransformCallback(body,physics::TransformCallback); NewtonBodySetForceAndTorqueCallback(body,physics::ApplyForceAndTorqueCallback); affectedByGravity = true; objectExists = true; // apply initial force /*float omega[3]; omega[0] = 10.0f; omega[1] = 10.0f; omega[2] = 10.0f; NewtonBodySetOmega (body, &omega[0]);*/ /* float force[3]; force[0] = 0.0f; force[1] = -9.8f; force[2] = 0.0f; NewtonBodyAddForce( body, &force[0] ); */ world = physworld; }
void dCollisionBoxNodeInfo::CalculateInertiaGeometry (dScene* world, dVector& inertia, dVector& centerOfMass) const { NewtonWorld* newton = world->GetNewtonWorld(); NewtonCollision* box = NewtonCreateBox(newton, m_size.m_x, m_size.m_y, m_size.m_z, 0, &m_matrix[0][0]); CalculateGeometryProperies (box, inertia, centerOfMass); NewtonReleaseCollision (newton, box); }
void Newtonnode::initConvexHull(NewtonWorld *pWorld,const ion::video::Mesh& srcmesh,const float mass, const float Ixx,const float Iyy,const float Izz) { if (pWorld==0) { ion::base::log("Newtonnode::initConvexHull()",ion::base::Error) << "No NewtonWorld pointer given\n"; return; } if (!srcmesh.isValid()) { ion::base::log("Newtonnode::initConvexHull()",ion::base::Error) << "Source mesh is invalid\n"; return; } if (!srcmesh.vertexstream().isMapped()) { ion::base::log("Newtonnode::initConvexHull()",ion::base::Error) << "source mesh \"" << srcmesh.objIdentifier() << " is not mapped!\n"; return; } if ((m_pNewtonCollision!=0) && (m_pNewtonworld!=0)) NewtonReleaseCollision(m_pNewtonworld,m_pNewtonCollision); float *pPoints; { pPoints=new float[3*srcmesh.vertexstream().capacity()]; for (ion_uint32 v=0;v<srcmesh.vertexstream().capacity();++v) { const ion::math::Vector3f &rV=srcmesh.vertexstream().position(v); pPoints[v*3+0]=rV.x(); pPoints[v*3+1]=rV.y(); pPoints[v*3+2]=rV.z(); } } m_pNewtonworld=pWorld; m_pNewtonCollision=NewtonCreateConvexHull(m_pNewtonworld,srcmesh.vertexstream().capacity(),pPoints,12,0); m_pBody=NewtonCreateBody(m_pNewtonworld,m_pNewtonCollision); NewtonBodySetUserData(m_pBody,this); NewtonBodySetMassMatrix(m_pBody,mass,Ixx,Iyy,Izz); NewtonReleaseCollision(m_pNewtonworld,m_pNewtonCollision); NewtonBodySetTransformCallback (m_pBody, physicsSetTransform); NewtonBodySetForceAndTorqueCallback (m_pBody, physicsApplyForceAndTorque); delete [] pPoints; }
~ShatterEffect() { for (dListNode* node = GetFirst(); node; node = node->GetNext()) { ShatterAtom& atom = node->GetInfo(); NewtonReleaseCollision (m_world, atom.m_collision); atom.m_mesh->Release(); } }
ShatterEffect(NewtonWorld* const world, NewtonMesh* const mesh, int interiorMaterial) :dList<ShatterAtom>(), m_world (world) { // first we populate the bounding Box area with few random point to get some interior subdivisions. // the subdivision are local to the point placement, by placing these points visual ally with a 3d tool // and have precise control of how the debris are created. // the number of pieces is equal to the number of point inside the Mesh plus the number of point on the mesh dVector size; dMatrix matrix(GetIdentityMatrix()); NewtonMeshCalculateOOBB(mesh, &matrix[0][0], &size.m_x, &size.m_y, &size.m_z); // pepper the inside of the BBox box of the mesh with random points int count = 0; dVector points[NUMBER_OF_ITERNAL_PARTS + 1]; while (count < NUMBER_OF_ITERNAL_PARTS) { dFloat x = RandomVariable(size.m_x); dFloat y = RandomVariable(size.m_y); dFloat z = RandomVariable(size.m_z); if ((x <= size.m_x) && (x >= -size.m_x) && (y <= size.m_y) && (y >= -size.m_y) && (z <= size.m_z) && (z >= -size.m_z)){ points[count] = dVector (x, y, z); count ++; } } // create a texture matrix, for applying the material's UV to all internal faces dMatrix textureMatrix (GetIdentityMatrix()); textureMatrix[0][0] = 1.0f / size.m_x; textureMatrix[1][1] = 1.0f / size.m_y; // now we call create we decompose the mesh into several convex pieces NewtonMesh* const debriMeshPieces = NewtonMeshVoronoiDecomposition (mesh, count, sizeof (dVector), &points[0].m_x, interiorMaterial, &textureMatrix[0][0]); // Get the volume of the original mesh NewtonCollision* const collision = NewtonCreateConvexHullFromMesh (m_world, mesh, 0.0f, 0); dFloat volume = NewtonConvexCollisionCalculateVolume (collision); NewtonReleaseCollision(m_world, collision); // now we iterate over each pieces and for each one we create a visual entity and a rigid body NewtonMesh* nextDebri; for (NewtonMesh* debri = NewtonMeshCreateFirstLayer (debriMeshPieces); debri; debri = nextDebri) { nextDebri = NewtonMeshCreateNextLayer (debriMeshPieces, debri); NewtonCollision* const collision = NewtonCreateConvexHullFromMesh (m_world, debri, 0.0f, 0); if (collision) { ShatterAtom& atom = Append()->GetInfo(); atom.m_mesh = new DemoMesh(debri); atom.m_collision = collision; NewtonConvexCollisionCalculateInertialMatrix (atom.m_collision, &atom.m_momentOfInirtia[0], &atom.m_centerOfMass[0]); dFloat debriVolume = NewtonConvexCollisionCalculateVolume (atom.m_collision); atom.m_massFraction = debriVolume / volume; } NewtonMeshDestroy(debri); } NewtonMeshDestroy(debriMeshPieces); }
cCollideShapeNewton::~cCollideShapeNewton() { //Release Newton Collision if(mpNewtonCollision) NewtonReleaseCollision(mpNewtonWorld,mpNewtonCollision); //Release all subshapes (for compound objects) for(int i=0; i < (int)mvSubShapes.size(); i++) { mpWorld->DestroyShape(mvSubShapes[i]); } }
void Newtonnode::initCube(NewtonWorld *pWorld,const float xlength,const float ylength,const float zlength) { if (pWorld==0) { ion::base::log("Newtonnode::initCube()",ion::base::Error) << "No NewtonWorld pointer given\n"; return; } if ((m_pNewtonCollision!=0) && (m_pNewtonworld!=0)) NewtonReleaseCollision(m_pNewtonworld,m_pNewtonCollision); m_pNewtonworld=pWorld; m_pNewtonCollision=NewtonCreateBox(m_pNewtonworld,xlength,ylength,zlength,0); m_pBody=NewtonCreateBody(m_pNewtonworld,m_pNewtonCollision); NewtonBodySetUserData(m_pBody,this); NewtonBodySetMassMatrix(m_pBody, 1.0f, 1.0f, 1.0f, 1.0f); NewtonReleaseCollision(m_pNewtonworld,m_pNewtonCollision); NewtonBodySetTransformCallback (m_pBody, physicsSetTransform); NewtonBodySetForceAndTorqueCallback (m_pBody, physicsApplyForceAndTorque); }
CustomDGRayCastCar::~CustomDGRayCastCar () { NewtonWorld *world; world = NewtonBodyGetWorld (m_body0); for ( int i = 0; i < m_tiresCount; i ++ ) { NewtonReleaseCollision ( world, m_tires[i].m_shape ); } if (m_tires) { delete[] m_tires; } }
/* ============= CMod_PhysicsEndBSPCollisionTree ============= */ void CMod_PhysicsEndBSPCollisionTree() { dVector worldMin, worldMax; NewtonTreeCollisionEndBuild (g_collision, 1); dMatrix worldMatrix (GetIdentityMatrix()); g_rigidBody = NewtonCreateBody (g_world, g_collision); NewtonReleaseCollision (g_world, g_collision); NewtonBodySetMatrix (g_rigidBody, &worldMatrix[0][0]); NewtonCollisionCalculateAABB (g_collision, &worldMatrix[0][0], &worldMin[0], &worldMax[0]); NewtonSetWorldSize (g_world, &worldMin[0], &worldMax[0]); }
void xxxxx (NewtonWorld* const world) { NewtonCollision* const collision = NewtonCreateTreeCollision(world, 0); NewtonTreeCollisionBeginBuild(collision); float faces[] = { 32.000000, 26.000000, -1.000000, // Quad 0 32.000000, 26.000000, 0.000000, 32.000000, 25.000000, 0.000000, 32.000000, 25.000000, -1.000000}; NewtonTreeCollisionAddFace(collision, 4, &faces[0], 3 * sizeof (float), 0); NewtonTreeCollisionEndBuild(collision, 0); NewtonReleaseCollision(world, collision); }
/* ============= 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); }
//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ void PhysicsActor::setScale(float _x, float _y, float _z) { m_scaleX = _x; m_scaleY = _y; m_scaleZ = _z; #if 0 NewtonCollision* collision = createCollision(); NewtonBodySetCollision(m_pActor, collision); NewtonReleaseCollision(dynamic_cast<PhysicsZone*>(m_pZone.get())->getZonePtr(), collision); #else std::cout << "Warning: PhysicsActor::setScale() not implemented!" << std::endl; #endif }
void SimplePlaneCollision (NewtonFrame& system) { NewtonWorld* world; LevelPrimitive *level; NewtonCollision* planeColl; world = system.m_world; // create a material carrier to collision with this object int defaultMaterialID; defaultMaterialID = NewtonMaterialGetDefaultGroupID (world); // create the sky box and the floor, level = LoadLevelAndSceneRoot (system, "flatplane.dae", 1); // Find the world mesh and replace the collision for a custom plane collision mesh dMatrix matrix; NewtonBodyGetMatrix (level->m_level, &matrix[0][0]); dVector plane (matrix.m_up); plane.m_w = - (plane % matrix.m_posit); planeColl = CreatePlaneCollidion (world, plane); NewtonBodySetCollision(level->m_level, planeColl); NewtonReleaseCollision(world, planeColl); dVector posit (0.0f, 0.0f, 0.0f, 0.0f); posit.m_y = FindFloor (world, posit.m_x, posit.m_z) + 5.0f; InitEyePoint (dVector (1.0f, 0.0f, 0.0f), posit); dVector size (1.0f, 1.0f, 1.0f); dVector location (cameraEyepoint + cameraDir.Scale (10.0f)); AddBoxes (&system, 10.0f, location, size, 3, 3, 10.0f, _SPHERE_PRIMITIVE, defaultMaterialID); AddBoxes (&system, 10.0f, location, size, 3, 3, 10.0f, _BOX_PRIMITIVE, defaultMaterialID); AddBoxes (&system, 10.0f, location, size, 3, 3, 10.0f, _CONE_PRIMITIVE, defaultMaterialID); AddBoxes (&system, 10.0f, location, size, 3, 3, 10.0f, _CYLINDER_PRIMITIVE, defaultMaterialID); AddBoxes (&system, 10.0f, location, size, 3, 3, 10.0f, _CAPSULE_PRIMITIVE, defaultMaterialID); AddBoxes (&system, 10.0f, location, size, 3, 3, 10.0f, _CHAMFER_CYLINDER_PRIMITIVE, defaultMaterialID); AddBoxes (&system, 10.0f, location, size, 3, 3, 10.0f, _RANDOM_CONVEX_HULL_PRIMITIVE, defaultMaterialID); AddBoxes (&system, 10.0f, location, size, 3, 3, 10.0f, _REGULAR_CONVEX_HULL_PRIMITIVE, defaultMaterialID); }
void Car::initPhysics() { NewtonWorld * nWorld = this->controller->getWorld(); NewtonCollision* collision; float mass = 900.0f; vector3df v1 = this->carNode->getBoundingBox().MinEdge; vector3df v2 = this->carNode->getBoundingBox().MaxEdge; dVector minBox(v1.X, v1.Y, v1.Z); dVector maxBox(v2.X, v2.Y, v2.Z); dVector size(maxBox - minBox); dVector origin((maxBox + minBox).Scale(0.5f)); size.m_w = 1.0f; origin.m_w = 1.0f; dMatrix offset(GetIdentityMatrix()); offset.m_posit = origin; collision = NewtonCreateBox(nWorld, size.m_x, size.m_y, size.m_z, 0, &offset[0][0]); dVector inertia; matrix4 m = this->carNode->getRelativeTransformation(); NewtonConvexHullModifierSetMatrix(collision, m.pointer()); NewtonBody * body = NewtonCreateBody(nWorld, collision, m.pointer()); NewtonBodySetUserData(body, this); NewtonConvexCollisionCalculateInertialMatrix(collision, &inertia[0], &origin[0]); NewtonBodySetMassMatrix(body, mass, mass * inertia.m_x, mass * inertia.m_y, mass * inertia.m_z); NewtonBodySetCentreOfMass(body, &origin[0]); NewtonBodySetForceAndTorqueCallback(body, applyCarMoveForce); NewtonBodySetTransformCallback(body, applyCarTransform); int matId = NewtonMaterialGetDefaultGroupID(nWorld); NewtonMaterialSetCollisionCallback(nWorld, matId, matId, this, 0, applyCarCollisionForce); NewtonReleaseCollision(nWorld, collision); this->setCarBodyAndGravity(body, dVector(0,-10,0,0)); this->setLocalCoordinates(this->createChassisMatrix()); }
static void AddShatterPrimitive (DemoEntityManager* const scene, dFloat mass, const dVector& origin, const dVector& size, int xCount, int zCount, dFloat spacing, PrimitiveType type, int materialID) { dMatrix matrix (GetIdentityMatrix()); // create the shape and visual mesh as a common data to be re used NewtonWorld* const world = scene->GetNewton(); NewtonCollision* const collision = CreateConvexCollision (world, GetIdentityMatrix(), size, type, materialID); // create a newton mesh from the collision primitive NewtonMesh* const mesh = NewtonMeshCreateFromCollision(collision); // apply a material map int externalMaterial = LoadTexture("reljef.tga"); int internalMaterial = LoadTexture("KAMEN-stup.tga"); NewtonMeshApplyBoxMapping(mesh, externalMaterial, externalMaterial, externalMaterial); // create a newton mesh from the collision primitive ShatterEffect shatter (world, mesh, internalMaterial); DemoMesh* const visualMesh = new DemoMesh(mesh); 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; matrix.m_posit.m_y = FindFloor (world, x, z) + 4.0f; AddShatterEntity (scene, visualMesh, collision, shatter, matrix.m_posit); } } // do not forget to release the assets NewtonMeshDestroy (mesh); visualMesh->Release(); NewtonReleaseCollision(world, collision); }
/** * @brief * Constructor */ BodyTerrain::BodyTerrain(PLPhysics::World &cWorld, uint32 nWidth, uint32 nHeight, const float fTerrain[], const Vector3 &vBoxMin, const Vector3 &vBoxMax, const Vector3 &vScale) : PLPhysics::BodyTerrain(cWorld, static_cast<World&>(cWorld).CreateBodyImpl()), m_nWidth(nWidth), m_nHeight(nHeight), m_pfTerrain(fTerrain), m_vBoxMin(vBoxMin), m_vBoxMax(vBoxMax), m_vScale(vScale) { // Deactivate the physics simulation if required const bool bSimulationActive = cWorld.IsSimulationActive(); if (bSimulationActive) cWorld.SetSimulationActive(false); // Get the Newton physics world NewtonWorld *pNewtonWorld = static_cast<World&>(cWorld).GetNewtonWorld(); // Create collision primitive NewtonCollision *pCollision = NewtonCreateUserMeshCollision(pNewtonWorld, m_vBoxMin, m_vBoxMax, this, MeshCollisionCollideCallback, UserMeshCollisionRayHitCallback, nullptr, nullptr, nullptr, 0); // Create the rigid body // [TODO] Remove this as soon as there's an up-to-date Linux version of Newton Game Dynamics available! #if (NEWTON_MAJOR_VERSION == 2) && (NEWTON_MINOR_VERSION >= 28) Newton::NewtonBody *pNewtonBody = NewtonCreateBody(pNewtonWorld, pCollision, Matrix4x4::Identity); #else Newton::NewtonBody *pNewtonBody = NewtonCreateBody(pNewtonWorld, pCollision); #endif NewtonReleaseCollision(pNewtonWorld, pCollision); // Initialize the Newton physics body static_cast<BodyImpl&>(GetBodyImpl()).InitializeNewtonBody(*this, *pNewtonBody, 0.0f); // Reactivate the physics simulation if required if (bSimulationActive) cWorld.SetSimulationActive(bSimulationActive); }
NewtonBody* dRigidbodyNodeInfo::CreateNewtonBody (NewtonWorld* const world, dScene* const scene, dScene::dTreeNode* const myNode) const { // find the collision and crate a rigid body _ASSERTE (IsType (dRigidbodyNodeInfo::GetRttiType())); // attach the parent node as user data dScene::dTreeNode* parentNode = scene->FindParentByType(myNode, dSceneNodeInfo::GetRttiType()); dSceneNodeInfo* sceneInfo = (dSceneNodeInfo*) scene->GetInfoFromNode(parentNode); dScene::dTreeNode* shapeNode = scene->FindChildByType (myNode, dCollisionNodeInfo::GetRttiType()); _ASSERTE (shapeNode); dCollisionNodeInfo* collInfo = (dCollisionNodeInfo*) scene->GetInfoFromNode(shapeNode); dMatrix matrix (sceneInfo->CalculateOrthoMatrix()); NewtonCollision* collision = collInfo->CreateNewtonCollision (world, scene, shapeNode); NewtonBody* body = NewtonCreateBody(world, collision, &matrix[0][0]); NewtonReleaseCollision(world, collision); // NewtonBodySetMatrix(body, &matrix[0][0]); NewtonBodySetUserData(body, parentNode); NewtonBodySetCentreOfMass(body, &m_centerOfMass[0]); NewtonBodySetMassMatrix(body, m_massMatrix.m_w, m_massMatrix.m_x, m_massMatrix.m_y, m_massMatrix.m_z); NewtonBodySetVelocity(body, &m_velocity[0]); NewtonBodySetOmega(body, &m_omega[0]); //dVector internalDamp(rigidBody->GetI); //NewtonBodySetLinearDamping(body, internalDamp); //dVariable* bodyType = rigidBody->FindVariable("rigidBodyType"); //if (!bodyType || !strcmp (bodyType->GetString(), "default gravity")) { // NewtonBodySetTransformCallback(body, DemoEntity::SetTransformCallback); //} return body; }
//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ bool PhysicsActor::attachBody(pCollisionShape_type _collision) { // valid Newton collision primitives include the following: // Null, Box, Sphere (ovoid), Cone, Capsule, Cylinder, ChamferCylinder, // ConvexHull (not impl yet), CompoundCollision (not impl), // TreeCollision (not impl yet), UserMesh (heightfield) m_pActor = NewtonCreateBody(dynamic_cast<PhysicsZone*>(m_pZone.get())->getZonePtr(), dynamic_cast<CollisionShape*>(_collision.get())->getShapePtr()); NewtonReleaseCollision(dynamic_cast<PhysicsZone*>(m_pZone.get())->getZonePtr(), dynamic_cast<CollisionShape*>(_collision.get())->getShapePtr()); // set the transform call back function NewtonBodySetTransformCallback(m_pActor, TransformCallback); NewtonBodySetForceAndTorqueCallback(m_pActor, ApplyForceAndTorqueCallback); NewtonBodySetAutoactiveCallback(m_pActor, ActivationStateCallback); //NewtonBodySetFreezeTreshold(m_pActor, NewtonBodySetUserData(m_pActor, this); return true; }
void CreateScene (NewtonWorld* world, SceneManager* sceneManager) { Entity* floor; NewtonCollision* shape; NewtonBody* floorBody; 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); AddMaterilInteraction (materialManager, m_grass, m_bricks, &matInterations); matInterations.m_impactSound = sndManager->LoadSound ("metal.wav"); AddMaterilInteraction (materialManager, m_metal, m_bricks, &matInterations); AddMaterilInteraction (materialManager, m_grass, m_grass, &matInterations); // Create a large body to be the floor floor = sceneManager->CreateEntity(); floor->LoadMesh ("LevelMesh.dat"); // add static floor Physics int materialMap[] = {m_bricks, m_grass, m_wood, m_metal}; shape = CreateMeshCollision (world, floor, materialMap); floorBody = CreateRigidBody (world, floor, shape, 0.0f); NewtonReleaseCollision (world, shape); // set the Transformation Matrix for this rigid body dMatrix matrix (floor->m_curRotation, floor->m_curPosition); NewtonBodySetMatrix (floorBody, &matrix[0][0]); // 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]); // add some visual entities. dFloat y0 = FindFloor (world, 0.0f, 0.0f) + 10.0f; for (int i = 0; i < 5; i ++) { Entity* smilly; NewtonBody* smillyBody; smilly = sceneManager->CreateEntity(); smilly->LoadMesh ("Smilly.dat"); smilly->m_curPosition.m_y = y0; y0 += 2.0f; smilly->m_prevPosition = smilly->m_curPosition; // add a body with a box shape shape = CreateNewtonBox (world, smilly, m_metal); smillyBody = CreateRigidBody (world, smilly, shape, 10.0f); NewtonReleaseCollision (world, shape); } // add some visual entities. y0 = FindFloor (world, 0.0f, 0.4f) + 10.5f; for (int i = 0; i < 5; i ++) { Entity* frowny; NewtonBody* frownyBody; 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); NewtonReleaseCollision (world, shape); } y0 = FindFloor (world, 0.0f, 2.0f) + 10.5f; for (int i = 0; i < 5; i ++) { Entity* frowny; NewtonBody* frownyBody; frowny = sceneManager->CreateEntity(); frowny->LoadMesh ("dumb.dat"); frowny->m_curPosition.m_z = 2.0f; frowny->m_curPosition.m_y = y0; y0 += 2.0f; frowny->m_prevPosition = frowny->m_curPosition; // add a body with a Convex hull shape int materialMap[] = {m_grass, m_wood, m_metal, m_metal}; shape = CreateNewtonCompoundFromEntitySubmesh (world, frowny, materialMap); frownyBody = CreateRigidBody (world, frowny, shape, 10.0f); NewtonReleaseCollision (world, shape); } // set the Camera EyePoint close to the scene action InitCamera (dVector (-15.0f, FindFloor (world, -15.0f, 0.0f) + 5.0f, 0.0f), dVector (1.0f, 0.0f, 0.0f)); }
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); }
void CreateScene (NewtonWorld* world, SceneManager* sceneManager) { Entity* floor; Entity* smilly; Entity* frowny; NewtonBody* floorBody; NewtonBody* smillyBody; NewtonBody* frownyBody; NewtonCollision* shape; // Create the material for this scene CreateMateials (world, sceneManager); // Create a large body to be the floor floor = sceneManager->CreateEntity(); // add scene collision from a level mesh shape = CreateHeightFieldCollision (world, "h2.raw", 0); floorBody = CreateRigidBody (world, floor, shape, 0.0f); NewtonReleaseCollision (world, 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]); // now we will use the properties of this body to set a proper world size. NewtonCollisionCalculateAABB (shape, &matrix[0][0], &boxP0.m_x, &boxP1.m_x); // add some extra padding boxP0.m_x -= 50.0f; boxP0.m_y -= 500.0f; boxP0.m_z -= 50.0f; boxP1.m_x += 50.0f; boxP1.m_y += 500.0f; boxP1.m_z += 50.0f; // set the new world size NewtonSetWorldSize (world, &boxP0[0], &boxP1[0]); // assign an Material ID to this body NewtonBodySetMaterialGroupID (floorBody, g_floorMaterial); // add some visual entities. dFloat y0 = FindFloor (world, 0.0f, 0.0f) + 10.0f; for (int i = 0; i < 5; i ++) { smilly = sceneManager->CreateEntity(); smilly->LoadMesh ("Smilly.dat"); smilly->m_curPosition.m_y = y0; y0 += 2.0f; smilly->m_prevPosition = smilly->m_curPosition; // add a body with a box shape shape = CreateNewtonBox (world, smilly, 0); smillyBody = CreateRigidBody (world, smilly, shape, 10.0f); NewtonReleaseCollision (world, shape); // assign an Material ID to this body NewtonBodySetMaterialGroupID (smillyBody, g_metalMaterial); } // add some visual entities. y0 = FindFloor (world, 0.0f, 0.4f) + 10.5f; for (int i = 0; i < 5; i ++) { 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, 0); frownyBody = CreateRigidBody (world, frowny, shape, 10.0f); NewtonReleaseCollision (world, shape); // assign an Material ID to this body NewtonBodySetMaterialGroupID (frownyBody, g_woodMaterial); } // set the Camera EyePoint close to the scene action InitCamera (dVector (-15.0f, FindFloor (world, -15.0f, 0.0f) + 5.0f, 0.0f), dVector (1.0f, 0.0f, 0.0f)); }
void CreateScene (NewtonWorld* world, SceneManager* sceneManager) { Entity* floor; Entity* smilly; Entity* frowny; NewtonBody* floorBody; NewtonBody* smillyBody; NewtonBody* frownyBody; NewtonCollision* shape; //////////////////////////////////////////////////////////////////////////////// // STATIC BODY 1 - FLOOR //////////////////////////////////////////////////////////////////////////////// // Create a large body to be the floor floor = sceneManager->CreateEntity(); floor->LoadMesh ("FloorBox.dat"); // add static floor Physics shape = CreateNewtonBox (world, floor, 0); floorBody = CreateRigidBody (world, floor, shape, 0.0f); NewtonReleaseCollision (world, shape); // set the Transformation Matrix for this rigid body dMatrix matrix (floor->m_curRotation, floor->m_curPosition); NewtonBodySetMatrix (floorBody, &matrix[0][0]); // 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]); //////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// // DYNAMIC BODY 1 //////////////////////////////////////////////////////////////////////////////// // add some visual entities. smilly = sceneManager->CreateEntity(); smilly->LoadMesh ("Smilly.dat"); smilly->m_curPosition.m_y = 10.0f; smilly->m_prevPosition = smilly->m_curPosition; // add a body with a box shape shape = CreateNewtonBox (world, smilly, 0); smillyBody = CreateRigidBody (world, smilly, shape, 10.0f); NewtonReleaseCollision (world, shape); //////////////////////////////////////////////////////////////////////////////// // DYNAMIC BODY 2 //////////////////////////////////////////////////////////////////////////////// // add some visual entities. frowny = sceneManager->CreateEntity(); frowny->LoadMesh ("Frowny.dat"); frowny->m_curPosition.m_z = 0.4f; frowny->m_curPosition.m_y = 10.0f + 0.4f; frowny->m_prevPosition = frowny->m_curPosition; // add a body with a Convex hull shape shape = CreateNewtonConvex (world, frowny, 0); frownyBody = CreateRigidBody (world, frowny, shape, 10.0f); NewtonReleaseCollision (world, shape); }