internal override bool _BuildCollisionObject() { Mesh mesh = meshSettings.Build(); GetComponent <MeshFilter>().sharedMesh = mesh; //convert the mesh data to Bullet data and create DoftBody BulletSharp.Math.Vector3[] bVerts = new BulletSharp.Math.Vector3[mesh.vertexCount]; for (int i = 0; i < mesh.vertexCount; i++) { bVerts[i] = mesh.vertices[i].ToBullet(); } SoftBody m_BSoftBody = SoftBodyHelpers.CreateFromTriMesh(World.WorldInfo, bVerts, mesh.triangles); m_collisionObject = m_BSoftBody; SoftBodySettings.ConfigureSoftBody(m_BSoftBody); //Set SB settings //Set SB position to GO position m_BSoftBody.Rotate(transform.rotation.ToBullet()); m_BSoftBody.Translate(transform.position.ToBullet()); m_BSoftBody.Scale(transform.localScale.ToBullet()); return(true); }
internal override bool _BuildCollisionObject() { //Mesh mesh = new Mesh(); //GetComponent<MeshFilter>().sharedMesh = mesh; //convert the mesh data to Bullet data and create DoftBody BulletSharp.Math.Vector3[] bVerts = new BulletSharp.Math.Vector3[actuallyverts.Count]; for (int i = 0; i < actuallyverts.Count; i++) { bVerts[i] = actuallyverts[i].ToBullet(); } SoftBody m_BSoftBody = CreateVolumetricSoftbody(World.WorldInfo, bVerts); m_collisionObject = m_BSoftBody; SoftBodySettings.ConfigureSoftBody(m_BSoftBody); //Set SB settings //Set SB position to GO position m_BSoftBody.Rotate(transform.rotation.ToBullet()); m_BSoftBody.Translate(transform.position.ToBullet()); m_BSoftBody.Scale(transform.localScale.ToBullet()); return(true); }
public void Evaluate(int SpreadMax) { if (this.FWorld.PluginIO.IsConnected && this.FShapes.PluginIO.IsConnected) { List <SoftBody> bodies = new List <SoftBody>(); for (int i = 0; i < SpreadMax; i++) { if (FDoCreate[i]) { AbstractSoftShapeDefinition shapedef = this.FShapes[i]; SoftBody body = shapedef.GetSoftBody(this.FWorld[0].WorldInfo); body.Translate(this.FPosition[i].ToBulletVector()); body.Scale(this.FScale[i].ToBulletVector()); body.Rotate(this.FRotate[i].ToBulletQuaternion()); body.Friction = this.FFriction[i]; body.Restitution = this.FRestitution[i]; SoftBodyCustomData bd = new SoftBodyCustomData(); bd.Id = this.FWorld[0].GetNewBodyId(); bd.Custom = this.FCustom[i]; bd.HasUV = shapedef.HasUV; bd.UV = shapedef.GetUV(body); body.UserObject = bd; if (this.FCustomObj.PluginIO.IsConnected) { bd.CustomObject = (ICloneable)this.FCustomObj[i].Clone(); } else { bd.CustomObject = null; } this.FWorld[0].Register(body); bodies.Add(body); } } this.FOutBodies.SliceCount = bodies.Count; for (int i = 0; i < bodies.Count; i++) { this.FOutBodies[i] = bodies[i]; } } else { this.FOutBodies.SliceCount = 0; } }
public void Evaluate(int SpreadMax) { if (this.FWorld.PluginIO.IsConnected && this.FShapes.PluginIO.IsConnected) { List <SoftBody> bodies = new List <SoftBody>(); for (int i = 0; i < SpreadMax; i++) { if (FDoCreate[i]) { AbstractSoftShapeDefinition shapedef = this.FShapes[i]; SoftBody body = shapedef.GetSoftBody(this.FWorld[0].WorldInfo); RigidBodyPose pose = this.initialPoseInput.IsConnected ? this.initialPoseInput[i] : RigidBodyPose.Default; body.Translate(pose.Position); body.Scale(this.FScale[i].ToBulletVector()); body.Rotate(pose.Orientation); body.Friction = this.FFriction[i]; body.Restitution = this.FRestitution[i]; SoftBodyCustomData bd = new SoftBodyCustomData(this.FWorld[0].GetNewSoftBodyId()); bd.Custom = this.FCustom[i]; bd.HasUV = shapedef.HasUV; bd.UV = shapedef.GetUV(body); bd.Definition = shapedef; body.UserObject = bd; this.FWorld[0].Register(body); bodies.Add(body); //Attach if to all nodes for (int j = 0; j < body.Nodes.Count; j++) { body.Nodes[j].Tag = (IntPtr)j; } } } this.FOutBodies.SliceCount = bodies.Count; for (int i = 0; i < bodies.Count; i++) { this.FOutBodies[i] = bodies[i]; } } else { this.FOutBodies.SliceCount = 0; } }
void Init_TetraBunny() { SoftBody psb = SoftBodyHelpers.CreateFromTetGenData(softBodyWorldInfo, Bunny.GetElements(), null, Bunny.GetNodes(), false, true, true); SoftWorld.AddSoftBody(psb); psb.Rotate(Quaternion.RotationYawPitchRoll((float)Math.PI / 2, 0, 0)); psb.SetVolumeMass(150); psb.Cfg.PIterations = 2; //psb.Cfg.PIterations = 1; cutting = false; //psb.CollisionShape.Margin = 0.01f; psb.Cfg.Collisions = FCollisions.CLSS | FCollisions.CLRS; //| FCollisions.CLSelf; ///pass zero in generateClusters to create cluster for each tetrahedron or triangle psb.GenerateClusters(0); //psb.Materials[0].Lst = 0.2f; psb.Cfg.DF = 10; }
void InitClusterCar() { //SetAzi(180); Vector3 origin = new Vector3(100, 80, 0); Quaternion orientation = Quaternion.RotationYawPitchRoll(-(float)Math.PI / 2, 0, 0); const float widthf = 8; const float widthr = 9; const float length = 8; const float height = 4; Vector3[] wheels = { new Vector3(+widthf, -height, +length), // Front left new Vector3(-widthf, -height, +length), // Front right new Vector3(+widthr, -height, -length), // Rear left new Vector3(-widthr, -height, -length), // Rear right }; SoftBody pa = CreateClusterBunny(Vector3.Zero, Vector3.Zero); SoftBody pfl = CreateClusterTorus(wheels[0], new Vector3(0, 0, (float)Math.PI / 2), new Vector3(2, 4, 2)); SoftBody pfr = CreateClusterTorus(wheels[1], new Vector3(0, 0, (float)Math.PI / 2), new Vector3(2, 4, 2)); SoftBody prl = CreateClusterTorus(wheels[2], new Vector3(0, 0, (float)Math.PI / 2), new Vector3(2, 5, 2)); SoftBody prr = CreateClusterTorus(wheels[3], new Vector3(0, 0, (float)Math.PI / 2), new Vector3(2, 5, 2)); pfl.Cfg.DF = pfr.Cfg.DF = prl.Cfg.DF = prr.Cfg.DF = 1; using (var lspecs = new LJoint.Specs()) { lspecs.Cfm = 1; lspecs.Erp = 1; lspecs.Position = wheels[0]; pa.AppendLinearJoint(lspecs, pfl); lspecs.Position = wheels[1]; pa.AppendLinearJoint(lspecs, pfr); lspecs.Position = wheels[2]; pa.AppendLinearJoint(lspecs, prl); lspecs.Position = wheels[3]; pa.AppendLinearJoint(lspecs, prr); } using (var aspecs = new AJoint.Specs()) { aspecs.Cfm = 1; aspecs.Erp = 1; aspecs.Axis = new Vector3(1, 0, 0); aspecs.Control = steerControlF; pa.AppendAngularJoint(aspecs, pfl); pa.AppendAngularJoint(aspecs, pfr); aspecs.Control = motorControl; pa.AppendAngularJoint(aspecs, prl); pa.AppendAngularJoint(aspecs, prr); } pa.Rotate(orientation); pfl.Rotate(orientation); pfr.Rotate(orientation); prl.Rotate(orientation); prr.Rotate(orientation); pa.Translate(origin); pfl.Translate(origin); pfr.Translate(origin); prl.Translate(origin); prr.Translate(origin); pfl.Cfg.PIterations = pfr.Cfg.PIterations = prl.Cfg.PIterations = prr.Cfg.PIterations = 1; pfl.Clusters[0].Matching = pfr.Clusters[0].Matching = prl.Clusters[0].Matching = prr.Clusters[0].Matching = 0.05f; pfl.Clusters[0].NodeDamping = pfr.Clusters[0].NodeDamping = prl.Clusters[0].NodeDamping = prr.Clusters[0].NodeDamping = 0.05f; CreateStairs(20, new Vector3(0, -8, 0), new Vector3(3, 2, 40)); CreateRigidBodyStack(50); }
internal override bool _BuildCollisionObject() { if (World == null) { return(false); } if (transform.localScale != Vector3.one) { Debug.LogError("The scale must be 1,1,1"); } if (bone2idxMap == null || bone2idxMap.Length == 0) { Debug.LogError("No bones have been mapped to soft body nodes for object " + name); } for (int i = 0; i < anchors.Length; i++) { if (anchors[i].anchorRigidBody == null) { Debug.LogError("No anchor rigid body has been set for anchor " + i); } if (anchors[i].anchorNodeIndexes == null || anchors[i].anchorNodeIndexes.Count == 0) { Debug.LogError("No nodes have been identified as anchors. Soft body will not be attached to RigidBody anchor " + anchors[i].anchorRigidBody); } } if (physicsSimMesh == null) { physicsSimMesh = GetComponent <MeshFilter>(); } Mesh mesh = physicsSimMesh.sharedMesh; //convert the mesh data to Bullet data and create DoftBody //todo should these be in world coordinates BulletSharp.Math.Vector3[] bVerts = new BulletSharp.Math.Vector3[mesh.vertexCount]; Vector3[] verts = mesh.vertices; for (int i = 0; i < mesh.vertexCount; i++) { bVerts[i] = verts[i].ToBullet(); } SoftBody m_BSoftBody = SoftBodyHelpers.CreateFromTriMesh(World.WorldInfo, bVerts, mesh.triangles); m_collisionObject = m_BSoftBody; SoftBodySettings.ConfigureSoftBody(m_BSoftBody); //Set SB settings //Set SB position to GO position m_BSoftBody.Rotate(physicsSimMesh.transform.rotation.ToBullet()); m_BSoftBody.Translate(physicsSimMesh.transform.position.ToBullet()); m_BSoftBody.Scale(physicsSimMesh.transform.localScale.ToBullet()); for (int i = 0; i < anchors.Length; i++) { BAnchor a = anchors[i]; for (int j = 0; j < a.anchorNodeIndexes.Count; j++) { m_BSoftBody.AppendAnchor(a.anchorNodeIndexes[j], (RigidBody)a.anchorRigidBody.GetCollisionObject(), false, a.anchorNodeStrength[j]); } } MeshRenderer mr = physicsSimMesh.GetComponent <MeshRenderer>(); if (mr != null) { if (debugDisplaySimulatedMesh) { mr.enabled = true; } else { mr.enabled = false; } } if (norms.Length == 0 || norms.Length != verts.Length) { norms = new Vector3[m_BSoftBody.Nodes.Count]; verts = new Vector3[m_BSoftBody.Nodes.Count]; } for (int i = 0; i < m_BSoftBody.Nodes.Count; i++) { norms[i] = m_BSoftBody.Nodes[i].Normal.ToUnity(); verts[i] = m_BSoftBody.Nodes[i].Position.ToUnity(); } for (int i = 0; i < bone2idxMap.Length; i++) { bone2idxMap[i].bindNormal = norms[bone2idxMap[i].nodeIdx]; bone2idxMap[i].bindBoneRotation = bone2idxMap[i].bone.rotation; for (int j = 0; j < bone2idxMap[i].edges.Length; j++) { bone2idxMap[i].edges[j].bindEdgeXnorm = Vector3.Cross(verts[bone2idxMap[i].edges[j].nodeIdx] - verts[bone2idxMap[i].nodeIdx], norms[bone2idxMap[i].nodeIdx]).normalized; } } return(true); }