Mesh CreateMultiSphereShape(MultiSphereShape shape) { Mesh mesh = null; int i; for (i = 0; i < shape.SphereCount; i++) { Vector3 position = shape.GetSpherePosition(i); Mesh sphereMesh = Mesh.CreateSphere(device, shape.GetSphereRadius(i), 12, 12); if (i == 0) { Matrix[] transform = new Matrix[] { Matrix.Translation(position) }; mesh = Mesh.Concatenate(device, new Mesh[] { sphereMesh }, MeshFlags.Managed, transform, null); } else { Mesh multiSphereMeshNew; Matrix[] transform = new Matrix[] { Matrix.Identity, Matrix.Translation(position) }; multiSphereMeshNew = Mesh.Concatenate(device, new Mesh[] { mesh, sphereMesh }, MeshFlags.Managed, transform, null); mesh.Dispose(); mesh = multiSphereMeshNew; } sphereMesh.Dispose(); } complexShapes.Add(shape, mesh); return(mesh); }
public static Vector3[] CreateMultiSphere(MultiSphereShape shape, out uint[] indices) { List <Vector3[]> allVertices = new List <Vector3[]>(); List <uint[]> allIndices = new List <uint[]>(); int vertexCount = 0; int indexCount = 0; int i; for (i = 0; i < shape.SphereCount; i++) { uint[] sphereIndices; Vector3[] sphereVertices = CreateSphere(shape.GetSphereRadius(i), out sphereIndices); // Adjust sphere position Vector3 position = shape.GetSpherePosition(i); for (int j = 0; j < sphereVertices.Length / 2; j++) { sphereVertices[j * 2] += position; } // Adjust indices if (indexCount != 0) { int indexOffset = vertexCount / 2; for (int j = 0; j < sphereIndices.Length; j++) { sphereIndices[j] += (uint)indexOffset; } } allVertices.Add(sphereVertices); allIndices.Add(sphereIndices); vertexCount += sphereVertices.Length; indexCount += sphereIndices.Length; } Vector3[] finalVertices = new Vector3[vertexCount]; int vo = 0; foreach (Vector3[] v in allVertices) { v.CopyTo(finalVertices, vo); vo += v.Length; } indices = new uint[indexCount]; int io = 0; foreach (uint[] ind in allIndices) { ind.CopyTo(indices, io); io += ind.Length; } return(finalVertices); }
public void RenderMultiSphereShape(MultiSphereShape shape, Mesh mesh) { int count = shape.SphereCount; for (int i = 0; i < count; i++) { mesh.DrawSubset(i); } }
MultiSphereShape _CreateMultiSphereShape() { BulletSharp.Math.Vector3[] positions = new BulletSharp.Math.Vector3[spheres.Length]; float[] radius = new float[spheres.Length]; for (int i = 0; i < spheres.Length; i++) { positions[i] = spheres[i].position.ToBullet(); radius[i] = spheres[i].radius; } MultiSphereShape mss = new MultiSphereShape(positions, radius); mss.LocalScaling = m_localScaling.ToBullet(); return(mss); }
public override CollisionShape GetCollisionShape() { if (collisionShapePtr == null) { BulletSharp.Math.Vector3[] positions = new BulletSharp.Math.Vector3[spheres.Length]; float[] radius = new float[spheres.Length]; for (int i = 0; i < spheres.Length; i++) { positions[i] = spheres[i].position.ToBullet(); radius[i] = spheres[i].radius; } collisionShapePtr = new MultiSphereShape(positions, radius); } return(collisionShapePtr); }
/// <summary> /// Adds the child shape. /// </summary> /// <param name="localTransform">The local transform.</param> /// <param name="shape">The shape.</param> public void AddChildShape(float4x4 localTransform, IMultiSphereShapeImp shape) { var btPositions = new Vector3[shape.SphereCount]; var btRadi = new float[shape.SphereCount]; for (int i = 0; i < shape.SphereCount; i++) { var pos = Translator.Float3ToBtVector3(shape.GetSpherePosition(i)); btPositions[i] = pos; btRadi[i] = shape.GetSphereRadius(i); } var btChildShape = new MultiSphereShape(btPositions, btRadi); var btLocalTransform = Translator.Float4X4ToBtMatrix(localTransform); BtCompoundShape.AddChildShape(btLocalTransform, btChildShape); }
//MultiSphereShape public IMultiSphereShapeImp AddMultiSphereShape(float3[] positions, float[] radi) { var btPositions = new Vector3[positions.Length]; for (int i = 0; i < positions.Length; i++) { btPositions[i] = new Vector3(positions[i].x, positions[i].y, positions[i].z); } var btMultiSphereShape = new MultiSphereShape(btPositions, radi); BtCollisionShapes.Add(btMultiSphereShape); var retval = new MultiSphereShapeImp(); retval.BtMultiSphereShape = btMultiSphereShape; btMultiSphereShape.UserObject = retval; return(retval); }
protected override void OnInitializePhysics() { // collision configuration contains default setup for memory, collision setup CollisionConf = new DefaultCollisionConfiguration(); Dispatcher = new CollisionDispatcher(CollisionConf); Broadphase = new DbvtBroadphase(); World = new DiscreteDynamicsWorld(Dispatcher, Broadphase, null, CollisionConf); World.Gravity = new Vector3(0, -10, 0); GImpactCollisionAlgorithm.RegisterAlgorithm(Dispatcher); string bulletFile; string[] args = Environment.GetCommandLineArgs(); if (args.Length == 1) { bulletFile = "testFile.bullet"; } else { bulletFile = args[1]; } fileLoader = new CustomBulletWorldImporter(World); if (!fileLoader.LoadFile(bulletFile)) { CollisionShape groundShape = new BoxShape(50); CollisionShapes.Add(groundShape); RigidBody ground = LocalCreateRigidBody(0, Matrix.Translation(0, -50, 0), groundShape); ground.UserObject = "Ground"; // create a few dynamic rigidbodies float mass = 1.0f; Vector3[] positions = new Vector3[2] { new Vector3(0.1f, 0.2f, 0.3f), new Vector3(0.4f, 0.5f, 0.6f) }; float[] radi = new float[2] { 0.3f, 0.4f }; CollisionShape colShape = new MultiSphereShape(positions, radi); //CollisionShape colShape = new CapsuleShapeZ(1, 1); //CollisionShape colShape = new CylinderShapeZ(1, 1, 1); //CollisionShape colShape = new BoxShape(1); //CollisionShape colShape = new SphereShape(1); CollisionShapes.Add(colShape); Vector3 localInertia = colShape.CalculateLocalInertia(mass); float start_x = StartPosX - ArraySizeX / 2; float start_y = StartPosY; float start_z = StartPosZ - ArraySizeZ / 2; int k, i, j; for (k = 0; k < ArraySizeY; k++) { for (i = 0; i < ArraySizeX; i++) { for (j = 0; j < ArraySizeZ; j++) { Matrix startTransform = Matrix.Translation( 2 * i + start_x, 2 * k + start_y, 2 * j + start_z ); // using motionstate is recommended, it provides interpolation capabilities // and only synchronizes 'active' objects DefaultMotionState myMotionState = new DefaultMotionState(startTransform); RigidBodyConstructionInfo rbInfo = new RigidBodyConstructionInfo(mass, myMotionState, colShape, localInertia); RigidBody body = new RigidBody(rbInfo); rbInfo.Dispose(); // make it drop from a height body.Translate(new Vector3(0, 20, 0)); World.AddRigidBody(body); } } } DefaultSerializer serializer = new DefaultSerializer(); serializer.RegisterNameForObject(ground, "GroundName"); for (i = 0; i < CollisionShapes.Count; i++) { serializer.RegisterNameForObject(CollisionShapes[i], "name" + i.ToString()); } Point2PointConstraint p2p = new Point2PointConstraint((RigidBody)World.CollisionObjectArray[2], new Vector3(0, 1, 0)); World.AddConstraint(p2p); serializer.RegisterNameForObject(p2p, "constraintje"); World.Serialize(serializer); BulletSharp.DataStream data = serializer.LockBuffer(); byte[] dataBytes = new byte[data.Length]; data.Read(dataBytes, 0, dataBytes.Length); FileStream file = new FileStream("testFile.bullet", FileMode.Create); file.Write(dataBytes, 0, dataBytes.Length); file.Close(); } }
/*private void MyTickCallBack(ManifoldPoint cp, CollisionObjectWrapper colobj0wrap, int partid0, int index0, CollisionObjectWrapper colobj1wrap, int partid1, int index1) * { * Debug.WriteLine("MyTickCallBack"); * int numManifolds = BtWorld.Dispatcher.NumManifolds; * RigidBodyImp myRb; * //Debug.WriteLine("numManifolds: " + numManifolds); * for (int i = 0; i < numManifolds; i++) * { * PersistentManifold contactManifold = BtWorld.Dispatcher.GetManifoldByIndexInternal(i); * int numContacts = contactManifold.NumContacts; * if (numContacts > 0) * { * CollisionObject obA = (CollisionObject) contactManifold.Body0; * CollisionObject obB = (CollisionObject) contactManifold.Body1; * * // Debug.WriteLine(numContacts); * var pnA = obA.UserObject; * * for (int j = 0; j < numContacts; j++) * { * ManifoldPoint pt = contactManifold.GetContactPoint(j); * * } * } * } * }*/ public IRigidBodyImp AddRigidBody(float mass, float3 worldTransform, float3 orientation, ICollisionShapeImp colShape /*, float3 intertia*/) { // Use bullet to do what needs to be done: var btMatrix = Matrix.RotationX(orientation.x) * Matrix.RotationY(orientation.y) * Matrix.RotationZ(orientation.z) * Matrix.Translation(worldTransform.x, worldTransform.y, worldTransform.z); var btMotionState = new DefaultMotionState(btMatrix); var shapeType = colShape.GetType().ToString(); CollisionShape btColShape; var isStatic = false; switch (shapeType) { //Primitives case "Fusee.Engine.BoxShapeImp": var box = (BoxShapeImp)colShape; var btBoxHalfExtents = Translater.Float3ToBtVector3(box.HalfExtents); btColShape = new BoxShape(btBoxHalfExtents); break; case "Fusee.Engine.CapsuleShapeImp": var capsule = (CapsuleShapeImp)colShape; btColShape = new CapsuleShape(capsule.Radius, capsule.HalfHeight); break; case "Fusee.Engine.ConeShapeImp": var cone = (ConeShapeImp)colShape; btColShape = new ConeShape(cone.Radius, cone.Height); break; case "Fusee.Engine.CylinderShapeImp": var cylinider = (CylinderShapeImp)colShape; var btCylinderHalfExtents = Translater.Float3ToBtVector3(cylinider.HalfExtents); btColShape = new CylinderShape(btCylinderHalfExtents); break; case "Fusee.Engine.MultiSphereShapeImp": var multiSphere = (MultiSphereShapeImp)colShape; var btPositions = new Vector3[multiSphere.SphereCount]; var btRadi = new float[multiSphere.SphereCount]; for (int i = 0; i < multiSphere.SphereCount; i++) { var pos = Translater.Float3ToBtVector3(multiSphere.GetSpherePosition(i)); btPositions[i] = pos; btRadi[i] = multiSphere.GetSphereRadius(i); } btColShape = new MultiSphereShape(btPositions, btRadi); break; case "Fusee.Engine.SphereShapeImp": var sphere = (SphereShapeImp)colShape; var btRadius = sphere.Radius; btColShape = new SphereShape(btRadius); break; //Misc case "Fusee.Engine.CompoundShapeImp": var compShape = (CompoundShapeImp)colShape; btColShape = new CompoundShape(true); btColShape = compShape.BtCompoundShape; break; case "Fusee.Engine.EmptyShapeImp": btColShape = new EmptyShape(); break; //Meshes case "Fusee.Engine.ConvexHullShapeImp": var convHull = (ConvexHullShapeImp)colShape; var btPoints = new Vector3[convHull.GetNumPoints()]; for (int i = 0; i < convHull.GetNumPoints(); i++) { var point = convHull.GetScaledPoint(i); btPoints[i] = Translater.Float3ToBtVector3(point); } btColShape = new ConvexHullShape(btPoints); //btColShape.LocalScaling = new Vector3(3,3,3); break; case "Fusee.Engine.StaticPlaneShapeImp": var staticPlane = (StaticPlaneShapeImp)colShape; Debug.WriteLine("staticplane: " + staticPlane.Margin); var btNormal = Translater.Float3ToBtVector3(staticPlane.PlaneNormal); btColShape = new StaticPlaneShape(btNormal, staticPlane.PlaneConstant); isStatic = true; //btColShape.Margin = 0.04f; //Debug.WriteLine("btColshape" + btColShape.Margin); break; case "Fusee.Engine.GImpactMeshShapeImp": var gImpMesh = (GImpactMeshShapeImp)colShape; gImpMesh.BtGImpactMeshShape.UpdateBound(); var btGimp = new GImpactMeshShape(gImpMesh.BtGImpactMeshShape.MeshInterface); btGimp.UpdateBound(); btColShape = btGimp; break; //Default default: Debug.WriteLine("defaultImp"); btColShape = new EmptyShape(); break; } var btLocalInertia = btColShape.CalculateLocalInertia(mass); // btLocalInertia *= (10.0f*10); RigidBodyConstructionInfo btRbcInfo = new RigidBodyConstructionInfo(mass, btMotionState, btColShape, btLocalInertia); var btRigidBody = new RigidBody(btRbcInfo); btRigidBody.Restitution = 0.2f; btRigidBody.Friction = 0.2f; btRigidBody.CollisionFlags = CollisionFlags.CustomMaterialCallback; BtWorld.AddRigidBody(btRigidBody); btRbcInfo.Dispose(); var retval = new RigidBodyImp(); retval._rbi = btRigidBody; btRigidBody.UserObject = retval; return(retval); }
//MultiSphere public MultiSphereShape AddMultiSphereShape(float3[] positions, float[] radi) { IMultiSphereShapeImp iMultiSphereShapeImp = _dwi.AddMultiSphereShape(positions, radi); var retval = new MultiSphereShape(); retval.MultiSphereShapeImp = iMultiSphereShapeImp; iMultiSphereShapeImp.UserObject = retval; return retval; }
public SerializeDemoSimulation() { CollisionConfiguration = new DefaultCollisionConfiguration(); Dispatcher = new CollisionDispatcher(CollisionConfiguration); Broadphase = new DbvtBroadphase(); World = new DiscreteDynamicsWorld(Dispatcher, Broadphase, null, CollisionConfiguration); GImpactCollisionAlgorithm.RegisterAlgorithm(Dispatcher); string bulletFile; string[] args = Environment.GetCommandLineArgs(); if (args.Length == 1) { bulletFile = "testFile.bullet"; } else { bulletFile = args[1]; } _fileLoader = new CustomBulletWorldImporter(World); if (!_fileLoader.LoadFile(bulletFile)) { var groundShape = new BoxShape(50); _collisionShapes.Add(groundShape); RigidBody ground = PhysicsHelper.CreateStaticBody(Matrix.Translation(0, -50, 0), groundShape, World); ground.UserObject = "Ground"; // create a few dynamic rigidbodies float mass = 1.0f; Vector3[] positions = new[] { new Vector3(0.1f, 0.2f, 0.3f), new Vector3(0.4f, 0.5f, 0.6f) }; float[] radi = new float[2] { 0.3f, 0.4f }; var colShape = new MultiSphereShape(positions, radi); //var colShape = new CapsuleShapeZ(1, 1); //var colShape = new CylinderShapeZ(1, 1, 1); //var colShape = new BoxShape(1); //var colShape = new SphereShape(1); _collisionShapes.Add(colShape); Vector3 localInertia = colShape.CalculateLocalInertia(mass); float startX = StartPosX - NumObjectsX / 2; float startY = StartPosY; float startZ = StartPosZ - NumObjectsZ / 2; for (int y = 0; y < NumObjectsY; y++) { for (int x = 0; x < NumObjectsX; x++) { for (int z = 0; z < NumObjectsZ; z++) { Matrix startTransform = Matrix.Translation( 2 * x + startX, 2 * y + startY, 2 * z + startZ ); // using motionstate is recommended, it provides interpolation capabilities // and only synchronizes 'active' objects DefaultMotionState myMotionState = new DefaultMotionState(startTransform); RigidBodyConstructionInfo rbInfo = new RigidBodyConstructionInfo(mass, myMotionState, colShape, localInertia); RigidBody body = new RigidBody(rbInfo); rbInfo.Dispose(); // make it drop from a height body.Translate(new Vector3(0, 20, 0)); World.AddRigidBody(body); } } } using (var serializer = new DefaultSerializer()) { serializer.RegisterNameForObject(ground, "GroundName"); for (int i = 0; i < _collisionShapes.Count; i++) { serializer.RegisterNameForObject(_collisionShapes[i], "name" + i.ToString()); } var p2p = new Point2PointConstraint((RigidBody)World.CollisionObjectArray[2], new Vector3(0, 1, 0)); World.AddConstraint(p2p); serializer.RegisterNameForObject(p2p, "constraintje"); World.Serialize(serializer); byte[] dataBytes = new byte[serializer.CurrentBufferSize]; Marshal.Copy(serializer.BufferPointer, dataBytes, 0, dataBytes.Length); using (var file = new FileStream("testFile.bullet", FileMode.Create)) { file.Write(dataBytes, 0, dataBytes.Length); } } } }