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); }
DemoEntity::DemoEntity(DemoEntityManager& world, const dScene* scene, dScene::dTreeNode* rootSceneNode, dTree<DemoMesh*, dScene::dTreeNode*>& meshCache, DemoEntityManager::EntityDictionary& entityDictionary, DemoEntity* parent) :dClassInfo() ,dHierarchy<DemoEntity>() ,m_matrix(GetIdentityMatrix()) ,m_curPosition (0.0f, 0.0f, 0.0f, 1.0f) ,m_nextPosition (0.0f, 0.0f, 0.0f, 1.0f) ,m_curRotation (1.0f, 0.0f, 0.0f, 0.0f) ,m_nextRotation (1.0f, 0.0f, 0.0f, 0.0f) ,m_lock (0) ,m_mesh (NULL) { // add this entity to the dictionary entityDictionary.Insert(this, rootSceneNode); // if this is a child mesh set it as child of th entity dMatrix parentMatrix (GetIdentityMatrix()); if (parent) { Attach (parent); dScene::dTreeNode* parentNode = scene->FindParentByType(rootSceneNode, dSceneNodeInfo::GetRttiType()); dSceneNodeInfo* parentInfo = (dSceneNodeInfo*) parentNode; parentMatrix = parentInfo->GetTransform(); } dSceneNodeInfo* info = (dSceneNodeInfo*) scene->GetInfoFromNode (rootSceneNode); // SetMatrix(info->GetTransform() * parentMatrix.Inverse4x4()); dMatrix matrix (info->GetTransform() * parentMatrix.Inverse4x4()); dQuaternion rot (matrix); // set the matrix twice in oder to get cur and next position SetMatrix(world, rot, matrix.m_posit); SetMatrix(world, rot, matrix.m_posit); // if this node has a mesh, find it and attach it to this entity dScene::dTreeNode* meshNode = scene->FindChildByType(rootSceneNode, dMeshNodeInfo::GetRttiType()); if (meshNode) { DemoMesh* mesh = meshCache.Find(meshNode)->GetInfo(); SetMesh(mesh); } // add all of the children nodes as child nodes for (void* child = scene->GetFirstChild(rootSceneNode); child; child = scene->GetNextChild (rootSceneNode, child)) { dScene::dTreeNode* node = scene->GetNodeFromLink(child); dNodeInfo* info = scene->GetInfoFromNode(node); if (info->GetTypeId() == dSceneNodeInfo::GetRttiType()) { new DemoEntity (world, scene, node, meshCache, entityDictionary, this); } } }
long NewtonDemos::onLoad(FXObject* sender, FXSelector id, void* eventPtr) { BEGIN_MENU_OPTION(); const FXchar patterns[]="Newton Dynamics Files (*.ngd)"; FXFileDialog open(this,"Load Newton Dynamics scene"); open.setPatternList(patterns); open.setDirectory ("../../../media"); if(open.execute()){ m_scene->Cleanup(); // load the scene from a ngd file format m_scene->makeCurrent(); m_scene->LoadScene (open.getFilename().text()); m_scene->makeNonCurrent(); // add a sky box to the scene, make the first object m_scene->Addtop (new SkyBox()); // place camera into position dMatrix camMatrix (GetIdentityMatrix()); // camMatrix.m_posit = dVector (-40.0f, 10.0f, 0.0f, 0.0f); camMatrix = dYawMatrix(-0.0f * 3.1416f / 180.0f); camMatrix.m_posit = dVector (-5.0f, 1.0f, -0.0f, 0.0f); m_scene->SetCameraMatrix(camMatrix, camMatrix.m_posit); RestoreSettings (); } m_scene->ResetTimer(); END_MENU_OPTION(); return 1; }
XXXX () { dMatrix s; dFloat m = 2.0f; for (int i = 0; i < 3; i ++) { for (int j = 0; j < 3; j ++) { s[i][j] = m; m += (i + 1) + j; } } s.m_posit = dVector (1, 2, 3, 1); dMatrix matrix; dVector scale; dMatrix stretch; s.PolarDecomposition (matrix, scale, stretch); dMatrix s1 (matrix, scale, stretch); dMatrix xxx (dPitchMatrix (30.0f * 3.14159f / 180.0f) * dRollMatrix (30.0f * 3.14159f / 180.0f)); dMatrix xxxx (GetIdentityMatrix()); xxx[0] = xxx[0].Scale (-1.0f); dFloat mmm = (xxx[0] * xxx[1]) % xxx[2]; xxxx[0][0] = 3.0f; xxxx[1][1] = 3.0f; xxxx[2][2] = 4.0f; dMatrix xxx2 (xxx * xxxx); mmm = (xxx2[0] * xxx2[1]) % xxx2[2]; xxx2.PolarDecomposition (matrix, scale, stretch); s1 = dMatrix (matrix, scale, stretch); s1 = dMatrix (matrix, scale, stretch); }
void CustomDryRollingFriction::GetInfo (NewtonJointRecord* info) const { strcpy (info->m_descriptionType, "dryRollingFriction"); info->m_attachBody_0 = m_body0; info->m_attachBody_1 = m_body1; info->m_minLinearDof[0] = -1.0e10f; info->m_maxLinearDof[0] = 1.0e10f; info->m_minLinearDof[1] = -1.0e10f; info->m_maxLinearDof[1] = 1.0e10f; info->m_minLinearDof[2] = -1.0e10f; info->m_maxLinearDof[2] = 1.0e10f; info->m_minAngularDof[0] = -1.0e10f; info->m_maxAngularDof[0] = 1.0e10f; info->m_minAngularDof[1] = -1.0e10f;; info->m_maxAngularDof[1] = 1.0e10f; info->m_minAngularDof[2] = -1.0e10f;; info->m_maxAngularDof[2] = 1.0e10f; dMatrix matrix (GetIdentityMatrix()); memcpy (info->m_attachmenMatrix_0, &matrix[0][0], sizeof (dMatrix)); // note this is not a bug memcpy (info->m_attachmenMatrix_1, &matrix[0][0], sizeof (dMatrix)); }
NewtonCollision* CreateNewtonConvex (NewtonWorld* world, Entity *ent, int shapeId) { dVector minBox; dVector maxBox; NewtonCollision* collision; dVector* tmpArray = new dVector [ent->m_vertexCount]; // Get the Bounding Box for this entity ent->GetBBox (minBox, maxBox); dVector size (maxBox - minBox); dVector origin ((maxBox + minBox).Scale (0.5f)); size.m_w = 1.0f; origin.m_w = 1.0f; // Translate al the points to the origin point for (int i = 0; i < ent->m_vertexCount; i ++) { dVector tmp (ent->m_vertex[i * 3 + 0], ent->m_vertex[i * 3 + 1], ent->m_vertex[i * 3 + 2], 0.0f); tmpArray[i] = tmp - origin; } // make and offset Matrix for this collision shape. dMatrix offset (GetIdentityMatrix()); offset.m_posit = origin; // now create a convex hull shape from the vertex geometry collision = NewtonCreateConvexHull(world, ent->m_vertexCount, &tmpArray[0][0], sizeof (dVector), 0.1f, shapeId, &offset[0][0]); delete tmpArray; return collision; }
void dMeshNodeInfo::BakeTransform (const dMatrix& transform) { dVector scale; dMatrix stretchMatrix; // dMatrix matrix (m_matrix * transform); // matrix.PolarDecomposition (m_matrix, scale, stretchMatrix); // matrix = dMatrix (GetIdentityMatrix(), scale, stretchMatrix); dMatrix tmp (m_matrix); dMatrix matrix (transform.Inverse4x4() * m_matrix * transform); matrix.PolarDecomposition (m_matrix, scale, stretchMatrix); matrix = transform * dMatrix (GetIdentityMatrix(), scale, stretchMatrix); int pointCount = NewtonMeshGetPointCount (m_mesh); int pointStride = NewtonMeshGetPointStrideInByte (m_mesh) / sizeof (dFloat); dFloat* const points = NewtonMeshGetPointArray (m_mesh); matrix.TransformTriplex(points, pointStride * sizeof (dFloat), points, pointStride * sizeof (dFloat), pointCount); dFloat* const normals = NewtonMeshGetNormalArray(m_mesh); dMatrix rotation (matrix.Inverse4x4().Transpose() * matrix); rotation.m_posit = dVector (0.0f, 0.0f, 0.0f, 1.0f); rotation.TransformTriplex(normals, pointStride * sizeof (dFloat), normals, pointStride * sizeof (dFloat), pointCount); int vertexCount = NewtonMeshGetVertexCount (m_mesh); int vertexStride = NewtonMeshGetVertexStrideInByte (m_mesh) / sizeof (dFloat); dFloat* const vertex = NewtonMeshGetVertexArray (m_mesh); matrix.TransformTriplex(vertex, vertexStride * sizeof (dFloat), vertex, vertexStride * sizeof (dFloat), vertexCount); }
void GetTranslationMatrix(M4* result, V3 p) { GetIdentityMatrix(result); result->data[12] = p.x; result->data[13] = p.y; result->data[14] = p.z; }
CustomUniversal::CustomUniversal(const dMatrix& pinsAndPivoFrame, NewtonBody* child, NewtonBody* parent) :NewtonCustomJoint(6, child, parent) { dMatrix matrix0; m_limit_0_On = true; m_angularMotor_0_On = false; m_angularDamp_0 = 0.5f; m_angularAccel_0 = -4.0f; m_minAngle_0 = -45.0f * 3.141592f / 180.0f; m_maxAngle_0 = 45.0f * 3.141592f / 180.0f; m_limit_1_On = true; m_angularMotor_1_On = false; m_angularDamp_1 = 0.3f; m_angularAccel_1 = -4.0f; m_minAngle_1 = -45.0f * 3.141592f / 180.0f; m_maxAngle_1 = 45.0f * 3.141592f / 180.0f; // Get the global matrices of each rigid body. NewtonBodyGetMatrix(m_body0, &matrix0[0][0]); dMatrix matrix1 (GetIdentityMatrix()); if (m_body1) { NewtonBodyGetMatrix(m_body1, &matrix1[0][0]); } // calculate the relative matrix of the pin and pivot on each body m_localMatrix0 = pinsAndPivoFrame * matrix0.Inverse(); m_localMatrix1 = pinsAndPivoFrame * matrix1.Inverse(); }
dMatrix DemoEntity::CalculateGlobalMatrix (const DemoEntity* const root) const { dMatrix matrix (GetIdentityMatrix()); for (const DemoEntity* ptr = this; ptr != root; ptr = ptr->GetParent()) { matrix = matrix * GetCurrentMatrix (); } return matrix; }
static void LoadDestructionCutterMesh (const NewtonWorld* world) { GLuint tex; dFloat width; dFloat breadth; dMatrix plane (GetIdentityMatrix()); dMatrix texMatrix (GetIdentityMatrix()); width = 10.0f; breadth = 10.0f; tex = LoadTexture ("destructionDetail.tga"); texMatrix[1][1] = 4.0f /breadth; texMatrix[2][2] = 4.0f /breadth; meshClipper = NewtonMeshCreatePlane (world, &plane[0][0], width, breadth, tex, &texMatrix[0][0], &texMatrix[0][0]); }
void GetRotationMatrix(M4* result, V3 r) { M4 x; M4 y; M4 zy; M4 z; GetIdentityMatrix(result); GetIdentityMatrix(&x); GetIdentityMatrix(&y); GetIdentityMatrix(&z); RotateX(&x, r.x); RotateY(&y, r.y); MatrixMult(&zy, z, y); MatrixMult(result, x, zy); }
static void AddShatterEntity (DemoEntityManager* const scene, DemoMesh* const visualMesh, NewtonCollision* const collision, const ShatterEffect& shatterEffect, dVector location) { dQuaternion rotation; SimpleShatterEffectEntity* const entity = new SimpleShatterEffectEntity (visualMesh, shatterEffect); entity->SetMatrix(*scene, rotation, location); entity->InterpolateMatrix (*scene, 1.0f); scene->Append(entity); dVector origin; dVector inertia; NewtonConvexCollisionCalculateInertialMatrix (collision, &inertia[0], &origin[0]); float mass = 10.0f; int materialId = 0; dFloat Ixx = mass * inertia[0]; dFloat Iyy = mass * inertia[1]; dFloat Izz = mass * inertia[2]; //create the rigid body dMatrix matrix (GetIdentityMatrix()); matrix.m_posit = location; NewtonWorld* const world = scene->GetNewton(); NewtonBody* const rigidBody = NewtonCreateBody (world, collision, &matrix[0][0]); entity->m_myBody = rigidBody; entity->m_myweight = dAbs (mass * DEMO_GRAVITY); // set the correct center of gravity for this body NewtonBodySetCentreOfMass (rigidBody, &origin[0]); // set the mass matrix NewtonBodySetMassMatrix (rigidBody, mass, Ixx, Iyy, Izz); // activate // NewtonBodyCoriolisForcesMode (blockBoxBody, 1); // save the pointer to the graphic object with the body. NewtonBodySetUserData (rigidBody, entity); // assign the wood id NewtonBodySetMaterialGroupID (rigidBody, materialId); // set continue collision mode // NewtonBodySetContinuousCollisionMode (rigidBody, continueCollisionMode); // set a destructor for this rigid body NewtonBodySetDestructorCallback (rigidBody, PhysicsBodyDestructor); // set the transform call back function NewtonBodySetTransformCallback (rigidBody, DemoEntity::SetTransformCallback); // set the force and torque call back function NewtonBodySetForceAndTorqueCallback (rigidBody, PhysicsApplyGravityForce); }
dBone::dBone(dBone* parent) :dHierarchy<dBone>(), m_localMatrix(GetIdentityMatrix()) { if (parent) { Attach (parent); } m_boneID = -1; }
dBone::dBone(dBone* parent) :dClassInfo(), dHierarchy<dBone>(), m_localMatrix(GetIdentityMatrix()) { if (parent) { Attach (parent); } m_boneID = -1; SetType (m_sceneNode); }
void ShowMousePicking (const dVector& p0, const dVector& p1) { dFloat radius = 0.25f; // set the color of the cube's surface GLfloat cubeColor[] = { 1.0f, 1.0f, 1.0f, 1.0 }; glMaterialfv(GL_FRONT, GL_SPECULAR, cubeColor); glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, cubeColor); glMaterialf(GL_FRONT, GL_SHININESS, 50.0); // set up the cube's texture glDisable(GL_TEXTURE_2D); GLUquadricObj *pObj; glPushMatrix(); dMatrix matrix (GetIdentityMatrix()); matrix.m_posit = p0; glMultMatrix(&matrix[0][0]); // Get a new Quadric off the stack pObj = gluNewQuadric(); gluQuadricTexture(pObj, true); gluSphere(pObj, radius, 10, 10); glPopMatrix(); glPushMatrix(); matrix.m_posit = p1; glMultMatrix(&matrix[0][0]); gluSphere(pObj, radius, 10, 10); gluDeleteQuadric(pObj); glPopMatrix(); dVector dir (p1 - p0); dFloat lenght (dir % dir); if (lenght > 1.0e-2f) { glPushMatrix(); lenght = sqrt (lenght); dMatrix align (dgYawMatrix(0.5f * 3.1426f)); align.m_posit.m_x = -lenght * 0.5f; matrix = align * dgGrammSchmidt(dir); matrix.m_posit += (p1 + p0).Scale (0.5f); glMultMatrix(&matrix[0][0]); // Get a new Quadric off the stack pObj = gluNewQuadric(); gluCylinder(pObj, radius * 0.5f, radius * 0.5f, lenght, 10, 2); gluDeleteQuadric(pObj); glPopMatrix(); } }
Import::Import(const char* pathName, Interface* ip, ImpInterface* impip) { // set the path for textures char* ptr = NULL; sprintf (m_path, "%s", pathName); for (int i = 0; m_path[i]; i ++) { if ((m_path[i] == '\\') || (m_path[i] == '/') ) { ptr = &m_path[i]; } } *ptr = 0; m_ip = ip; m_impip = impip; m_succes = TRUE; MaterialCache materialCache (NewDefaultMultiMtl()); SetSceneParameters(); dFloat scale; scale = float (GetMasterScale(UNITS_METERS)); dMatrix scaleMatrix (GetIdentityMatrix()); scaleMatrix[0][0] = 1.0f / scale; scaleMatrix[1][1] = 1.0f / scale; scaleMatrix[2][2] = 1.0f / scale; dMatrix globalRotation (scaleMatrix * dPitchMatrix(3.14159265f * 0.5f) * dRollMatrix(3.14159265f * 0.5f)); NewtonWorld* newton = NewtonCreate(); dScene scene (newton); scene.Deserialize (pathName); // dScene::Iterator iter (scene); // for (iter.Begin(); iter; iter ++) { // dScene::dTreeNode* node = iter.GetNode(); // dNodeInfo* info = scene.GetInfoFromNode(node); // if (info->GetTypeId() == dMeshNodeInfo::GetRttiType()) { // dMeshNodeInfo* mesh = (dMeshNodeInfo*) scene.GetInfoFromNode(node); // mesh->ConvertToTriangles(); // } // } scene.BakeTransform (globalRotation); GeometryCache geometryCache; MaxNodeChache maxNodeCache; LoadMaterials (scene, materialCache); LoadGeometries (scene, geometryCache, materialCache); LoadNodes (scene, geometryCache, materialCache.m_multiMat, maxNodeCache); ApplyModifiers (scene, maxNodeCache); scene.CleanUp(); NewtonDestroy(newton); }
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); }
DemoEntity::DemoEntity(DemoEntity* parent) :dClassInfo() ,dHierarchy<DemoEntity>() ,m_matrix(GetIdentityMatrix()) ,m_curPosition (0.0f, 0.0f, 0.0f, 1.0f) ,m_nextPosition (0.0f, 0.0f, 0.0f, 1.0f) ,m_curRotation (1.0f, 0.0f, 0.0f, 0.0f) ,m_nextRotation (1.0f, 0.0f, 0.0f, 0.0f) ,m_lock(0) ,m_mesh (NULL) { }
void NewtonCustomJoint::CalculateGlobalMatrix (const dMatrix& localMatrix0, const dMatrix& localMatrix1, dMatrix& matrix0, dMatrix& matrix1) const { dMatrix body0Matrix; // Get the global matrices of each rigid body. NewtonBodyGetMatrix(m_body0, &body0Matrix[0][0]); dMatrix body1Matrix (GetIdentityMatrix()); if (m_body1) { NewtonBodyGetMatrix(m_body1, &body1Matrix[0][0]); } matrix0 = localMatrix0 * body0Matrix; matrix1 = localMatrix1 * body1Matrix; }
/* ============= 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]); }
static void Magnets (NewtonFrame& system, dFloat strength) { NewtonWorld* world; LevelPrimitive* scene; world = system.m_world; // create the sky box and the floor, scene = LoadLevelAndSceneRoot (system, "playground.dae", 1); dVector posit (0.0f, 0.0f, 0.0f, 1.0f); posit.m_y = FindFloor (world, posit.m_x, posit.m_z) + 5.0f; InitEyePoint (dVector (1.0f, 0.0f, 0.0f), posit); // create a material carrier to colliding with this objects int defaultMaterialID; int magneticFieldID; int magneticPicesID; defaultMaterialID = NewtonMaterialGetDefaultGroupID (world); magneticFieldID = NewtonMaterialCreateGroupID (world); magneticPicesID = NewtonMaterialCreateGroupID (world); NewtonMaterialSetCollisionCallback (world, magneticPicesID, magneticFieldID, NULL, NULL, Magnet::MagneticField); // create a spherical object to serve are the magnet core dMatrix matrix (GetIdentityMatrix()); matrix.m_posit = posit; matrix.m_posit.m_x += 7.0f; new Magnet (system, matrix, 20.0f, defaultMaterialID, magneticFieldID, strength); // add a material to control the buoyancy dVector size (1.0f, 0.25f, 0.5f); dVector sphSize (1.0f, 1.0f, 1.0f); dVector capSize (1.25f, 0.4f, 1.0f); dVector location (cameraEyepoint + cameraDir.Scale (10.0f)); AddBoxes (&system, 1.0f, location, sphSize, 3, 3, 5.0f, _SPHERE_PRIMITIVE, magneticPicesID); AddBoxes (&system, 1.0f, location, size, 3, 3, 5.0f, _BOX_PRIMITIVE, magneticPicesID); AddBoxes (&system, 1.0f, location, size, 3, 3, 5.0f, _CONE_PRIMITIVE, magneticPicesID); AddBoxes (&system, 1.0f, location, size, 3, 3, 5.0f, _CYLINDER_PRIMITIVE, magneticPicesID); AddBoxes (&system, 1.0f, location, capSize, 3, 3, 5.0f, _CAPSULE_PRIMITIVE, magneticPicesID); AddBoxes (&system, 1.0f, location, size, 3, 3, 5.0f, _CHAMFER_CYLINDER_PRIMITIVE, magneticPicesID); AddBoxes (&system, 1.0f, location, size, 3, 3, 5.0f, _RANDOM_CONVEX_HULL_PRIMITIVE, magneticPicesID); AddBoxes (&system, 1.0f, location, size, 3, 3, 5.0f, _REGULAR_CONVEX_HULL_PRIMITIVE, magneticPicesID); posit.m_x -= 10.0f; InitEyePoint (dVector (1.0f, 0.0f, 0.0f), posit); }
dMatrix dBone::CalcGlobalMatrix (const dBone* root) const { dBone* ptr; if (root != this) { dMatrix matrix (m_localMatrix); for (ptr = GetParent(); ptr != root; ptr = ptr->GetParent()) { matrix = matrix * ptr->m_localMatrix; } return matrix; } else { return GetIdentityMatrix(); } }
void RigidBodyData::CreateBody(NewtonCollision* const collision, const dVector& veloc, const dVector& omega) { _ASSERTE (!m_body); RigidBodyWorldDesc& me = *(RigidBodyWorldDesc*) RigidBodyWorldDesc::GetDescriptor(); dMatrix matrix (GetIdentityMatrix()); m_body = NewtonCreateDynamicBody(me.m_newton, collision, &matrix[0][0]); //NewtonBodySetMassMatrix(m_body, m_mass, m_mass * m_inertia.m_x, m_mass * m_inertia.m_y, m_mass * m_inertia.m_z); NewtonBodySetMassProperties(m_body, m_mass, collision); NewtonBodySetCentreOfMass(m_body, &m_origin[0]); NewtonBodySetVelocity(m_body, &veloc[0]); NewtonBodySetOmega(m_body, &omega[0]); NewtonBodySetForceAndTorqueCallback(m_body, RigidBodyController::ApplyGravityForce); }
/* ============= 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); }
static void MagneticFieldNoForce (const NewtonBody* body, dFloat timestep, int threadIndex) { dMatrix matrix; dVector zero (0.0f, 0.0f, 0.0f, 0.0f); NewtonBodySetForce (body, &zero[0]); NewtonBodySetTorque (body, &zero[0]); NewtonBodySetOmega (body, &zero[0]); NewtonBodySetVelocity (body, &zero[0]); // telepor the magnetic field trigger to the center of the core Magnet* magnet; magnet = (Magnet*)NewtonBodyGetUserData(body); NewtonBodyGetMatrix (magnet->m_magneticCore, &matrix[0][0]); dMatrix posit (GetIdentityMatrix()); posit.m_posit = matrix.m_posit; NewtonBodySetMatrix (body, &posit[0][0]); }
void RWeaponTracks::Render() { Update(); LPDIRECT3DDEVICE9 dev = RGetDevice(); if(m_current_node_size > 1) { MakeBuffer(); static D3DXMATRIX _init_mat = GetIdentityMatrix(); dev->SetTransform( D3DTS_WORLD, &_init_mat ); dev->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE ); dev->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE ); dev->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG2 ); dev->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG2 ); dev->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE ); dev->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE); dev->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA ); dev->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA ); dev->SetRenderState( D3DRS_LIGHTING, FALSE ); dev->SetRenderState( D3DRS_ZWRITEENABLE, FALSE ); dev->SetTexture(0, NULL); dev->SetFVF( RLVertexType ); if(m_bSpline) dev->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, m_current_spline_vertex_size-2 ,(LPVOID)m_pVertSpline, sizeof(RLVertex)); else dev->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, m_current_vertex_size-2 ,(LPVOID)m_pVert, sizeof(RLVertex)); dev->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW ); dev->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE); dev->SetRenderState( D3DRS_ZWRITEENABLE, TRUE ); dev->SetRenderState( D3DRS_LIGHTING, TRUE ); } }
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()); }
void NewtonCustomJoint::CalculateLocalMatrix (const dVector& pivot, const dVector& dir, dMatrix& localMatrix0, dMatrix& localMatrix1) const { dMatrix matrix0; // Get the global matrices of each rigid body. NewtonBodyGetMatrix(m_body0, &matrix0[0][0]); dMatrix matrix1 (GetIdentityMatrix()); if (m_body1) { NewtonBodyGetMatrix(m_body1, &matrix1[0][0]); } // create a global matrix at the pivot point with front vector aligned to the pin vector dMatrix pinAndPivotMatrix (dgGrammSchmidt(dir)); pinAndPivotMatrix.m_posit = pivot; pinAndPivotMatrix.m_posit.m_w = 1.0f; // calculate the relative matrix of the pin and pivot on each body localMatrix0 = pinAndPivotMatrix * matrix0.Inverse(); localMatrix1 = pinAndPivotMatrix * matrix1.Inverse(); }
static rmatrix MakeSaneWorldMatrix(const v3& pos, v3 dir, v3 up) { #ifdef _DEBUG if (!IS_EQ(MagnitudeSq(dir), 1)) { DMLog("dir %f\n", Magnitude(dir)); if (IS_EQ(MagnitudeSq(dir), 0)) dir = { 1, 0, 0 }; else Normalize(dir); } if (!IS_EQ(MagnitudeSq(up), 1)) { DMLog("up %f\n", Magnitude(up)); if (IS_EQ(MagnitudeSq(up), 0)) up = { 1, 0, 0 }; else Normalize(up); } #endif auto mat = GetIdentityMatrix(); auto right = Normalized(CrossProduct(dir, up)); up = Normalized(CrossProduct(right, dir)); v3 basis[] = { right, dir, up }; for (size_t i{}; i < 3; ++i) for (size_t j{}; j < 3; ++j) mat.m[i][j] = basis[i][j]; SetTransPos(mat, pos); return mat; }