protected override void OnInitializePhysics() { // collision configuration contains default setup for memory, collision setup CollisionConf = new DefaultCollisionConfiguration(); Dispatcher = new CollisionDispatcher(CollisionConf); Broadphase = new DbvtBroadphase(); Solver = new SequentialImpulseConstraintSolver(); World = new DiscreteDynamicsWorld(Dispatcher, Broadphase, Solver, CollisionConf); World.Gravity = Freelook.Up * -10.0f; BspLoader bspLoader = new BspLoader(); string[] args = Environment.GetCommandLineArgs(); if (args.Length == 1) { bspLoader.LoadBspFile("data/BspDemo.bsp"); } else { bspLoader.LoadBspFile(args[1]); } BspConverter bsp2Bullet = new BspToBulletConverter(this); bsp2Bullet.ConvertBsp(bspLoader, 0.1f); }
protected override void OnInitializePhysics() { // collision configuration contains default setup for memory, collision setup CollisionConf = new DefaultCollisionConfiguration(); Dispatcher = new CollisionDispatcher(CollisionConf); Broadphase = new AxisSweep3(new Vector3(-10000, -10000, -10000), new Vector3(10000, 10000, 10000)); Solver = new SequentialImpulseConstraintSolver(); World = new DiscreteDynamicsWorld(Dispatcher, Broadphase, Solver, CollisionConf); World.Gravity = new Vector3(0, -10, 0); //World.DispatchInfo.UseConvexConservativeDistanceUtil = true; //World.DispatchInfo.ConvexConservativeDistanceThreshold = 0.01f; // Setup a big ground box CollisionShape groundShape = new BoxShape(100, 10, 100); CollisionShapes.Add(groundShape); Matrix groundTransform = Matrix.Translation(0, -10, 0); RigidBody ground = LocalCreateRigidBody(0, groundTransform, groundShape); ground.UserObject = "Ground"; // Spawn one ragdoll SpawnRagdoll(new Vector3(1, 0.5f, 0)); SpawnRagdoll(new Vector3(-1, 0.5f, 0)); }
protected override void OnInitializePhysics() { // collision configuration contains default setup for memory, collision setup CollisionConf = new DefaultCollisionConfiguration(); Dispatcher = new CollisionDispatcher(CollisionConf); Vector3 worldAabbMin = new Vector3(-10000, -10000, -10000); Vector3 worldAabbMax = new Vector3(10000, 10000, 10000); Broadphase = new AxisSweep3(worldAabbMin, worldAabbMax); Solver = new SequentialImpulseConstraintSolver(); World = new DiscreteDynamicsWorld(Dispatcher, Broadphase, Solver, CollisionConf); World.Gravity = new Vector3(0, -10, 0); World.SetInternalTickCallback(MotorPreTickCallback, this, true); // create the ground CollisionShape groundShape = new BoxShape(200, 10, 200); CollisionShapes.Add(groundShape); CollisionObject ground = LocalCreateRigidBody(0, Matrix.Translation(0, -10, 0), groundShape); ground.UserObject = "Ground"; fCyclePeriod = 2000.0f; fMuscleStrength = 0.5f; m_Time = 0; SpawnTestRig(new Vector3(1, 0.5f, 0), false); SpawnTestRig(new Vector3(-2, 0.5f, 0), true); }
/* void MyContactCallback(object sender, ContactAddedEventArgs e) { if (e.CollisionObject0Wrapper.CollisionObject.CollisionShape.ShapeType == BroadphaseNativeType.CompoundShape) { CompoundShape compound = e.CollisionObject0Wrapper.CollisionObject.CollisionShape as CompoundShape; CollisionShape childShape = compound.GetChildShape(e.Index0); } if (e.CollisionObject1Wrapper.CollisionObject.CollisionShape.ShapeType == BroadphaseNativeType.CompoundShape) { CompoundShape compound = e.CollisionObject1Wrapper.CollisionObject.CollisionShape as CompoundShape; CollisionShape childShape = compound.GetChildShape(e.Index1); } e.IsContactModified = true; } */ public void SetupEmptyDynamicsWorld() { // collision configuration contains default setup for memory, collision setup CollisionConf = new DefaultCollisionConfiguration(); Dispatcher = new CollisionDispatcher(CollisionConf); CompoundCollisionAlgorithm.CompoundChildShapePairCallback = MyCompoundChildShapeCallback; convexDecompositionObjectOffset = new Vector3(10, 0, 0); Broadphase = new AxisSweep3(new Vector3(-10000, -10000, -10000), new Vector3(10000, 10000, 10000)); //Broadphase = new SimpleBroadphase(); Solver = new SequentialImpulseConstraintSolver(); World = new DiscreteDynamicsWorld(Dispatcher, Broadphase, Solver, CollisionConf); // create the ground CollisionShape groundShape = new BoxShape(30, 2, 30); CollisionShapes.Add(groundShape); CollisionObject ground = LocalCreateRigidBody(0, Matrix.Translation(0, -4.5f, 0), groundShape); ground.UserObject = "Ground"; // create a few dynamic rigidbodies float mass = 1.0f; CollisionShape colShape = new BoxShape(1); CollisionShapes.Add(colShape); Vector3 localInertia = colShape.CalculateLocalInertia(mass); }
protected override void OnInitializePhysics() { // collision configuration contains default setup for memory, collision setup CollisionConf = new DefaultCollisionConfiguration(); Dispatcher = new CollisionDispatcher(CollisionConf); Broadphase = new DbvtBroadphase(); Solver = new SequentialImpulseConstraintSolver(); World = new DiscreteDynamicsWorld(Dispatcher, Broadphase, Solver, CollisionConf); World.Gravity = Freelook.Up * -10.0f; BspLoader bspLoader = new BspLoader(); //string[] args = Environment.GetCommandLineArgs(); //if (args.Length == 1) //{ UnityEngine.TextAsset bytes = (UnityEngine.TextAsset)UnityEngine.Resources.Load("BspDemo"); System.IO.Stream byteStream = new System.IO.MemoryStream(bytes.bytes); bspLoader.LoadBspFile(byteStream); //} //else //{ // bspLoader.LoadBspFile(args[1]); //} BspConverter bsp2Bullet = new BspToBulletConverter(this); bsp2Bullet.ConvertBsp(bspLoader, 0.1f); }
void SetupEmptyDynamicsWorld() { CollisionConf = new DefaultCollisionConfiguration(); Dispatcher = new CollisionDispatcher(CollisionConf); Broadphase = new DbvtBroadphase(); Solver = new SequentialImpulseConstraintSolver(); World = new DiscreteDynamicsWorld(Dispatcher, Broadphase, Solver, CollisionConf); World.Gravity = new Vector3(0, -10, 0); }
public Physics() { CLStuff.InitCL(); cloths = new Cloth[numFlags]; for (int flagIndex = 0; flagIndex < numFlags; ++flagIndex) { cloths[flagIndex] = new Cloth(); cloths[flagIndex].CreateBuffers(clothWidth, clothHeight); } gSolver = new OpenCLSoftBodySolver(CLStuff.commandQueue, CLStuff.cxMainContext); softBodyOutput = new SoftBodySolverOutputCLToCpu(); // collision configuration contains default setup for memory, collision setup CollisionConf = new SoftBodyRigidBodyCollisionConfiguration(); Dispatcher = new CollisionDispatcher(CollisionConf); Broadphase = new DbvtBroadphase(); Solver = new SequentialImpulseConstraintSolver(); World = new SoftRigidDynamicsWorld(Dispatcher, Broadphase, Solver, CollisionConf, gSolver); World.Gravity = new Vector3(0, -10, 0); // create the ground CollisionShape groundShape = new BoxShape(50, 50, 50); CollisionShapes.Add(groundShape); CollisionObject ground = LocalCreateRigidBody(0, Matrix.Translation(0, -60, 0), groundShape); ground.UserObject = "Ground"; SoftWorld.WorldInfo.AirDensity = 1.2f; SoftWorld.WorldInfo.WaterDensity = 0; SoftWorld.WorldInfo.WaterOffset = 0; SoftWorld.WorldInfo.WaterNormal = Vector3.Zero; SoftWorld.WorldInfo.Gravity = new Vector3(0, -10, 0); CreateFlag(clothWidth, clothHeight, out flags); // Create output buffer descriptions for ecah flag // These describe where the simulation should send output data to for (int flagIndex = 0; flagIndex < flags.Count; ++flagIndex) { // flags[flagIndex].WindVelocity = new Vector3(0, 0, 15.0f); // In this case we have a DX11 output buffer with a vertex at index 0, 8, 16 and so on as well as a normal at 3, 11, 19 etc. // Copies will be performed GPU-side directly into the output buffer CpuVertexBufferDescriptor vertexBufferDescriptor = new CpuVertexBufferDescriptor(cloths[flagIndex].CpuBuffer, 0, 8, 3, 8); cloths[flagIndex].VertexBufferDescriptor = vertexBufferDescriptor; } gSolver.Optimize(SoftWorld.SoftBodyArray); World.StepSimulation(1.0f / 60.0f, 0); }
/* void MyContactCallback(object sender, ContactAddedEventArgs e) { if (e.CollisionObject0Wrapper.CollisionObject.CollisionShape.ShapeType == BroadphaseNativeType.CompoundShape) { CompoundShape compound = e.CollisionObject0Wrapper.CollisionObject.CollisionShape as CompoundShape; CollisionShape childShape = compound.GetChildShape(e.Index0); } if (e.CollisionObject1Wrapper.CollisionObject.CollisionShape.ShapeType == BroadphaseNativeType.CompoundShape) { CompoundShape compound = e.CollisionObject1Wrapper.CollisionObject.CollisionShape as CompoundShape; CollisionShape childShape = compound.GetChildShape(e.Index1); } e.IsContactModified = true; } */ public void SetupEmptyDynamicsWorld() { // collision configuration contains default setup for memory, collision setup CollisionConf = new DefaultCollisionConfiguration(); Dispatcher = new CollisionDispatcher(CollisionConf); Broadphase = new AxisSweep3(new Vector3(-10000, -10000, -10000), new Vector3(10000, 10000, 10000)); Solver = new SequentialImpulseConstraintSolver(); World = new DiscreteDynamicsWorld(Dispatcher, Broadphase, Solver, CollisionConf); // create the ground CollisionShape groundShape = new BoxShape(30, 2, 30); CollisionShapes.Add(groundShape); CollisionObject ground = LocalCreateRigidBody(0, Matrix.Translation(0, -4.5f, 0), groundShape); ground.UserObject = "Ground"; }
public Physics() { // collision configuration contains default setup for memory, collision setup CollisionConf = new DefaultCollisionConfiguration(); Dispatcher = new CollisionDispatcher(CollisionConf); Broadphase = new DbvtBroadphase(); Solver = new SequentialImpulseConstraintSolver(); World = new DiscreteDynamicsWorld(Dispatcher, Broadphase, Solver, CollisionConf); World.Gravity = new Vector3(0, 0, -10); BspLoader bspLoader = new BspLoader(); bspLoader.LoadBspFile("BspDemo.bsp"); BspConverter bsp2Bullet = new BspToBulletConverter(this); bsp2Bullet.ConvertBsp(bspLoader, 0.1f); }
protected override void OnInitializePhysics() { // collision configuration contains default setup for memory, collision setup CollisionConf = new DefaultCollisionConfiguration(); Dispatcher = new CollisionDispatcher(CollisionConf); Broadphase = new DbvtBroadphase(); Solver = new SequentialImpulseConstraintSolver(); World = new DiscreteDynamicsWorld(Dispatcher, Broadphase, Solver, CollisionConf); World.Gravity = new Vector3(0, -10, 0); importer = new BulletXmlWorldImporter(World); if (!importer.LoadFile("data\\bullet_basic.xml")) { //throw new FileNotFoundException(); } }
static DefaultRigidBodyWorld() { // Broadphase algorithms are responsible for calculating bounding // boxes. We should probably use an AABB Tree (DbvtBroadphase) // because they are generally good for worlds with lots of motion. // Sweep and Prune Broadphases are best when most of the world is // static. Broadphase = new DbvtBroadphase(); CollisionConfiguration = new DefaultCollisionConfiguration(); Dispatcher = new CollisionDispatcher(CollisionConfiguration); Solver = new SequentialImpulseConstraintSolver(); DynamicsWorld = new DiscreteDynamicsWorld(Dispatcher, Broadphase, Solver, CollisionConfiguration); DynamicsWorld.Gravity = new Vector3(0F, 0F, -9.81F); Ground = PhysicsHelpers.MakePlane(new Vector3(0, 0, 1), 0); DynamicsWorld.AddRigidBody(Ground); }
protected override void OnInitializePhysics() { // collision configuration contains default setup for memory, collision setup CollisionConf = new DefaultCollisionConfiguration(); Dispatcher = new CollisionDispatcher(CollisionConf); Broadphase = new AxisSweep3(new Vector3(-1000, -1000, -1000), new Vector3(1000, 1000, 1000)); Solver = new SequentialImpulseConstraintSolver(); World = new DiscreteDynamicsWorld(Dispatcher, Broadphase, Solver, CollisionConf); World.DispatchInfo.AllowedCcdPenetration = 0.0001f; //World.Gravity = Freelook.Up * -10.0f; Matrix startTransform = Matrix.Translation(10.210098f, -1.6433364f, 16.453260f); ghostObject = new PairCachingGhostObject(); ghostObject.WorldTransform = startTransform; Broadphase.OverlappingPairCache.SetInternalGhostPairCallback(new GhostPairCallback()); const float characterHeight = 1.75f; const float characterWidth = 1.75f; ConvexShape capsule = new CapsuleShape(characterWidth, characterHeight); ghostObject.CollisionShape = capsule; ghostObject.CollisionFlags = CollisionFlags.CharacterObject; const float stepHeight = 0.35f; character = new KinematicCharacterController(ghostObject, capsule, stepHeight); BspLoader bspLoader = new BspLoader(); bspLoader.LoadBspFile("data/BspDemo.bsp"); BspConverter bsp2Bullet = new BspToBulletConverter(this); bsp2Bullet.ConvertBsp(bspLoader, 0.1f); World.AddCollisionObject(ghostObject, CollisionFilterGroups.CharacterFilter, CollisionFilterGroups.StaticFilter | CollisionFilterGroups.DefaultFilter); World.AddAction(character); convexResultCallback = new ClosestConvexResultCallback(); convexResultCallback.CollisionFilterMask = (short)CollisionFilterGroups.StaticFilter; cameraSphere = new SphereShape(0.2f); }
protected override void _InitializePhysicsWorld() { Debug.Log("Creating SoftRigidDynamicsWorld"); // collision configuration contains default setup for memory, collision setup CollisionConf = new SoftBodyRigidBodyCollisionConfiguration(); Dispatcher = new CollisionDispatcher(CollisionConf); //TODO I think this will limit collision detection to -1000 to 1000 should be configurable Broadphase = new AxisSweep3(new BulletSharp.Math.Vector3(-1000, -1000, -1000), new BulletSharp.Math.Vector3(1000, 1000, 1000), maxProxies); //TODO this is taken from the Bullet examples, but I don't understand why the // the default constraint solver. Solver = new SequentialImpulseConstraintSolver(); softBodyWorldInfo = new SoftBodyWorldInfo { AirDensity = 1.2f, WaterDensity = 0, WaterOffset = 0, WaterNormal = BulletSharp.Math.Vector3.Zero, Gravity = UnityEngine.Physics.gravity.ToBullet(), Dispatcher = Dispatcher, Broadphase = Broadphase }; softBodyWorldInfo.SparseSdf.Initialize(); SoftRigidDynamicsWorld sw = new SoftRigidDynamicsWorld(Dispatcher, Broadphase, Solver, CollisionConf); World = sw; World.Gravity = UnityEngine.Physics.gravity.ToBullet(); World.DispatchInfo.EnableSpu = true; softBodyWorldInfo.SparseSdf.Reset(); softBodyWorldInfo.AirDensity = 1.2f; softBodyWorldInfo.WaterDensity = 0; softBodyWorldInfo.WaterOffset = 0; softBodyWorldInfo.WaterNormal = BulletSharp.Math.Vector3.Zero; softBodyWorldInfo.Gravity = UnityEngine.Physics.gravity.ToBullet(); }
public Physics() { demos = new DemoConstructor[] { Init_Cloth, Init_Pressure, Init_Volume, Init_Ropes, Init_RopeAttach, Init_ClothAttach, Init_Sticks, Init_Collide, Init_Collide2, Init_Collide3, Init_Impact, Init_Aero, Init_Aero2, Init_Friction, Init_Torus, Init_TorusMatch, Init_Bunny, Init_BunnyMatch, Init_Cutting1, Init_ClusterDeform, Init_ClusterCollide1, Init_ClusterCollide2, Init_ClusterSocket, Init_ClusterHinge, Init_ClusterCombine, Init_ClusterCar, Init_ClusterRobot, Init_ClusterStackSoft, Init_ClusterStackMixed, Init_TetraCube, Init_TetraBunny }; // collision configuration contains default setup for memory, collision setup CollisionConf = new SoftBodyRigidBodyCollisionConfiguration(); Dispatcher = new CollisionDispatcher(CollisionConf); Broadphase = new AxisSweep3(new Vector3(-1000, -1000, -1000), new Vector3(1000, 1000, 1000), maxProxies); // the default constraint solver. Solver = new SequentialImpulseConstraintSolver(); softBodyWorldInfo = new SoftBodyWorldInfo(); softBodyWorldInfo.AirDensity = 1.2f; softBodyWorldInfo.WaterDensity = 0; softBodyWorldInfo.WaterOffset = 0; softBodyWorldInfo.WaterNormal = Vector3.Zero; softBodyWorldInfo.Gravity = new Vector3(0, -10, 0); softBodyWorldInfo.Dispatcher = Dispatcher; softBodyWorldInfo.Broadphase = Broadphase; softBodyWorldInfo.SparseSdf.Initialize(); World = new SoftRigidDynamicsWorld(Dispatcher, Broadphase, Solver, CollisionConf); World.Gravity = new Vector3(0, -10, 0); World.DispatchInfo.EnableSpu = true; InitializeDemo(); }
protected override void OnInitializePhysics() { // collision configuration contains default setup for memory, collision setup CollisionConf = new DefaultCollisionConfiguration(); // Use the default collision dispatcher. For parallel processing you can use a diffent dispatcher. Dispatcher = new CollisionDispatcher(CollisionConf); VoronoiSimplexSolver simplex = new VoronoiSimplexSolver(); MinkowskiPenetrationDepthSolver pdSolver = new MinkowskiPenetrationDepthSolver(); Convex2DConvex2DAlgorithm.CreateFunc convexAlgo2d = new Convex2DConvex2DAlgorithm.CreateFunc(simplex, pdSolver); Dispatcher.RegisterCollisionCreateFunc(BroadphaseNativeType.Convex2DShape, BroadphaseNativeType.Convex2DShape, convexAlgo2d); Dispatcher.RegisterCollisionCreateFunc(BroadphaseNativeType.Box2DShape, BroadphaseNativeType.Convex2DShape, convexAlgo2d); Dispatcher.RegisterCollisionCreateFunc(BroadphaseNativeType.Convex2DShape, BroadphaseNativeType.Box2DShape, convexAlgo2d); Dispatcher.RegisterCollisionCreateFunc(BroadphaseNativeType.Box2DShape, BroadphaseNativeType.Box2DShape, new Box2DBox2DCollisionAlgorithm.CreateFunc()); Broadphase = new DbvtBroadphase(); // the default constraint solver. Solver = new SequentialImpulseConstraintSolver(); World = new DiscreteDynamicsWorld(Dispatcher, Broadphase, Solver, CollisionConf); World.Gravity = new Vector3(0, -10, 0); // create a few basic rigid bodies CollisionShape groundShape = new BoxShape(150, 7, 150); CollisionShapes.Add(groundShape); RigidBody ground = LocalCreateRigidBody(0, Matrix.Identity, groundShape); ground.UserObject = "Ground"; // create a few dynamic rigidbodies // Re-using the same collision is better for memory usage and performance float u = 0.96f; Vector3[] points = { new Vector3(0, u, 0), new Vector3(-u, -u, 0), new Vector3(u, -u, 0) }; ConvexShape childShape0 = new BoxShape(1, 1, Depth); ConvexShape colShape = new Convex2DShape(childShape0); ConvexShape childShape1 = new ConvexHullShape(points); ConvexShape colShape2 = new Convex2DShape(childShape1); ConvexShape childShape2 = new CylinderShapeZ(1, 1, Depth); ConvexShape colShape3 = new Convex2DShape(childShape2); CollisionShapes.Add(colShape); CollisionShapes.Add(colShape2); CollisionShapes.Add(colShape3); CollisionShapes.Add(childShape0); CollisionShapes.Add(childShape1); CollisionShapes.Add(childShape2); colShape.Margin = 0.03f; float mass = 1.0f; Vector3 localInertia = colShape.CalculateLocalInertia(mass); Matrix startTransform; Vector3 x = new Vector3(-ArraySizeX, 8, -20); Vector3 y = Vector3.Zero; Vector3 deltaX = new Vector3(1, 2, 0); Vector3 deltaY = new Vector3(2, 0, 0); int i, j; for (i = 0; i < ArraySizeY; i++) { y = x; for (j = 0; j < ArraySizeX; j++) { startTransform = Matrix.Translation(y - new Vector3(-10, 0, 0)); //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects DefaultMotionState myMotionState = new DefaultMotionState(startTransform); RigidBodyConstructionInfo rbInfo; switch (j % 3) { case 0: rbInfo = new RigidBodyConstructionInfo(mass, myMotionState, colShape, localInertia); break; case 1: rbInfo = new RigidBodyConstructionInfo(mass, myMotionState, colShape3, localInertia); break; default: rbInfo = new RigidBodyConstructionInfo(mass, myMotionState, colShape2, localInertia); break; } RigidBody body = new RigidBody(rbInfo); rbInfo.Dispose(); //body.ActivationState = ActivationState.IslandSleeping; body.LinearFactor = new Vector3(1, 1, 0); body.AngularFactor = new Vector3(0, 0, 1); World.AddRigidBody(body); y += deltaY; } x += deltaX; } }
protected override void OnInitializePhysics() { // collision configuration contains default setup for memory, collision setup CollisionConf = new DefaultCollisionConfiguration(); Dispatcher = new CollisionDispatcher(CollisionConf); Vector3 worldMin = new Vector3(-1000, -1000, -1000); Vector3 worldMax = new Vector3(1000, 1000, 1000); Broadphase = new AxisSweep3(worldMin, worldMax); Solver = new SequentialImpulseConstraintSolver(); World = new DiscreteDynamicsWorld(Dispatcher, Broadphase, Solver, CollisionConf); World.SolverInfo.SplitImpulse = 1; World.Gravity = new Vector3(0, -10, 0); const int totalVerts = NumVertsX * NumVertsY; const int totalTriangles = 2 * (NumVertsX - 1) * (NumVertsY - 1); indexVertexArrays = new TriangleIndexVertexArray(); IndexedMesh mesh = new IndexedMesh(); mesh.NumTriangles = totalTriangles; mesh.NumVertices = totalVerts; mesh.TriangleIndexStride = 3 * sizeof(int); mesh.VertexStride = Vector3.SizeInBytes; mesh.TriangleIndexBase = Marshal.AllocHGlobal(mesh.TriangleIndexStride * totalTriangles); mesh.VertexBase = Marshal.AllocHGlobal(mesh.VertexStride * totalVerts); var indicesStream = mesh.GetTriangleStream(); using (var indices = new BinaryWriter(indicesStream)) { for (int i = 0; i < NumVertsX - 1; i++) { for (int j = 0; j < NumVertsY - 1; j++) { indices.Write(j*NumVertsX + i); indices.Write(j*NumVertsX + i + 1); indices.Write((j + 1)*NumVertsX + i + 1); indices.Write(j*NumVertsX + i); indices.Write((j + 1)*NumVertsX + i + 1); indices.Write((j + 1)*NumVertsX + i); } } } indexVertexArrays.AddIndexedMesh(mesh); convexcastBatch = new ConvexcastBatch(40.0f, 0.0f, -10.0f, 80.0f); CollisionShape colShape = new BoxShape(1); CollisionShapes.Add(colShape); for (int j = 0; j < NumDynamicBoxesX; j++) { for (int i = 0; i < NumDynamicBoxesY; i++) { //CollisionShape colShape = new CapsuleShape(0.5f,2.0f);//boxShape = new SphereShape(1.0f); Matrix startTransform = Matrix.Translation(5 * (i - NumDynamicBoxesX / 2), 10, 5 * (j - NumDynamicBoxesY / 2)); LocalCreateRigidBody(1.0f, startTransform, colShape); } } SetVertexPositions(WaveHeight, 0.0f); const bool useQuantizedAabbCompression = true; groundShape = new BvhTriangleMeshShape(indexVertexArrays, useQuantizedAabbCompression); CollisionShapes.Add(groundShape); staticBody = LocalCreateRigidBody(0.0f, Matrix.Identity, groundShape); staticBody.CollisionFlags |= CollisionFlags.StaticObject; staticBody.UserObject = "Ground"; }
protected override void OnInitializePhysics() { // collision configuration contains default setup for memory, collision setup DefaultCollisionConstructionInfo cci = new DefaultCollisionConstructionInfo(); cci.DefaultMaxPersistentManifoldPoolSize = 32768; CollisionConf = new DefaultCollisionConfiguration(cci); Dispatcher = new CollisionDispatcher(CollisionConf); Dispatcher.DispatcherFlags = DispatcherFlags.DisableContactPoolDynamicAllocation; // the maximum size of the collision world. Make sure objects stay within these boundaries // Don't make the world AABB size too large, it will harm simulation quality and performance Vector3 worldAabbMin = new Vector3(-1000, -1000, -1000); Vector3 worldAabbMax = new Vector3(1000, 1000, 1000); HashedOverlappingPairCache pairCache = new HashedOverlappingPairCache(); Broadphase = new AxisSweep3(worldAabbMin, worldAabbMax, 3500, pairCache); //Broadphase = new DbvtBroadphase(); Solver = new SequentialImpulseConstraintSolver(); World = new DiscreteDynamicsWorld(Dispatcher, Broadphase, Solver, CollisionConf); World.Gravity = new Vector3(0, -10, 0); World.SolverInfo.SolverMode |= SolverModes.EnableFrictionDirectionCaching; World.SolverInfo.NumIterations = 5; if (benchmark < 5) { // create the ground CollisionShape groundShape = new BoxShape(250, 50, 250); CollisionShapes.Add(groundShape); CollisionObject ground = base.LocalCreateRigidBody(0, Matrix.Translation(0, -50, 0), groundShape); ground.UserObject = "Ground"; } float cubeSize = 1.0f; float spacing = cubeSize; float mass = 1.0f; int size = 8; Vector3 pos = new Vector3(0.0f, cubeSize * 2, 0.0f); float offset = -size * (cubeSize * 2.0f + spacing) * 0.5f; switch (benchmark) { case 1: // 3000 BoxShape blockShape = new BoxShape(cubeSize - collisionRadius); mass = 2.0f; for (int k = 0; k < 47; k++) { for (int j = 0; j < size; j++) { pos[2] = offset + (float)j * (cubeSize * 2.0f + spacing); for (int i = 0; i < size; i++) { pos[0] = offset + (float)i * (cubeSize * 2.0f + spacing); RigidBody cmbody = LocalCreateRigidBody(mass, Matrix.Translation(pos), blockShape); } } offset -= 0.05f * spacing * (size - 1); // spacing *= 1.01f; pos[1] += (cubeSize * 2.0f + spacing); } break; case 2: CreatePyramid(new Vector3(-20, 0, 0), 12, new Vector3(cubeSize)); CreateWall(new Vector3(-2.0f, 0.0f, 0.0f), 12, new Vector3(cubeSize)); CreateWall(new Vector3(4.0f, 0.0f, 0.0f), 12, new Vector3(cubeSize)); CreateWall(new Vector3(10.0f, 0.0f, 0.0f), 12, new Vector3(cubeSize)); CreateTowerCircle(new Vector3(25.0f, 0.0f, 0.0f), 8, 24, new Vector3(cubeSize)); break; case 3: // TODO: Ragdolls break; case 4: cubeSize = 1.5f; ConvexHullShape convexHullShape = new ConvexHullShape(); float scaling = 1; convexHullShape.LocalScaling = new Vector3(scaling); for (int i = 0; i < Taru.Vtx.Length / 3; i++) { Vector3 vtx = new Vector3(Taru.Vtx[i * 3], Taru.Vtx[i * 3 + 1], Taru.Vtx[i * 3 + 2]); convexHullShape.AddPoint(vtx * (1.0f / scaling)); } //this will enable polyhedral contact clipping, better quality, slightly slower convexHullShape.InitializePolyhedralFeatures(); for (int k = 0; k < 15; k++) { for (int j = 0; j < size; j++) { pos[2] = offset + (float)j * (cubeSize * 2.0f + spacing); for (int i = 0; i < size; i++) { pos[0] = offset + (float)i * (cubeSize * 2.0f + spacing); LocalCreateRigidBody(mass, Matrix.Translation(pos), convexHullShape); } } offset -= 0.05f * spacing * (size - 1); spacing *= 1.01f; pos[1] += (cubeSize * 2.0f + spacing); } break; case 5: Vector3 boxSize = new Vector3(1.5f); float boxMass = 1.0f; float sphereRadius = 1.5f; float sphereMass = 1.0f; float capsuleHalf = 2.0f; float capsuleRadius = 1.0f; float capsuleMass = 1.0f; size = 10; int height = 10; cubeSize = boxSize[0]; spacing = 2.0f; pos = new Vector3(0.0f, 20.0f, 0.0f); offset = -size * (cubeSize * 2.0f + spacing) * 0.5f; int numBodies = 0; Random random = new Random(); for (int k = 0; k < height; k++) { for (int j = 0; j < size; j++) { pos[2] = offset + (float)j * (cubeSize * 2.0f + spacing); for (int i = 0; i < size; i++) { pos[0] = offset + (float)i * (cubeSize * 2.0f + spacing); Vector3 bpos = new Vector3(0, 25, 0) + new Vector3(5.0f * pos.X, pos.Y, 5.0f * pos.Z); int idx = random.Next(10); Matrix trans = Matrix.Translation(bpos); switch (idx) { case 0: case 1: case 2: { float r = 0.5f * (idx + 1); BoxShape boxShape = new BoxShape(boxSize * r); LocalCreateRigidBody(boxMass * r, trans, boxShape); } break; case 3: case 4: case 5: { float r = 0.5f * (idx - 3 + 1); SphereShape sphereShape = new SphereShape(sphereRadius * r); LocalCreateRigidBody(sphereMass * r, trans, sphereShape); } break; case 6: case 7: case 8: { float r = 0.5f * (idx - 6 + 1); CapsuleShape capsuleShape = new CapsuleShape(capsuleRadius * r, capsuleHalf * r); LocalCreateRigidBody(capsuleMass * r, trans, capsuleShape); } break; } numBodies++; } } offset -= 0.05f * spacing * (size - 1); spacing *= 1.1f; pos[1] += (cubeSize * 2.0f + spacing); } //CreateLargeMeshBody(); break; case 6: boxSize = new Vector3(1.5f, 1.5f, 1.5f); convexHullShape = new ConvexHullShape(); for (int i = 0; i < Taru.Vtx.Length / 3; i++) { Vector3 vtx = new Vector3(Taru.Vtx[i * 3], Taru.Vtx[i * 3 + 1], Taru.Vtx[i * 3 + 2]); convexHullShape.AddPoint(vtx); } size = 10; height = 10; cubeSize = boxSize[0]; spacing = 2.0f; pos = new Vector3(0.0f, 20.0f, 0.0f); offset = -size * (cubeSize * 2.0f + spacing) * 0.5f; for (int k = 0; k < height; k++) { for (int j = 0; j < size; j++) { pos[2] = offset + (float)j * (cubeSize * 2.0f + spacing); for (int i = 0; i < size; i++) { pos[0] = offset + (float)i * (cubeSize * 2.0f + spacing); Vector3 bpos = new Vector3(0, 25, 0) + new Vector3(5.0f * pos.X, pos.Y, 5.0f * pos.Z); LocalCreateRigidBody(mass, Matrix.Translation(bpos), convexHullShape); } } offset -= 0.05f * spacing * (size - 1); spacing *= 1.1f; pos[1] += (cubeSize * 2.0f + spacing); } //CreateLargeMeshBody(); break; case 7: // TODO //CreateTest6(); //InitRays(); break; } }
protected override void OnInitializePhysics() { // collision configuration contains default setup for memory, collision setup CollisionConf = new DefaultCollisionConfiguration(); Dispatcher = new CollisionDispatcher(CollisionConf); Vector3 worldMin = new Vector3(-1000, -1000, -1000); Vector3 worldMax = new Vector3(1000, 1000, 1000); Broadphase = new AxisSweep3(worldMin, worldMax); Solver = new SequentialImpulseConstraintSolver(); World = new DiscreteDynamicsWorld(Dispatcher, Broadphase, Solver, CollisionConf); World.SolverInfo.SplitImpulse = 1; World.Gravity = new Vector3(0, -10, 0); IsDebugDrawEnabled = true; CollisionShape colShape = new BoxShape(1); CollisionShapes.Add(colShape); for (int j = 0; j < NumDynamicBoxesX; j++) { for (int i = 0; i < NumDynamicBoxesY; i++) { //CollisionShape colShape = new CapsuleShape(0.5f,2.0f);//boxShape = new SphereShape(1.0f); Matrix startTransform = Matrix.Translation(5 * (i - NumDynamicBoxesX / 2), 10, 5 * (j - NumDynamicBoxesY / 2)); LocalCreateRigidBody(1.0f, startTransform, colShape); } } SetVertexPositions(WaveHeight, 0.0f); const bool useQuantizedAabbCompression = true; groundShape = new BvhTriangleMeshShape(indexVertexArrays, useQuantizedAabbCompression); CollisionShapes.Add(groundShape); staticBody = LocalCreateRigidBody(0.0f, Matrix.Identity, groundShape); staticBody.CollisionFlags |= CollisionFlags.StaticObject; staticBody.UserObject = "Ground"; }
protected override void OnInitializePhysics() { // collision configuration contains default setup for memory, collision setup CollisionConf = new SoftBodyRigidBodyCollisionConfiguration(); Dispatcher = new CollisionDispatcher(CollisionConf); Broadphase = new AxisSweep3(new Vector3(-1000, -1000, -1000), new Vector3(1000, 1000, 1000), maxProxies); // the default constraint solver. Solver = new SequentialImpulseConstraintSolver(); softBodyWorldInfo = new SoftBodyWorldInfo { AirDensity = 1.2f, WaterDensity = 0, WaterOffset = 0, WaterNormal = Vector3.Zero, Gravity = new Vector3(0, -10, 0), Dispatcher = Dispatcher, Broadphase = Broadphase }; softBodyWorldInfo.SparseSdf.Initialize(); World = new SoftRigidDynamicsWorld(Dispatcher, Broadphase, Solver, CollisionConf); World.Gravity = new Vector3(0, -10, 0); World.DispatchInfo.EnableSpu = true; World.SetInternalTickCallback(PickingPreTickCallback, this, true); InitializeDemo(); }
public static void InitBullet() { // collision configuration contains default setup for memory, collision setup. Advanced users can create their own configuration. BtEngineCollisionConfiguration = new DefaultCollisionConfiguration(); // use the default collision dispatcher. For parallel processing you can use a diffent dispatcher (see Extras/BulletMultiThreaded) BtEngineDispatcher = new CollisionDispatcher(BtEngineCollisionConfiguration); BtEngineDispatcher.NearCallback = RoomNearCallback; // btDbvtBroadphase is a good general purpose broadphase. You can also try out btAxis3Sweep. BtEngineOverlappingPairCache = new DbvtBroadphase(); BtEngineGhostPairCallback = new GhostPairCallback(); BtEngineOverlappingPairCache.OverlappingPairCache.SetInternalGhostPairCallback(BtEngineGhostPairCallback); // the default constraint solver. For parallel processing you can use a different solver (see Extras/BulletMultiThreaded) BtEngineSolver = new SequentialImpulseConstraintSolver(); BtEngineDynamicsWorld = new DiscreteDynamicsWorld(BtEngineDispatcher, BtEngineOverlappingPairCache, BtEngineSolver, BtEngineCollisionConfiguration); BtEngineDynamicsWorld.SetInternalTickCallback(InternalTickCallback); BtEngineDynamicsWorld.Gravity = new BulletSharp.Math.Vector3(0, 0, -4500.0f); DebugDrawer = new RenderDebugDrawer(); DebugDrawer.DebugMode = DebugDrawModes.DrawWireframe | DebugDrawModes.DrawConstraints; BtEngineDynamicsWorld.DebugDrawer = DebugDrawer; //Global.BtEngineDynamicsWorld.PairCache.SetInternalGhostPairCallback(Global.BtEngineFilterCallback); }
public void SetUp() { _conf = new DefaultCollisionConfiguration(); _dispatcher = new CollisionDispatcher(_conf); _broadphase = new DbvtBroadphase(); _solver = new SequentialImpulseConstraintSolver(); _world = new DiscreteDynamicsWorld(_dispatcher, _broadphase, _solver, _conf); }
public Physics() { ManifoldPoint.ContactAdded += CustomMaterialCombinerCallback; // collision configuration contains default setup for memory, collision setup CollisionConf = new DefaultCollisionConfiguration(); Dispatcher = new CollisionDispatcher(CollisionConf); Broadphase = new AxisSweep3(new Vector3(-10000, -10000, -10000), new Vector3(10000, 10000, 10000)); Solver = new SequentialImpulseConstraintSolver(); World = new DiscreteDynamicsWorld(Dispatcher, Broadphase, Solver, CollisionConf); World.Gravity = new Vector3(0, -10, 0); const int totalVerts = NumVertsX * NumVertsY; const int totalTriangles = 2 * (NumVertsX - 1) * (NumVertsY - 1); gVertices = new Vector3[totalVerts]; int[] gIndices = new int[totalTriangles * 3]; BulletMaterial[] gMaterials = new BulletMaterial[2]; int[] gFaceMaterialIndices = new int[totalTriangles]; // Explicitly set up the materials. It's a small array so let's do it bit by bit. gMaterials[0].Friction = 0; gMaterials[0].Restitution = 0.9f; gMaterials[1].Friction = 0.9f; gMaterials[1].Restitution = 0.1f; int i; // Set up the vertex data SetVertexPositions(waveheight, 0.0f); int index = 0; // Set up the face data for (i = 0; i < NumVertsX - 1; i++) { for (int j = 0; j < NumVertsY - 1; j++) { gIndices[index++] = j * NumVertsX + i; gIndices[index++] = j * NumVertsX + i + 1; gIndices[index++] = (j + 1) * NumVertsX + i + 1; gIndices[index++] = j * NumVertsX + i; gIndices[index++] = (j + 1) * NumVertsX + i + 1; gIndices[index++] = (j + 1) * NumVertsX + i; } } // Set up the face->material index data for (int a = 0; a < totalTriangles; a++) { // This will give the first half of the faces low friction and high restitution // and the second half of the faces high friction and low restitution if (a > totalTriangles / 2) gFaceMaterialIndices[a] = 0; else gFaceMaterialIndices[a] = 1; } // Create the array structure TriangleIndexVertexMaterialArray indexVertexArrays = new TriangleIndexVertexMaterialArray( gIndices, gVertices, gMaterials, gFaceMaterialIndices); // Create the multimaterial mesh shape trimeshShape = new MultimaterialTriangleMeshShape(indexVertexArrays, true); CollisionShapes.Add(trimeshShape); // create the ground //CollisionShape groundShape = new BoxShape(50, 1, 50); //CollisionShapes.PushBack(groundShape); CollisionObject ground = LocalCreateRigidBody(0, Matrix.Identity, trimeshShape); ground.UserObject = "Ground"; ground.CollisionFlags |= CollisionFlags.StaticObject | CollisionFlags.CustomMaterialCallback; CollisionShape colShape = new BoxShape(0.5f); CollisionShapes.Add(colShape); for (i = 0; i < 12; i++) { RigidBody body = LocalCreateRigidBody(1, Matrix.Translation(10 - i, 10, -20 + i * 3), colShape); body.CollisionFlags |= CollisionFlags.CustomMaterialCallback; body.Friction = 0.9f; body.Gravity = new Vector3(0, -20, 0); body.ApplyCentralImpulse(new Vector3(-7.7f, 0, 0)); } }
protected void Dispose(bool disposing) { if (debugType >= BDebug.DebugType.Debug) Debug.Log("BDynamicsWorld Disposing physics."); if (lateUpdateHelper != null) { lateUpdateHelper.m_ddWorld = null; lateUpdateHelper.m_world = null; } if (m_world != null) { //remove/dispose constraints int i; if (_ddWorld != null) { if (debugType >= BDebug.DebugType.Debug) Debug.LogFormat("Removing Constraints {0}", _ddWorld.NumConstraints); for (i = _ddWorld.NumConstraints - 1; i >= 0; i--) { TypedConstraint constraint = _ddWorld.GetConstraint(i); _ddWorld.RemoveConstraint(constraint); if (constraint.Userobject is BTypedConstraint) ((BTypedConstraint)constraint.Userobject).m_isInWorld = false; if (debugType >= BDebug.DebugType.Debug) Debug.LogFormat("Removed Constaint {0}", constraint.Userobject); constraint.Dispose(); } } if (debugType >= BDebug.DebugType.Debug) Debug.LogFormat("Removing Collision Objects {0}", _ddWorld.NumCollisionObjects); //remove the rigidbodies from the dynamics world and delete them for (i = m_world.NumCollisionObjects - 1; i >= 0; i--) { CollisionObject obj = m_world.CollisionObjectArray[i]; RigidBody body = obj as RigidBody; if (body != null && body.MotionState != null) { Debug.Assert(body.NumConstraintRefs == 0, "Rigid body still had constraints"); body.MotionState.Dispose(); } m_world.RemoveCollisionObject(obj); if (obj.UserObject is BCollisionObject) ((BCollisionObject)obj.UserObject).isInWorld = false; if (debugType >= BDebug.DebugType.Debug) Debug.LogFormat("Removed CollisionObject {0}", obj.UserObject); obj.Dispose(); } if (m_world.DebugDrawer != null) { if (m_world.DebugDrawer is IDisposable) { IDisposable dis = (IDisposable)m_world.DebugDrawer; dis.Dispose(); } } m_world.Dispose(); Broadphase.Dispose(); Dispatcher.Dispose(); CollisionConf.Dispose(); _ddWorld = null; m_world = null; } if (Broadphase != null) { Broadphase.Dispose(); Broadphase = null; } if (Dispatcher != null) { Dispatcher.Dispose(); Dispatcher = null; } if (CollisionConf != null) { CollisionConf.Dispose(); CollisionConf = null; } if (Solver != null) { Solver.Dispose(); Solver = null; } if (softBodyWorldInfo != null) { softBodyWorldInfo.Dispose(); softBodyWorldInfo = null; } _isDisposed = true; singleton = null; }
protected override void OnInitializePhysics() { // collision configuration contains default setup for memory, collision setup CollisionConf = new DefaultCollisionConfiguration(); Dispatcher = new CollisionDispatcher(CollisionConf); Vector3 worldMin = new Vector3(-1000, -1000, -1000); Vector3 worldMax = new Vector3(1000, 1000, 1000); Broadphase = new AxisSweep3(worldMin, worldMax); Solver = new SequentialImpulseConstraintSolver(); World = new DiscreteDynamicsWorld(Dispatcher, Broadphase, Solver, CollisionConf); World.SolverInfo.SplitImpulse = 1; World.Gravity = new Vector3(0, -10, 0); const int totalVerts = NUM_VERTS_X * NUM_VERTS_Y; const int totalTriangles = 2 * (NUM_VERTS_X - 1) * (NUM_VERTS_Y - 1); indexVertexArrays = new TriangleIndexVertexArray(); IndexedMesh mesh = new IndexedMesh(); mesh.Allocate(totalVerts, totalTriangles, 3 * sizeof(int), Vector3.SizeInBytes, PhyScalarType.Int32, PhyScalarType.Single); using (var indices = mesh.LockIndices()) { for (int i = 0; i < NUM_VERTS_X - 1; i++) { for (int j = 0; j < NUM_VERTS_Y - 1; j++) { indices.Write(j * NUM_VERTS_X + i); indices.Write(j * NUM_VERTS_X + i + 1); indices.Write((j + 1) * NUM_VERTS_X + i + 1); indices.Write(j * NUM_VERTS_X + i); indices.Write((j + 1) * NUM_VERTS_X + i + 1); indices.Write((j + 1) * NUM_VERTS_X + i); } } } indexVertexArrays.AddIndexedMesh(mesh); raycastBar = new RaycastBar(4000.0f, 0.0f); //raycastBar = new RaycastBar(true, 40.0f, -50.0f, 50.0f); CollisionShape colShape = new BoxShape(1); CollisionShapes.Add(colShape); for (int i = 0; i < 10; i++) { //CollisionShape colShape = new CapsuleShape(0.5f,2.0f);//boxShape = new SphereShape(1.0f); Matrix startTransform = Matrix.Translation(2 * i, 10, 1); LocalCreateRigidBody(1.0f, startTransform, colShape); } SetVertexPositions(waveHeight, 0.0f); const bool useQuantizedAabbCompression = true; groundShape = new BvhTriangleMeshShape(indexVertexArrays, useQuantizedAabbCompression); CollisionShapes.Add(groundShape); staticBody = LocalCreateRigidBody(0.0f, Matrix.Identity, groundShape); staticBody.CollisionFlags |= CollisionFlags.StaticObject; staticBody.UserObject = "Ground"; }
public void Create() { if (created) { this.Destroy(); } this.bodyindex = 0; this.cstindex = 0; collisionConfiguration = new SoftBodyRigidBodyCollisionConfiguration(); dispatcher = new CollisionDispatcher(collisionConfiguration); solver = new SequentialImpulseConstraintSolver(); overlappingPairCache = new DbvtBroadphase(); dynamicsWorld = new SoftRigidDynamicsWorld(dispatcher, overlappingPairCache, solver, collisionConfiguration); worldInfo = new SoftBodyWorldInfo(); worldInfo.Gravity = dynamicsWorld.Gravity; worldInfo.Broadphase = overlappingPairCache; worldInfo.Dispatcher = dispatcher; worldInfo.SparseSdf.Initialize(); this.created = true; if (this.WorldHasReset != null) { this.WorldHasReset(); } }
/* Does not set any local variables. Is safe to use to create duplicate physics worlds for independant simulation. */ public bool CreatePhysicsWorld( out CollisionWorld world, out CollisionConfiguration collisionConfig, out CollisionDispatcher dispatcher, out BroadphaseInterface broadphase, out SequentialImpulseConstraintSolver solver, out SoftBodyWorldInfo softBodyWorldInfo) { bool success = true; if (m_worldType == WorldType.SoftBodyAndRigidBody && m_collisionType == CollisionConfType.DefaultDynamicsWorldCollisionConf) { BDebug.LogError(debugType, "For World Type = SoftBodyAndRigidBody collisionType must be collisionType=SoftBodyRigidBodyCollisionConf. Switching"); m_collisionType = CollisionConfType.SoftBodyRigidBodyCollisionConf; success = false; } collisionConfig = null; if (m_collisionType == CollisionConfType.DefaultDynamicsWorldCollisionConf) { collisionConfig = new DefaultCollisionConfiguration(); } else if (m_collisionType == CollisionConfType.SoftBodyRigidBodyCollisionConf) { collisionConfig = new SoftBodyRigidBodyCollisionConfiguration(); } dispatcher = new CollisionDispatcher(collisionConfig); if (m_broadphaseType == BroadphaseType.DynamicAABBBroadphase) { broadphase = new DbvtBroadphase(); } else if (m_broadphaseType == BroadphaseType.Axis3SweepBroadphase) { broadphase = new AxisSweep3(m_axis3SweepBroadphaseMin.ToBullet(), m_axis3SweepBroadphaseMax.ToBullet(), axis3SweepMaxProxies); } else if (m_broadphaseType == BroadphaseType.Axis3SweepBroadphase_32bit) { broadphase = new AxisSweep3_32Bit(m_axis3SweepBroadphaseMin.ToBullet(), m_axis3SweepBroadphaseMax.ToBullet(), axis3SweepMaxProxies); } else { broadphase = null; } world = null; softBodyWorldInfo = null; solver = null; if (m_worldType == WorldType.CollisionOnly) { world = new CollisionWorld(dispatcher, broadphase, collisionConfig); } else if (m_worldType == WorldType.RigidBodyDynamics) { world = new DiscreteDynamicsWorld(dispatcher, broadphase, null, collisionConfig); } else if (m_worldType == WorldType.MultiBodyWorld) { world = new MultiBodyDynamicsWorld(dispatcher, broadphase, null, collisionConfig); } else if (m_worldType == WorldType.SoftBodyAndRigidBody) { solver = new SequentialImpulseConstraintSolver(); solver.RandSeed = sequentialImpulseConstraintSolverRandomSeed; softBodyWorldInfo = new SoftBodyWorldInfo { AirDensity = 1.2f, WaterDensity = 0, WaterOffset = 0, WaterNormal = BulletSharp.Math.Vector3.Zero, Gravity = UnityEngine.Physics.gravity.ToBullet(), Dispatcher = dispatcher, Broadphase = broadphase }; softBodyWorldInfo.SparseSdf.Initialize(); world = new SoftRigidDynamicsWorld(dispatcher, broadphase, solver, collisionConfig); world.DispatchInfo.EnableSpu = true; softBodyWorldInfo.SparseSdf.Reset(); softBodyWorldInfo.AirDensity = 1.2f; softBodyWorldInfo.WaterDensity = 0; softBodyWorldInfo.WaterOffset = 0; softBodyWorldInfo.WaterNormal = BulletSharp.Math.Vector3.Zero; softBodyWorldInfo.Gravity = m_gravity.ToBullet(); } if (world is DiscreteDynamicsWorld) { ((DiscreteDynamicsWorld)world).Gravity = m_gravity.ToBullet(); } if (_doDebugDraw) { DebugDrawUnity db = new DebugDrawUnity(); db.DebugMode = _debugDrawMode; world.DebugDrawer = db; } return success; }
protected override void OnInitializePhysics() { CollisionShape groundShape = new BoxShape(50, 3, 50); CollisionShapes.Add(groundShape); CollisionConf = new DefaultCollisionConfiguration(); Dispatcher = new CollisionDispatcher(CollisionConf); Solver = new SequentialImpulseConstraintSolver(); Vector3 worldMin = new Vector3(-10000, -10000, -10000); Vector3 worldMax = new Vector3(10000, 10000, 10000); Broadphase = new AxisSweep3(worldMin, worldMax); //Broadphase = new DbvtBroadphase(); World = new DiscreteDynamicsWorld(Dispatcher, Broadphase, Solver, CollisionConf); int i; Matrix tr; Matrix vehicleTr; //if (UseTrimeshGround) { const float scale = 20.0f; //create a triangle-mesh ground const int NumVertsX = 20; const int NumVertsY = 20; const int totalVerts = NumVertsX * NumVertsY; const int totalTriangles = 2 * (NumVertsX - 1) * (NumVertsY - 1); TriangleIndexVertexArray vertexArray = new TriangleIndexVertexArray(); IndexedMesh mesh = new IndexedMesh(); mesh.Allocate(totalTriangles, totalVerts); mesh.NumTriangles = totalTriangles; mesh.NumVertices = totalVerts; mesh.TriangleIndexStride = 3 * sizeof(int); mesh.VertexStride = Vector3.SizeInBytes; using (var indicesStream = mesh.GetTriangleStream()) { var indices = new BinaryWriter(indicesStream); for (i = 0; i < NumVertsX - 1; i++) { for (int j = 0; j < NumVertsY - 1; j++) { indices.Write(j * NumVertsX + i); indices.Write(j * NumVertsX + i + 1); indices.Write((j + 1) * NumVertsX + i + 1); indices.Write(j * NumVertsX + i); indices.Write((j + 1) * NumVertsX + i + 1); indices.Write((j + 1) * NumVertsX + i); } } indices.Dispose(); } using (var vertexStream = mesh.GetVertexStream()) { var vertices = new BinaryWriter(vertexStream); for (i = 0; i < NumVertsX; i++) { for (int j = 0; j < NumVertsY; j++) { float wl = .2f; float height = 20.0f * (float)(Math.Sin(i * wl) * Math.Cos(j * wl)); vertices.Write((i - NumVertsX * 0.5f) * scale); vertices.Write(height); vertices.Write((j - NumVertsY * 0.5f) * scale); } } vertices.Dispose(); } vertexArray.AddIndexedMesh(mesh); groundShape = new BvhTriangleMeshShape(vertexArray, true); tr = Matrix.Identity; vehicleTr = Matrix.Translation(0, -2, 0); }/* else { // Use HeightfieldTerrainShape int width = 40, length = 40; //int width = 128, length = 128; // Debugging is too slow for this float maxHeight = 10.0f; float heightScale = maxHeight / 256.0f; Vector3 scale = new Vector3(20.0f, maxHeight, 20.0f); //PhyScalarType scalarType = PhyScalarType.PhyUChar; //FileStream file = new FileStream(heightfieldFile, FileMode.Open, FileAccess.Read); // Use float data PhyScalarType scalarType = PhyScalarType.PhyFloat; byte[] terr = new byte[width * length * 4]; MemoryStream file = new MemoryStream(terr); BinaryWriter writer = new BinaryWriter(file); for (i = 0; i < width; i++) for (int j = 0; j < length; j++) writer.Write((float)((maxHeight / 2) + 4 * Math.Sin(j * 0.5f) * Math.Cos(i))); writer.Flush(); file.Position = 0; HeightfieldTerrainShape heightterrainShape = new HeightfieldTerrainShape(width, length, file, heightScale, 0, maxHeight, upIndex, scalarType, false); heightterrainShape.SetUseDiamondSubdivision(true); groundShape = heightterrainShape; groundShape.LocalScaling = new Vector3(scale.X, 1, scale.Z); tr = Matrix.Translation(new Vector3(-scale.X / 2, scale.Y / 2, -scale.Z / 2)); vehicleTr = Matrix.Translation(new Vector3(20, 3, -3)); // Create graphics object file.Position = 0; BinaryReader reader = new BinaryReader(file); int totalTriangles = (width - 1) * (length - 1) * 2; int totalVerts = width * length; game.groundMesh = new Mesh(game.Device, totalTriangles, totalVerts, MeshFlags.SystemMemory | MeshFlags.Use32Bit, VertexFormat.Position | VertexFormat.Normal); SlimDX.DataStream data = game.groundMesh.LockVertexBuffer(LockFlags.None); for (i = 0; i < width; i++) { for (int j = 0; j < length; j++) { float height; if (scalarType == PhyScalarType.PhyFloat) { // heightScale isn't applied internally for float data height = reader.ReadSingle(); } else if (scalarType == PhyScalarType.PhyUChar) { height = file.ReadByte() * heightScale; } else { height = 0.0f; } data.Write((j - length * 0.5f) * scale.X); data.Write(height); data.Write((i - width * 0.5f) * scale.Z); // Normals will be calculated later data.Position += 12; } } game.groundMesh.UnlockVertexBuffer(); file.Close(); data = game.groundMesh.LockIndexBuffer(LockFlags.None); for (i = 0; i < width - 1; i++) { for (int j = 0; j < length - 1; j++) { // Using diamond subdivision if ((j + i) % 2 == 0) { data.Write(j * width + i); data.Write((j + 1) * width + i + 1); data.Write(j * width + i + 1); data.Write(j * width + i); data.Write((j + 1) * width + i); data.Write((j + 1) * width + i + 1); } else { data.Write(j * width + i); data.Write((j + 1) * width + i); data.Write(j * width + i + 1); data.Write(j * width + i + 1); data.Write((j + 1) * width + i); data.Write((j + 1) * width + i + 1); } / * // Not using diamond subdivision data.Write(j * width + i); data.Write((j + 1) * width + i); data.Write(j * width + i + 1); data.Write(j * width + i + 1); data.Write((j + 1) * width + i); data.Write((j + 1) * width + i + 1); * / } } game.groundMesh.UnlockIndexBuffer(); game.groundMesh.ComputeNormals(); }*/ CollisionShapes.Add(groundShape); //create ground object RigidBody ground = LocalCreateRigidBody(0, tr, groundShape); ground.UserObject = "Ground"; CollisionShape chassisShape = new BoxShape(1.0f, 0.5f, 2.0f); CollisionShapes.Add(chassisShape); CompoundShape compound = new CompoundShape(); CollisionShapes.Add(compound); //localTrans effectively shifts the center of mass with respect to the chassis Matrix localTrans = Matrix.Translation(Vector3.UnitY); compound.AddChildShape(localTrans, chassisShape); RigidBody carChassis = LocalCreateRigidBody(800, Matrix.Identity, compound); carChassis.UserObject = "Chassis"; //carChassis.SetDamping(0.2f, 0.2f); //CylinderShapeX wheelShape = new CylinderShapeX(wheelWidth, wheelRadius, wheelRadius); // clientResetScene(); // create vehicle VehicleTuning tuning = new VehicleTuning(); IVehicleRaycaster vehicleRayCaster = new DefaultVehicleRaycaster(World); //vehicle = new RaycastVehicle(tuning, carChassis, vehicleRayCaster); vehicle = new CustomVehicle(tuning, carChassis, vehicleRayCaster); carChassis.ActivationState = ActivationState.DisableDeactivation; World.AddAction(vehicle); const float connectionHeight = 1.2f; bool isFrontWheel = true; // choose coordinate system vehicle.SetCoordinateSystem(rightIndex, upIndex, forwardIndex); BulletSharp.Math.Vector3 connectionPointCS0 = new Vector3(CUBE_HALF_EXTENTS - (0.3f * wheelWidth), connectionHeight, 2 * CUBE_HALF_EXTENTS - wheelRadius); vehicle.AddWheel(connectionPointCS0, wheelDirectionCS0, wheelAxleCS, suspensionRestLength, wheelRadius, tuning, isFrontWheel); connectionPointCS0 = new Vector3(-CUBE_HALF_EXTENTS + (0.3f * wheelWidth), connectionHeight, 2 * CUBE_HALF_EXTENTS - wheelRadius); vehicle.AddWheel(connectionPointCS0, wheelDirectionCS0, wheelAxleCS, suspensionRestLength, wheelRadius, tuning, isFrontWheel); isFrontWheel = false; connectionPointCS0 = new Vector3(-CUBE_HALF_EXTENTS + (0.3f * wheelWidth), connectionHeight, -2 * CUBE_HALF_EXTENTS + wheelRadius); vehicle.AddWheel(connectionPointCS0, wheelDirectionCS0, wheelAxleCS, suspensionRestLength, wheelRadius, tuning, isFrontWheel); connectionPointCS0 = new Vector3(CUBE_HALF_EXTENTS - (0.3f * wheelWidth), connectionHeight, -2 * CUBE_HALF_EXTENTS + wheelRadius); vehicle.AddWheel(connectionPointCS0, wheelDirectionCS0, wheelAxleCS, suspensionRestLength, wheelRadius, tuning, isFrontWheel); for (i = 0; i < vehicle.NumWheels; i++) { WheelInfo wheel = vehicle.GetWheelInfo(i); wheel.SuspensionStiffness = suspensionStiffness; wheel.WheelsDampingRelaxation = suspensionDamping; wheel.WheelsDampingCompression = suspensionCompression; wheel.FrictionSlip = wheelFriction; wheel.RollInfluence = rollInfluence; } vehicle.RigidBody.WorldTransform = vehicleTr; }
/// <summary> /// Creates the world /// </summary> void CreateWorld(string levelName) { Launch.Log("[PhysicsMain] Creating new world..."); // have to make more of these every level because disposing the world apparently disposes of them too. broadphase = new DbvtBroadphase(); solver = new SequentialImpulseConstraintSolver(); dcc = new DefaultCollisionConfiguration(); dispatcher = new CollisionDispatcher(dcc); // set up this stuff... not quite sure what it's for, but you need it if you want the CCD to work for the karts dispatcher.RegisterCollisionCreateFunc(BroadphaseNativeType.ConvexHullShape, BroadphaseNativeType.ConvexHullShape, dcc.GetCollisionAlgorithmCreateFunc(BroadphaseNativeType.TriangleMeshShape, BroadphaseNativeType.TriangleMeshShape)); dispatcher.RegisterCollisionCreateFunc(BroadphaseNativeType.ConvexHullShape, BroadphaseNativeType.ConvexHullShape, dcc.GetCollisionAlgorithmCreateFunc(BroadphaseNativeType.ConvexHullShape, BroadphaseNativeType.ConvexHullShape)); world = new DiscreteDynamicsWorld(dispatcher, broadphase, solver, dcc); // and then turn on CCD world.DispatchInfo.UseContinuous = true; world.Gravity = new Vector3(0, Settings.Default.Gravity, 0); ManifoldPoint.ContactAddedCallback = ContactAdded; }
protected override void OnInitializePhysics() { int i; shootBoxInitialSpeed = 4000; // collision configuration contains default setup for memory, collision setup CollisionConf = new DefaultCollisionConfiguration(); Dispatcher = new CollisionDispatcher(CollisionConf); //Dispatcher.RegisterCollisionCreateFunc(BroadphaseNativeType.BoxShape, BroadphaseNativeType.BoxShape, // CollisionConf.GetCollisionAlgorithmCreateFunc(BroadphaseNativeType.ConvexShape, BroadphaseNativeType.ConvexShape)); Broadphase = new DbvtBroadphase(); // the default constraint solver. Solver = new SequentialImpulseConstraintSolver(); World = new DiscreteDynamicsWorld(Dispatcher, Broadphase, Solver, CollisionConf); World.SolverInfo.SolverMode |= SolverModes.Use2FrictionDirections | SolverModes.RandomizeOrder; //World.SolverInfo.SplitImpulse = 0; World.SolverInfo.NumIterations = 20; World.DispatchInfo.UseContinuous = ccdMode; World.Gravity = new Vector3(0, -10, 0); BoxShape ground = new BoxShape(200, 1, 200); ground.InitializePolyhedralFeatures(); CollisionShapes.Add(ground); RigidBody body = LocalCreateRigidBody(0, Matrix.Identity, ground); body.Friction = 0.5f; //body.RollingFriction = 0.3f; body.UserObject = "Ground"; //CollisionShape shape = new CylinderShape(CubeHalfExtents, CubeHalfExtents, CubeHalfExtents); CollisionShape shape = new BoxShape(CubeHalfExtents, CubeHalfExtents, CubeHalfExtents); CollisionShapes.Add(shape); const int numObjects = 120; for (i = 0; i < numObjects; i++) { //stack them const int colsize = 10; int row = (int)((i * CubeHalfExtents * 2) / (colsize * 2 * CubeHalfExtents)); int row2 = row; int col = (i) % (colsize) - colsize / 2; if (col > 3) { col = 11; row2 |= 1; } Matrix trans = Matrix.Translation(col * 2 * CubeHalfExtents + (row2 % 2) * CubeHalfExtents, row * 2 * CubeHalfExtents + CubeHalfExtents + ExtraHeight, 0); body = LocalCreateRigidBody(1, trans, shape); body.SetAnisotropicFriction(shape.AnisotropicRollingFrictionDirection, AnisotropicFrictionFlags.AnisotropicRollingFriction); body.Friction = 0.5f; //body.RollingFriction = 0.3f; if (ccdMode) { body.CcdMotionThreshold = 1e-7f; body.CcdSweptSphereRadius = 0.9f * CubeHalfExtents; } } }
protected override void OnInitializePhysics() { // collision configuration contains default setup for memory, collision setup CollisionConf = new DefaultCollisionConfiguration(); Dispatcher = new CollisionDispatcher(CollisionConf); //Broadphase = new SimpleBroadphase(); Broadphase = new AxisSweep3_32Bit(new Vector3(-10000, -10000, -10000), new Vector3(10000, 10000, 10000), 1024); Solver = new SequentialImpulseConstraintSolver(); World = new DiscreteDynamicsWorld(Dispatcher, Broadphase, Solver, CollisionConf); World.Gravity = new Vector3(0, -10, 0); // create trimesh model and shape InitGImpactCollision(); // Create Scene float mass = 0.0f; CollisionShape staticboxShape1 = new BoxShape(200, 1, 200);//floor CollisionShapes.Add(staticboxShape1); LocalCreateRigidBody(mass, Matrix.Translation(0, -10, 0), staticboxShape1); CollisionShape staticboxShape2 = new BoxShape(1, 50, 200);//left wall CollisionShapes.Add(staticboxShape2); LocalCreateRigidBody(mass, Matrix.Translation(-200, 15, 0), staticboxShape2); CollisionShape staticboxShape3 = new BoxShape(1, 50, 200);//right wall CollisionShapes.Add(staticboxShape3); LocalCreateRigidBody(mass, Matrix.Translation(200, 15, 0), staticboxShape3); CollisionShape staticboxShape4 = new BoxShape(200, 50, 1);//front wall CollisionShapes.Add(staticboxShape4); LocalCreateRigidBody(mass, Matrix.Translation(0, 15, 200), staticboxShape4); CollisionShape staticboxShape5 = new BoxShape(200, 50, 1);//back wall CollisionShapes.Add(staticboxShape5); LocalCreateRigidBody(mass, Matrix.Translation(0, 15, -200), staticboxShape5); //static plane Vector3 normal = new Vector3(-0.5f, 0.5f, 0.0f); normal.Normalize(); CollisionShape staticplaneShape6 = new StaticPlaneShape(normal, 0.5f);// A plane CollisionShapes.Add(staticplaneShape6); /*RigidBody staticBody2 =*/ LocalCreateRigidBody(mass, Matrix.Translation(0, -9, 0), staticplaneShape6); //another static plane normal = new Vector3(0.5f, 0.7f, 0.0f); //normal.Normalize(); CollisionShape staticplaneShape7 = new StaticPlaneShape(normal, 0.0f);// A plane CollisionShapes.Add(staticplaneShape7); /*staticBody2 =*/ LocalCreateRigidBody(mass, Matrix.Translation(0, -10, 0), staticplaneShape7); // Create Static Torus float height = 28; const float step = 2.5f; const float massT = 1.0f; Matrix startTransform = Matrix.RotationQuaternion(Quaternion.RotationYawPitchRoll((float)Math.PI * 0.5f, 0, (float)Math.PI * 0.5f)) * Matrix.Translation(0, height, -5); #if BULLET_GIMPACT kinematicTorus = LocalCreateRigidBody(0, startTransform, trimeshShape); #else //kinematicTorus = LocalCreateRigidBody(0, startTransform, CreateTorusShape()); #endif //kinematicTorus.CollisionFlags = kinematicTorus.CollisionFlags | CollisionFlags.StaticObject; //kinematicTorus.ActivationState = ActivationState.IslandSleeping; kinematicTorus.CollisionFlags = kinematicTorus.CollisionFlags | CollisionFlags.KinematicObject; kinematicTorus.ActivationState = ActivationState.DisableDeactivation; // Kinematic //kinTorusTran = new Vector3(-0.1f, 0, 0); //kinTorusRot = Quaternion.RotationYawPitchRoll(0, (float)Math.PI * 0.01f, 0); #if TEST_GIMPACT_TORUS #if BULLET_GIMPACT // Create dynamic Torus for (int i = 0; i < 6; i++) { height -= step; startTransform = Matrix.RotationQuaternion(Quaternion.RotationYawPitchRoll(0, 0, (float)Math.PI * 0.5f)) * Matrix.Translation(0, height, -5); /*RigidBody bodyA =*/ LocalCreateRigidBody(massT, startTransform, trimeshShape); height -= step; startTransform = Matrix.RotationQuaternion(Quaternion.RotationYawPitchRoll((float)Math.PI * 0.5f, 0, (float)Math.PI * 0.5f)) * Matrix.Translation(0, height, -5); /*RigidBody bodyB =*/ LocalCreateRigidBody(massT, startTransform, trimeshShape); } #else /* // Create dynamic Torus for (int i = 0; i < 6; i++) { height -= step; startTransform.setOrigin(btVector3(0, height, -5)); startTransform.setRotation(btQuaternion(0, 0, 3.14159265 * 0.5)); btRigidBody* bodyA = localCreateRigidBody(massT, startTransform, createTorusShape()); height -= step; startTransform.setOrigin(btVector3(0, height, -5)); startTransform.setRotation(btQuaternion(3.14159265 * 0.5, 0, 3.14159265 * 0.5)); btRigidBody* bodyB = localCreateRigidBody(massT, startTransform, createTorusShape()); } */ #endif #endif // Create Dynamic Boxes for (int i = 0; i < 8; i++) { CollisionShape boxShape = new BoxShape(new Vector3(1, 1, 1)); CollisionShapes.Add(boxShape); LocalCreateRigidBody(1, Matrix.Translation(2 * i - 5, 2, -3), boxShape); } }