public Physics() { // 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); // create the ground CollisionShape groundShape = new BoxShape(50, 50, 50); collisionShapes.Add(groundShape); CollisionObject ground = LocalCreateRigidBody(0, Matrix4.CreateTranslation(0, -50, 0), groundShape); ground.UserObject = "Ground"; // create a few dynamic rigidbodies const float mass = 1.0f; CollisionShape colShape = new BoxShape(1); collisionShapes.Add(colShape); Vector3 localInertia = colShape.CalculateLocalInertia(mass); var rbInfo = new RigidBodyConstructionInfo(mass, null, colShape, localInertia); const float start_x = StartPosX - ArraySizeX / 2; const float start_y = StartPosY; const 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++) { Matrix4 startTransform = Matrix4.CreateTranslation( new Vector3( 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 rbInfo.MotionState = new DefaultMotionState(startTransform); RigidBody body = new RigidBody(rbInfo); // make it drop from a height body.Translate(new Vector3(0, 20, 0)); World.AddRigidBody(body); } } } rbInfo.Dispose(); }
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(); // 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); 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(); } }
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); // create the ground CollisionShape groundShape = new BoxShape(20, 50, 10); CollisionShapes.Add(groundShape); CollisionObject ground = LocalCreateRigidBody(0, Matrix.RotationAxis(new Vector3(0, 0, 1), (float)Math.PI * 0.03f) * Matrix.Translation(0, -50, 0), groundShape); ground.Friction = 1; ground.RollingFriction = 1; ground.UserObject = "Ground"; groundShape = new BoxShape(100, 50, 100); CollisionShapes.Add(groundShape); ground = LocalCreateRigidBody(0, Matrix.Translation(0, -54, 0), groundShape); ground.Friction = 1; ground.RollingFriction = 1; ground.UserObject = "Ground"; // create a few dynamic rigidbodies CollisionShape[] colShapes = { new SphereShape(1), new CapsuleShape(0.5f,1), new CapsuleShapeX(0.5f,1), new CapsuleShapeZ(0.5f,1), new ConeShape(0.5f,1), new ConeShapeX(0.5f,1), new ConeShapeZ(0.5f,1), new CylinderShape(new Vector3(0.5f,1,0.5f)), new CylinderShapeX(new Vector3(1,0.5f,0.5f)), new CylinderShapeZ(new Vector3(0.5f,0.5f,1)), }; foreach (var collisionShape in colShapes) { CollisionShapes.Add(collisionShape); } const float mass = 1.0f; CollisionShape colShape = new BoxShape(1); CollisionShapes.Add(colShape); Vector3 localInertia = colShape.CalculateLocalInertia(mass); var rbInfo = new RigidBodyConstructionInfo(mass, null, null, localInertia); const float startX = StartPosX - ArraySizeX / 2; const float startY = StartPosY; const float startZ = StartPosZ - ArraySizeZ / 2; int shapeIndex = 0; for (int k = 0; k < ArraySizeY; k++) { for (int i = 0; i < ArraySizeX; i++) { for (int j = 0; j < ArraySizeZ; j++) { Matrix startTransform = Matrix.Translation( 2 * i + startX, 2 * k + startY + 20, 2 * j + startZ ); shapeIndex++; // using motionstate is recommended, it provides interpolation capabilities // and only synchronizes 'active' objects rbInfo.MotionState = new DefaultMotionState(startTransform); rbInfo.CollisionShape = colShapes[shapeIndex % colShapes.Length]; RigidBody body = new RigidBody(rbInfo); body.Friction = 1; body.RollingFriction = 0.3f; body.SetAnisotropicFriction(colShape.AnisotropicRollingFrictionDirection, AnisotropicFrictionFlags.RollingFriction); World.AddRigidBody(body); } } } rbInfo.Dispose(); }
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); // create the ground BoxShape groundShape = new BoxShape(50, 1, 50); //groundShape.InitializePolyhedralFeatures(); //CollisionShape groundShape = new StaticPlaneShape(new Vector3(0,1,0), 50); CollisionShapes.Add(groundShape); CollisionObject ground = LocalCreateRigidBody(0, Matrix.Identity, groundShape); ground.UserObject = "Ground"; // create a few dynamic rigidbodies const float mass = 1.0f; BoxShape colShape = new BoxShape(1); CollisionShapes.Add(colShape); Vector3 localInertia = colShape.CalculateLocalInertia(mass); const float startX = StartPosX - ArraySizeX / 2; const float startY = StartPosY; const float startZ = StartPosZ - ArraySizeZ / 2; RigidBodyConstructionInfo rbInfo = new RigidBodyConstructionInfo(mass, null, colShape, localInertia); 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 + startX, 2 * k + startY, 2 * j + startZ ); // using motionstate is recommended, it provides interpolation capabilities // and only synchronizes 'active' objects rbInfo.MotionState = new DefaultMotionState(startTransform); RigidBody body = new RigidBody(rbInfo); // make it drop from a height body.Translate(new Vector3(0, 20, 0)); World.AddRigidBody(body); } } } rbInfo.Dispose(); }
void Start() { //Create a World Debug.Log("Initialize physics"); List<CollisionShape> CollisionShapes = new List<CollisionShape>(); DefaultCollisionConfiguration CollisionConf = new DefaultCollisionConfiguration(); CollisionDispatcher Dispatcher = new CollisionDispatcher(CollisionConf); DbvtBroadphase Broadphase = new DbvtBroadphase(); DiscreteDynamicsWorld World = new DiscreteDynamicsWorld(Dispatcher, Broadphase, null, CollisionConf); World.Gravity = new BulletSharp.Math.Vector3(0, -10, 0); // create a few dynamic rigidbodies const float mass = 1.0f; //Add a single cube RigidBody fallRigidBody; BoxShape shape = new BoxShape(1f, 1f, 1f); BulletSharp.Math.Vector3 localInertia = BulletSharp.Math.Vector3.Zero; shape.CalculateLocalInertia(mass, out localInertia); RigidBodyConstructionInfo rbInfo = new RigidBodyConstructionInfo(mass, null, shape, localInertia); fallRigidBody = new RigidBody(rbInfo); rbInfo.Dispose(); Matrix st = Matrix.Translation(new BulletSharp.Math.Vector3(0f, 10f, 0f)); fallRigidBody.WorldTransform = st; World.AddRigidBody(fallRigidBody); //Step the simulation 300 steps for (int i = 0; i < 300; i++) { World.StepSimulation(1f / 60f, 10); Matrix trans; fallRigidBody.GetWorldTransform(out trans); Debug.Log("box height: " + trans.Origin); } //Clean up. World.RemoveRigidBody(fallRigidBody); fallRigidBody.Dispose(); UnityEngine.Debug.Log("ExitPhysics"); if (World != null) { //remove/dispose constraints int i; for (i = World.NumConstraints - 1; i >= 0; i--) { TypedConstraint constraint = World.GetConstraint(i); World.RemoveConstraint(constraint); constraint.Dispose(); } //remove the rigidbodies from the dynamics world and delete them for (i = World.NumCollisionObjects - 1; i >= 0; i--) { CollisionObject obj = World.CollisionObjectArray[i]; RigidBody body = obj as RigidBody; if (body != null && body.MotionState != null) { body.MotionState.Dispose(); } World.RemoveCollisionObject(obj); obj.Dispose(); } //delete collision shapes foreach (CollisionShape ss in CollisionShapes) ss.Dispose(); CollisionShapes.Clear(); World.Dispose(); Broadphase.Dispose(); Dispatcher.Dispose(); CollisionConf.Dispose(); } if (Broadphase != null) { Broadphase.Dispose(); } if (Dispatcher != null) { Dispatcher.Dispose(); } if (CollisionConf != null) { CollisionConf.Dispose(); } }
//IMPORTANT Time.fixedTime must match the timestep being used here. public static List<UnityEngine.Vector3> SimulateBall(BRigidBody ballRb, UnityEngine.Vector3 ballThrowForce, int numberOfSimulationSteps, bool reverseOrder) { var ballPositions = new List<UnityEngine.Vector3>(numberOfSimulationSteps); //Create a World Debug.Log("Initialize physics"); CollisionConfiguration CollisionConf; CollisionDispatcher Dispatcher; BroadphaseInterface Broadphase; CollisionWorld cw; SequentialImpulseConstraintSolver Solver; BulletSharp.SoftBody.SoftBodyWorldInfo softBodyWorldInfo; //This should create a copy of the BPhysicsWorld with the same settings BPhysicsWorld bw = BPhysicsWorld.Get(); bw.CreatePhysicsWorld(out cw, out CollisionConf, out Dispatcher, out Broadphase, out Solver, out softBodyWorldInfo); World = (DiscreteDynamicsWorld) cw; //Copy all existing rigidbodies in scene // IMPORTANT rigidbodies must be added to the offline world in the same order that they are in the source world // this is because collisions must be resolved in the same order for the sim to be deterministic DiscreteDynamicsWorld sourceWorld = (DiscreteDynamicsWorld) bw.world; BulletSharp.RigidBody bulletBallRb = null; BulletSharp.Math.Matrix mm = BulletSharp.Math.Matrix.Identity; for(int i = 0; i < sourceWorld.NumCollisionObjects; i++) { CollisionObject co = sourceWorld.CollisionObjectArray[i]; if (co != null && co.UserObject is BRigidBody) { BRigidBody rb = (BRigidBody) co.UserObject; float mass = rb.isDynamic() ? rb.mass : 0f; BCollisionShape existingShape = rb.GetComponent<BCollisionShape>(); CollisionShape shape = null; if (existingShape is BSphereShape) { shape = ((BSphereShape)existingShape).CopyCollisionShape(); } else if (existingShape is BBoxShape) { shape = ((BBoxShape)existingShape).CopyCollisionShape(); } RigidBody bulletRB = null; BulletSharp.Math.Vector3 localInertia = new BulletSharp.Math.Vector3(); rb.CreateOrConfigureRigidBody(ref bulletRB, ref localInertia, shape, null); BulletSharp.Math.Vector3 pos = rb.GetCollisionObject().WorldTransform.Origin; BulletSharp.Math.Quaternion rot = rb.GetCollisionObject().WorldTransform.GetOrientation(); BulletSharp.Math.Matrix.AffineTransformation(1f, ref rot, ref pos, out mm); bulletRB.WorldTransform = mm; World.AddRigidBody(bulletRB, rb.groupsIBelongTo, rb.collisionMask); if (rb == ballRb) { bulletBallRb = bulletRB; bulletRB.ApplyCentralImpulse(ballThrowForce.ToBullet()); } } } //Step the simulation numberOfSimulationSteps times for (int i = 0; i < numberOfSimulationSteps; i++) { int numSteps = World.StepSimulation(1f / 60f, 10, 1f / 60f); ballPositions.Add(bulletBallRb.WorldTransform.Origin.ToUnity()); } UnityEngine.Debug.Log("ExitPhysics"); if (World != null) { //remove/dispose constraints int i; for (i = World.NumConstraints - 1; i >= 0; i--) { TypedConstraint constraint = World.GetConstraint(i); World.RemoveConstraint(constraint); constraint.Dispose(); } //remove the rigidbodies from the dynamics world and delete them for (i = World.NumCollisionObjects - 1; i >= 0; i--) { CollisionObject obj = World.CollisionObjectArray[i]; RigidBody body = obj as RigidBody; if (body != null && body.MotionState != null) { body.MotionState.Dispose(); } World.RemoveCollisionObject(obj); obj.Dispose(); } World.Dispose(); Broadphase.Dispose(); Dispatcher.Dispose(); CollisionConf.Dispose(); } if (Broadphase != null) { Broadphase.Dispose(); } if (Dispatcher != null) { Dispatcher.Dispose(); } if (CollisionConf != null) { CollisionConf.Dispose(); } return ballPositions; }