private SimulationObject[] getSimulationObjects() { SimulationObject[] objects = new SimulationObject[1]; #region Terrain Base objects[0] = new SimulationObject( ObjectType.StaticRigidBody, geometry: GetObjectGeometry("cube.obj", 25), mass: 1000.0, position: new Vector3(0.0, -4.0, 0.0), rotationStatus: new Quaternion(new Vector3(0.0, 0.0, 0.0), 0.0)); objects[0].SetLinearVelocity(new Vector3(0.0, 0.0, 0.0)); objects[0].SetAngularVelocity(new Vector3(0.0, 0.0, 0.0)); objects[0].SetRestitutionCoeff(0.1); objects[0].SetDynamicFrictionCoeff(1.0); objects[0].SetStaticFrictionCoeff(1.0); objects[0].SetExcludeFromCollisionDetection(false); Vector3[] vertexPosition = Array.ConvertAll(objects[0].ObjectGeometry[0].VertexPosition, item => item.Vertex); var inertiaTensor = new InertiaTensor( vertexPosition, objects[0].ObjectGeometry[0].Triangle, objects[0].Mass); //Traslo per normalizzare l'oggetto rispetto al suo centro di massa for (int j = 0; j < objects[0].ObjectGeometry[0].VertexPosition.Length; j++) { objects[0].ObjectGeometry[0].SetVertexPosition( vertexPosition[j] - inertiaTensor.GetMassCenter(), j); } var inertiaTensor1 = new InertiaTensor( vertexPosition, objects[0].ObjectGeometry[0].Triangle, objects[0].Mass); objects[0].SetStartPosition(inertiaTensor1.GetMassCenter()); objects[0].SetBaseInertiaTensor(inertiaTensor1.GetInertiaTensor()); objects[0].SetRotationMatrix(Quaternion.ConvertToMatrix(Quaternion.Normalize(objects[0].RotationStatus))); objects[0].SetInertiaTensor((objects[0].RotationMatrix * objects[0].BaseInertiaTensor) * Matrix3x3.Transpose(objects[0].RotationMatrix)); for (int j = 0; j < objects[0].ObjectGeometry[0].VertexPosition.Length; j++) { Vector3 relPositionRotate = objects[0].RotationMatrix * objects[0].RelativePositions[j]; objects[0].ObjectGeometry[0].SetVertexPosition(objects[0].Position + relPositionRotate, j); } #endregion return objects; }
private void SetObjectProperties() { Vector3 startPosition = new Vector3(); Matrix3x3 baseTensors = new Matrix3x3(); int totalVertex = 0; for (int i = 0; i < ObjectGeometry.Length; i++) { Vector3[] vertexPosition = Array.ConvertAll( ObjectGeometry[i].VertexPosition, item => item.Vertex + StartCompositePositionObjects[i]); //TODO da rivedere var inertiaTensor = new InertiaTensor( vertexPosition, ObjectGeometry[i].Triangle, PartialMass[i]); var normalizedInertiaTensor = inertiaTensor; //Traslo per normalizzare l'oggetto rispetto al suo centro di massa if (inertiaTensor.GetMassCenter() != new Vector3()) { for (int j = 0; j < ObjectGeometry[i].VertexPosition.Length; j++) { ObjectGeometry[i].SetVertexPosition( ObjectGeometry[i].VertexPosition[j].Vertex + inertiaTensor.GetMassCenter(), j); } normalizedInertiaTensor = new InertiaTensor( vertexPosition, ObjectGeometry[i].Triangle, PartialMass[i]); } startPosition += normalizedInertiaTensor.GetMassCenter() * PartialMass[i]; totalVertex += ObjectGeometry[i].VertexPosition.Length; baseTensors += inertiaTensor.GetInertiaTensor(); } RotationMatrix = Quaternion.ConvertToMatrix(Quaternion.Normalize(RotationStatus)); if (Mass > 0) StartPosition = startPosition / Mass; SetRelativePosition(totalVertex); BaseInertiaTensor = Matrix3x3.Invert(baseTensors); InertiaTensor = (RotationMatrix * BaseInertiaTensor) * Matrix3x3.Transpose(RotationMatrix); }