diff --git a/BulletSharpPInvoke/demos/BasicDemo/BasicDemo.cs b/BulletSharpPInvoke/demos/BasicDemo/BasicDemo.cs index 6540ac40..f6794977 100644 --- a/BulletSharpPInvoke/demos/BasicDemo/BasicDemo.cs +++ b/BulletSharpPInvoke/demos/BasicDemo/BasicDemo.cs @@ -7,16 +7,12 @@ namespace BasicDemo { class BasicDemo : Demo { - Vector3 eye = new Vector3(30, 20, 10); - Vector3 target = new Vector3(0, 5, -4); + Vector3 eye = new Vector3(30, 20, 15); + Vector3 target = new Vector3(0, 3, 0); // create 125 (5x5x5) dynamic objects const int ArraySizeX = 5, ArraySizeY = 5, ArraySizeZ = 5; - - // scaling of the objects (0.1 = 20 centimeter boxes ) - const float StartPosX = -5; - const float StartPosY = -5; - const float StartPosZ = -3; + Vector3 startPosition = new Vector3(0, 2, 0); protected override void OnInitialize() { @@ -36,48 +32,45 @@ protected override void OnInitializePhysics() World = new DiscreteDynamicsWorld(Dispatcher, Broadphase, null, CollisionConf); World.Gravity = new Vector3(0, -10, 0); - // create the ground - BoxShape groundShape = new BoxShape(50, 1, 50); + CreateGround(); + CreateBoxes(); + } + + private void CreateGround() + { + var groundShape = new BoxShape(50, 1, 50); //groundShape.InitializePolyhedralFeatures(); - //CollisionShape groundShape = new StaticPlaneShape(new Vector3(0,1,0), 50); + //var groundShape = new StaticPlaneShape(Vector3.UnitY, 1); CollisionShapes.Add(groundShape); CollisionObject ground = LocalCreateRigidBody(0, Matrix.Identity, groundShape); ground.UserObject = "Ground"; + } - // create a few dynamic rigidbodies + private void CreateBoxes() + { const float mass = 1.0f; - - BoxShape colShape = new BoxShape(1); + var 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); + var rbInfo = new RigidBodyConstructionInfo(mass, null, colShape, localInertia); - for (int k = 0; k < ArraySizeY; k++) + for (int y = 0; y < ArraySizeY; y++) { - for (int i = 0; i < ArraySizeX; i++) + for (int x = 0; x < ArraySizeX; x++) { - for (int j = 0; j < ArraySizeZ; j++) + for (int z = 0; z < ArraySizeZ; z++) { - 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); + Vector3 position = startPosition + 2 * new Vector3(x, y, z); // make it drop from a height - body.Translate(new Vector3(0, 20, 0)); + position += new Vector3(0, 10, 0); + + // using MotionState is recommended, it provides interpolation capabilities + // and only synchronizes 'active' objects + rbInfo.MotionState = new DefaultMotionState(Matrix.Translation(position)); + var body = new RigidBody(rbInfo); World.AddRigidBody(body); } diff --git a/BulletSharpPInvoke/demos/BenchmarkDemo/BenchmarkDemo.cs b/BulletSharpPInvoke/demos/BenchmarkDemo/BenchmarkDemo.cs index 271b0e43..41608dcd 100644 --- a/BulletSharpPInvoke/demos/BenchmarkDemo/BenchmarkDemo.cs +++ b/BulletSharpPInvoke/demos/BenchmarkDemo/BenchmarkDemo.cs @@ -10,11 +10,18 @@ class BenchmarkDemo : Demo Vector3 eye = new Vector3(60, 40, 20); Vector3 target = new Vector3(0, 5, -4); - int benchmark = 2; - - const float collisionRadius = 0.0f; const float defaultContactProcessingThreshold = 0.0f; + int scene = 1; + Action[] _scenes; + + public BenchmarkDemo() + { + _scenes = new Action[] { + Create3KBoxes, CreateStructures, CreateTaruStack, CreateShapesGravity, CreateTaruGravity + }; + } + protected override void OnInitialize() { Freelook.SetEyeTarget(eye, target); @@ -30,7 +37,7 @@ protected override void OnInitializePhysics() { // collision configuration contains default setup for memory, collision setup using (var cci = new DefaultCollisionConstructionInfo() - { DefaultMaxPersistentManifoldPoolSize = 32768 }) + { DefaultMaxPersistentManifoldPoolSize = 32768 }) { CollisionConf = new DefaultCollisionConfiguration(cci); } @@ -43,7 +50,7 @@ protected override void OnInitializePhysics() Vector3 worldAabbMin = new Vector3(-1000, -1000, -1000); Vector3 worldAabbMax = new Vector3(1000, 1000, 1000); - HashedOverlappingPairCache pairCache = new HashedOverlappingPairCache(); + var pairCache = new HashedOverlappingPairCache(); Broadphase = new AxisSweep3(worldAabbMin, worldAabbMax, 3500, pairCache); //Broadphase = new DbvtBroadphase(); @@ -54,228 +61,169 @@ protected override void OnInitializePhysics() World.SolverInfo.SolverMode |= SolverModes.EnableFrictionDirectionCaching; World.SolverInfo.NumIterations = 5; - if (benchmark < 5) - { - // create the ground - var groundShape = new BoxShape(250, 50, 250); - CollisionShapes.Add(groundShape); - var ground = base.LocalCreateRigidBody(0, Matrix.Translation(0, -50, 0), groundShape); - ground.UserObject = "Ground"; - } + _scenes[scene](); + } - 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; + private void CreateGround() + { + var groundShape = new BoxShape(250, 50, 250); + CollisionShapes.Add(groundShape); + var ground = base.LocalCreateRigidBody(0, Matrix.Translation(0, -50, 0), groundShape); + ground.UserObject = "Ground"; + } - switch (benchmark) - { - case 1: - // 3000 + private void Create3KBoxes() + { + CreateGround(); + + const float mass = 2.0f; + const int arrayWidth = 8; + const int arrayHeight = 47; + const float cubeHalfExtent = 1.0f; + const float cubeWidth = cubeHalfExtent * 2; + const float spacing = cubeHalfExtent; + const float offset = cubeWidth + spacing; + var startPosition = new Vector3(-20, 0, -10); + var blockShape = new BoxShape(cubeHalfExtent); + + CreateStack(startPosition, arrayWidth, arrayHeight, arrayWidth, offset, 1, mass, blockShape); + } - BoxShape blockShape = new BoxShape(cubeSize - collisionRadius); - mass = 2.0f; + private void CreateStructures() + { + CreateGround(); + Vector3 boxSize = new Vector3(1); + CreatePyramid(new Vector3(-20, 0, 0), 12, boxSize); + CreateWall(new Vector3(-2, 0, 0), 12, boxSize); + CreateWall(new Vector3(4, 0, 0), 12, boxSize); + CreateWall(new Vector3(10, 0, 0), 12, boxSize); + CreateTowerCircle(new Vector3(25, 0, 0), 8, 24, boxSize); + } - for (int k = 0; k < 47; k++) - { - for (int j = 0; j < size; j++) - { - pos[2] = offset + j * (cubeSize * 2.0f + spacing); - for (int i = 0; i < size; i++) - { - pos[0] = offset + 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; + private void CreateTaruStack() + { + CreateGround(); - 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; + const float mass = 2.0f; + const int arrayWidth = 8; + const int arrayHeight = 15; + const float offset = 5; + const float widthSpacingFactor = 1.02f; + var startPosition = new Vector3(-20, 0, -10); + var convexHullShape = new ConvexHullShape(Taru.Vertices); - case 3: - // TODO: Ragdolls - break; + //this will enable polyhedral contact clipping, better quality, slightly slower + convexHullShape.InitializePolyhedralFeatures(); - case 4: - cubeSize = 1.5f; + CreateStack(startPosition, arrayWidth, arrayHeight, arrayWidth, offset, + widthSpacingFactor, mass, convexHullShape); + } - ConvexHullShape convexHullShape = new ConvexHullShape(); + private void CreateShapesGravity() + { + const float cubeHalfExtent = 1.5f; + const float cubeWidth = cubeHalfExtent * 2; + Vector3 boxSize = new Vector3(cubeHalfExtent); + float boxMass = 1.0f; + float sphereRadius = 1.5f; + float sphereMass = 1.0f; + float capsuleHalf = 2.0f; + float capsuleRadius = 1.0f; + float capsuleMass = 1.0f; - float scaling = 1; + int stackSize = 10; + int stackHeight = 10; - convexHullShape.LocalScaling = new Vector3(scaling); + float spacing = 2.0f; + var position = new Vector3(0.0f, 20.0f, 0.0f); + float offset = -stackSize * (cubeWidth + spacing) * 0.5f; - 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)); - } + int numBodies = 0; - //this will enable polyhedral contact clipping, better quality, slightly slower - convexHullShape.InitializePolyhedralFeatures(); + var random = new Random(); - for (int k = 0; k < 15; k++) + for (int k = 0; k < stackHeight; k++) + { + for (int j = 0; j < stackSize; j++) + { + position.Z = offset + j * (cubeWidth + spacing); + for (int i = 0; i < stackSize; i++) { - for (int j = 0; j < size; j++) - { - pos[2] = offset + j * (cubeSize * 2.0f + spacing); - for (int i = 0; i < size; i++) - { - pos[0] = offset + i * (cubeSize * 2.0f + spacing); - LocalCreateRigidBody(mass, Matrix.Translation(pos), convexHullShape); - } - } - offset -= 0.05f * spacing * (size - 1); - spacing *= 1.01f; - pos.Y += 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; + position.X = offset + i * (cubeWidth + spacing); + Vector3 bpos = new Vector3(0, 25, 0) + new Vector3(5.0f * position.X, position.Y, 5.0f * position.Z); + int idx = random.Next(10); + Matrix trans = Matrix.Translation(bpos); - Random random = new Random(); - - for (int k = 0; k < height; k++) - { - for (int j = 0; j < size; j++) + switch (idx) { - pos[2] = offset + j * (cubeSize * 2.0f + spacing); - for (int i = 0; i < size; i++) - { - pos[0] = offset + 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: { - 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; + float r = 0.5f * (idx + 1); + var boxShape = new BoxShape(boxSize * r); + LocalCreateRigidBody(boxMass * r, trans, boxShape); } + 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); + case 3: + case 4: + case 5: + { + float r = 0.5f * (idx - 3 + 1); + var sphereShape = new SphereShape(sphereRadius * r); + LocalCreateRigidBody(sphereMass * r, trans, sphereShape); + } + break; - convexHullShape = new ConvexHullShape(); + case 6: + case 7: + case 8: + { + float r = 0.5f * (idx - 6 + 1); + var capsuleShape = new CapsuleShape(capsuleRadius * r, capsuleHalf * r); + LocalCreateRigidBody(capsuleMass * r, trans, capsuleShape); + } + break; + } - 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); + numBodies++; } + } + offset -= 0.05f * spacing * (stackSize - 1); + spacing *= 1.1f; + position.Y += cubeWidth + spacing; + } - 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; - + //CreateLargeMeshBody(); + } - for (int k = 0; k < height; k++) - { - for (int j = 0; j < size; j++) - { - pos[2] = offset + j * (cubeSize * 2.0f + spacing); - for (int i = 0; i < size; i++) - { - pos[0] = offset + 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); - } + private void CreateTaruGravity() + { + const float mass = 1.0f; + const float cubeHalfExtent = 1.5f; + const float cubeWidth = cubeHalfExtent * 2; + const int stackWidth = 10; + const int stackHeight = 10; + const float spacing = 5.0f; + const float offset = cubeWidth + spacing; + const float widthSpacingFactor = 1.12f; + var startPosition = new Vector3(-250, 0, -150); - //CreateLargeMeshBody(); + var convexHullShape = new ConvexHullShape(Taru.Vertices); - break; + CreateStack(startPosition, stackWidth, stackHeight, stackWidth, offset, + widthSpacingFactor, mass, convexHullShape); - case 7: - // TODO - //CreateTest6(); - //InitRays(); - break; - } + //CreateLargeMeshBody(); } - void CreatePyramid(Vector3 offsetPosition, int stackSize, Vector3 boxSize) + private void CreatePyramid(Vector3 offsetPosition, int stackSize, Vector3 boxSize) { const float mass = 1.0f; const float space = 0.0001f; - var blockShape = new BoxShape(boxSize - new Vector3(collisionRadius)); + var blockShape = new BoxShape(boxSize); Vector3 diff = boxSize * 1.02f; Vector3 offset = -stackSize * 0.5f * (diff * 2 + new Vector3(space)); @@ -298,12 +246,12 @@ void CreatePyramid(Vector3 offsetPosition, int stackSize, Vector3 boxSize) } } - void CreateWall(Vector3 offsetPosition, int stackSize, Vector3 boxSize) + private void CreateWall(Vector3 offsetPosition, int stackSize, Vector3 boxSize) { - var blockShape = new BoxShape(boxSize - new Vector3(collisionRadius)); + var blockShape = new BoxShape(boxSize); const float mass = 1.0f; - var position = Matrix.Identity; + var transform = Matrix.Identity; for (int y = 0; y < stackSize; y++) { @@ -311,16 +259,16 @@ void CreateWall(Vector3 offsetPosition, int stackSize, Vector3 boxSize) float height = ((stackSize - y) * 2 - 1) * boxSize.Y; for (int i = 0; i < y; i++) { - position.Origin = offsetPosition + + transform.Origin = offsetPosition + new Vector3(0, height, (offset + i * 2) * boxSize.Z); - LocalCreateRigidBody(mass, position, blockShape); + LocalCreateRigidBody(mass, transform, blockShape); } } } - void CreateTowerCircle(Vector3 offsetPosition, int stackSize, int rotSize, Vector3 boxSize) + private void CreateTowerCircle(Vector3 offsetPosition, int stackSize, int rotSize, Vector3 boxSize) { - var blockShape = new BoxShape(boxSize - new Vector3(collisionRadius)); + var blockShape = new BoxShape(boxSize); const float mass = 1.0f; float radius = 1.3f * rotSize * boxSize.X / (float)Math.PI; @@ -346,24 +294,40 @@ void CreateTowerCircle(Vector3 offsetPosition, int stackSize, int rotSize, Vecto } } + private void CreateStack(Vector3 startPosition, int widthX, int height, int widthZ, float offset, + float widthSpacingFactor, float mass, CollisionShape shape) + { + float widthSpacing = 1; + for (int y = 0; y < height; y++) + { + for (int z = 0; z < widthZ; z++) + { + for (int x = 0; x < widthX; x++) + { + Vector3 position = startPosition + offset * new Vector3(x * widthSpacing, y, z * widthSpacing); + LocalCreateRigidBody(mass, Matrix.Translation(position), shape); + } + } + widthSpacing *= widthSpacingFactor; + } + } + public override RigidBody LocalCreateRigidBody(float mass, Matrix startTransform, CollisionShape shape) { //rigidbody is dynamic if and only if mass is non zero, otherwise static bool isDynamic = (mass != 0.0f); Vector3 localInertia = isDynamic ? shape.CalculateLocalInertia(mass) : Vector3.Zero; - RigidBody body; using (var rbInfo = new RigidBodyConstructionInfo(mass, null, shape, localInertia)) { - body = new RigidBody(rbInfo) + var body = new RigidBody(rbInfo) { ContactProcessingThreshold = defaultContactProcessingThreshold, WorldTransform = startTransform }; + World.AddRigidBody(body); + return body; } - - World.AddRigidBody(body); - return body; } } diff --git a/BulletSharpPInvoke/demos/BenchmarkDemo/Taru.cs b/BulletSharpPInvoke/demos/BenchmarkDemo/Taru.cs index 304838d7..8a970fac 100644 --- a/BulletSharpPInvoke/demos/BenchmarkDemo/Taru.cs +++ b/BulletSharpPInvoke/demos/BenchmarkDemo/Taru.cs @@ -2,51 +2,51 @@ namespace BenchmarkDemo { public class Taru { - public static float[] Vtx = + public static float[] Vertices = { -1.08664f,-1.99237f,0.0f, -0.768369f,-1.99237f,-0.768369f, -1.28852f,1.34412e-007f,-1.28852f, -1.82224f,1.90735e-007f,0.0f, -0.0f,-1.99237f,-1.08664f, -0.0f,0.0f,-1.82224f, -0.0f,-1.99237f,-1.08664f, --0.768369f,-1.99237f,-0.768369f, --1.28852f,1.34412e-007f,-1.28852f, -0.0f,0.0f,-1.82224f, --1.08664f,-1.99237f,1.82086e-007f, --1.82224f,1.90735e-007f,1.59305e-007f, --0.768369f,-1.99237f,0.76837f, --1.28852f,2.47058e-007f,1.28852f, -1.42495e-007f,-1.99237f,1.08664f, -2.38958e-007f,2.70388e-007f,1.82224f, -0.768369f,-1.99237f,0.768369f, -1.28852f,2.47058e-007f,1.28852f, -0.768369f,1.99237f,-0.768369f, -1.08664f,1.99237f,0.0f, -0.0f,1.99237f,-1.08664f, --0.768369f,1.99237f,-0.768369f, -0.0f,1.99237f,-1.08664f, --1.08664f,1.99237f,0.0f, --0.768369f,1.99237f,0.768369f, -1.42495e-007f,1.99237f,1.08664f, -0.768369f,1.99237f,0.768369f, -1.42495e-007f,-1.99237f,1.08664f, --0.768369f,-1.99237f,0.76837f, --1.08664f,-1.99237f,1.82086e-007f, --0.768369f,-1.99237f,-0.768369f, -0.0f,-1.99237f,-1.08664f, -0.768369f,-1.99237f,-0.768369f, -1.08664f,-1.99237f,0.0f, -0.768369f,-1.99237f,0.768369f, -0.768369f,1.99237f,-0.768369f, -0.0f,1.99237f,-1.08664f, --0.768369f,1.99237f,-0.768369f, --1.08664f,1.99237f,0.0f, --0.768369f,1.99237f,0.768369f, -1.42495e-007f,1.99237f,1.08664f, -0.768369f,1.99237f,0.768369f, -1.08664f,1.99237f,0.0f + 1.08664f,-1.99237f,0.0f, + 0.768369f,-1.99237f,-0.768369f, + 1.28852f,1.34412e-007f,-1.28852f, + 1.82224f,1.90735e-007f,0.0f, + 0.0f,-1.99237f,-1.08664f, + 0.0f,0.0f,-1.82224f, + 0.0f,-1.99237f,-1.08664f, + -0.768369f,-1.99237f,-0.768369f, + -1.28852f,1.34412e-007f,-1.28852f, + 0.0f,0.0f,-1.82224f, + -1.08664f,-1.99237f,1.82086e-007f, + -1.82224f,1.90735e-007f,1.59305e-007f, + -0.768369f,-1.99237f,0.76837f, + -1.28852f,2.47058e-007f,1.28852f, + 1.42495e-007f,-1.99237f,1.08664f, + 2.38958e-007f,2.70388e-007f,1.82224f, + 0.768369f,-1.99237f,0.768369f, + 1.28852f,2.47058e-007f,1.28852f, + 0.768369f,1.99237f,-0.768369f, + 1.08664f,1.99237f,0.0f, + 0.0f,1.99237f,-1.08664f, + -0.768369f,1.99237f,-0.768369f, + 0.0f,1.99237f,-1.08664f, + -1.08664f,1.99237f,0.0f, + -0.768369f,1.99237f,0.768369f, + 1.42495e-007f,1.99237f,1.08664f, + 0.768369f,1.99237f,0.768369f, + 1.42495e-007f,-1.99237f,1.08664f, + -0.768369f,-1.99237f,0.76837f, + -1.08664f,-1.99237f,1.82086e-007f, + -0.768369f,-1.99237f,-0.768369f, + 0.0f,-1.99237f,-1.08664f, + 0.768369f,-1.99237f,-0.768369f, + 1.08664f,-1.99237f,0.0f, + 0.768369f,-1.99237f,0.768369f, + 0.768369f,1.99237f,-0.768369f, + 0.0f,1.99237f,-1.08664f, + -0.768369f,1.99237f,-0.768369f, + -1.08664f,1.99237f,0.0f, + -0.768369f,1.99237f,0.768369f, + 1.42495e-007f,1.99237f,1.08664f, + 0.768369f,1.99237f,0.768369f, + 1.08664f,1.99237f,0.0f }; } } diff --git a/BulletSharpPInvoke/demos/Box2DDemo/Box2DDemo.cs b/BulletSharpPInvoke/demos/Box2DDemo/Box2DDemo.cs index eaf6b45b..d07f7c05 100644 --- a/BulletSharpPInvoke/demos/Box2DDemo/Box2DDemo.cs +++ b/BulletSharpPInvoke/demos/Box2DDemo/Box2DDemo.cs @@ -29,10 +29,10 @@ protected override void OnInitializePhysics() // 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(); + var simplex = new VoronoiSimplexSolver(); + var pdSolver = new MinkowskiPenetrationDepthSolver(); - Convex2DConvex2DAlgorithm.CreateFunc convexAlgo2D = new Convex2DConvex2DAlgorithm.CreateFunc(simplex, pdSolver); + var convexAlgo2D = new Convex2DConvex2DAlgorithm.CreateFunc(simplex, pdSolver); Dispatcher.RegisterCollisionCreateFunc(BroadphaseNativeType.Convex2DShape, BroadphaseNativeType.Convex2DShape, convexAlgo2D); Dispatcher.RegisterCollisionCreateFunc(BroadphaseNativeType.Box2DShape, BroadphaseNativeType.Convex2DShape, convexAlgo2D); @@ -47,13 +47,20 @@ protected override void OnInitializePhysics() 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); + CreateGround(); + Create2dBodies(); + } + + private void CreateGround() + { + var groundShape = new BoxShape(150, 7, 150); CollisionShapes.Add(groundShape); var ground = LocalCreateRigidBody(0, Matrix.Identity, groundShape); ground.UserObject = "Ground"; + } - // create a few dynamic rigidbodies + private void Create2dBodies() + { // 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) }; @@ -106,10 +113,12 @@ protected override void OnInitializePhysics() rbInfo.CollisionShape = colShape2; break; } - RigidBody body = new RigidBody(rbInfo); - //body.ActivationState = ActivationState.IslandSleeping; - body.LinearFactor = new Vector3(1, 1, 0); - body.AngularFactor = new Vector3(0, 0, 1); + var body = new RigidBody(rbInfo) + { + //ActivationState = ActivationState.IslandSleeping, + LinearFactor = new Vector3(1, 1, 0), + AngularFactor = new Vector3(0, 0, 1) + }; World.AddRigidBody(body); diff --git a/BulletSharpPInvoke/demos/CcdPhysicsDemo/CcdPhysicsDemo.cs b/BulletSharpPInvoke/demos/CcdPhysicsDemo/CcdPhysicsDemo.cs index bc81c3fc..e50063f7 100644 --- a/BulletSharpPInvoke/demos/CcdPhysicsDemo/CcdPhysicsDemo.cs +++ b/BulletSharpPInvoke/demos/CcdPhysicsDemo/CcdPhysicsDemo.cs @@ -52,8 +52,6 @@ public override void OnHandleInput() protected override void OnInitializePhysics() { - int i; - shootBoxInitialSpeed = 4000; // collision configuration contains default setup for memory, collision setup @@ -65,8 +63,7 @@ protected override void OnInitializePhysics() Broadphase = new DbvtBroadphase(); - - // the default constraint solver. + // the default constraint solver Solver = new SequentialImpulseConstraintSolver(); World = new DiscreteDynamicsWorld(Dispatcher, Broadphase, Solver, CollisionConf); @@ -78,6 +75,12 @@ protected override void OnInitializePhysics() World.Gravity = new Vector3(0, -10, 0); + CreateGround(); + CreateBoxStack(); + } + + private void CreateGround() + { BoxShape ground = new BoxShape(200, 1, 200); ground.InitializePolyhedralFeatures(); CollisionShapes.Add(ground); @@ -85,13 +88,16 @@ protected override void OnInitializePhysics() 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); + private void CreateBoxStack() + { + //var shape = new CylinderShape(CubeHalfExtents, CubeHalfExtents, CubeHalfExtents); + var shape = new BoxShape(CubeHalfExtents); CollisionShapes.Add(shape); const int numObjects = 120; - for (i = 0; i < numObjects; i++) + for (int i = 0; i < numObjects; i++) { //stack them const int colsize = 10; @@ -108,7 +114,7 @@ protected override void OnInitializePhysics() Matrix trans = Matrix.Translation(col * 2 * CubeHalfExtents + (row2 % 2) * CubeHalfExtents, row * 2 * CubeHalfExtents + CubeHalfExtents + ExtraHeight, 0); - body = LocalCreateRigidBody(1, trans, shape); + RigidBody body = LocalCreateRigidBody(1, trans, shape); body.SetAnisotropicFriction(shape.AnisotropicRollingFrictionDirection, AnisotropicFrictionFlags.RollingFriction); body.Friction = 0.5f; //body.RollingFriction = 0.3f; @@ -123,8 +129,6 @@ protected override void OnInitializePhysics() public override void ShootBox(Vector3 camPos, Vector3 destination) { - if (World == null) return; - const float mass = 1.0f; if (shootBoxShape == null) diff --git a/BulletSharpPInvoke/demos/ConcaveRaycastDemo/ConcaveRaycastDemo.cs b/BulletSharpPInvoke/demos/ConcaveRaycastDemo/ConcaveRaycastDemo.cs index 7ae60170..43a00752 100644 --- a/BulletSharpPInvoke/demos/ConcaveRaycastDemo/ConcaveRaycastDemo.cs +++ b/BulletSharpPInvoke/demos/ConcaveRaycastDemo/ConcaveRaycastDemo.cs @@ -71,11 +71,19 @@ protected override void OnInitializePhysics() World.SolverInfo.SplitImpulse = 1; World.Gravity = new Vector3(0, -10, 0); + raycastBar = new RaycastBar(4000.0f, 0.0f, -1000.0f, 10); + //raycastBar = new RaycastBar(true, 40.0f, -50.0f, 50.0f); + + CreateBoxes(); + CreateGround(); + } + private void CreateGround() + { const int totalVerts = NumVertsX * NumVertsY; const int totalTriangles = 2 * (NumVertsX - 1) * (NumVertsY - 1); const int triangleIndexStride = 3 * sizeof(int); - const int vertexStride = Vector3.SizeInBytes; + int vertexStride = Vector3.SizeInBytes; var mesh = new IndexedMesh(); mesh.Allocate(totalTriangles, totalVerts, triangleIndexStride, vertexStride); @@ -103,11 +111,6 @@ protected override void OnInitializePhysics() indexVertexArrays = new TriangleIndexVertexArray(); indexVertexArrays.AddIndexedMesh(mesh); - raycastBar = new RaycastBar(4000.0f, 0.0f, -1000.0f, 10); - //raycastBar = new RaycastBar(true, 40.0f, -50.0f, 50.0f); - - CreateBoxes(); - SetVertexPositions(waveHeight, 0.0f); const bool useQuantizedAabbCompression = true; diff --git a/BulletSharpPInvoke/demos/SoftDemo/SoftDemo.cs b/BulletSharpPInvoke/demos/SoftDemo/SoftDemo.cs index 0a88ae54..eb47e16e 100644 --- a/BulletSharpPInvoke/demos/SoftDemo/SoftDemo.cs +++ b/BulletSharpPInvoke/demos/SoftDemo/SoftDemo.cs @@ -102,13 +102,11 @@ SoftRigidDynamicsWorld SoftWorld get { return World as SoftRigidDynamicsWorld; } } - delegate void DemoConstructor(); - - DemoConstructor[] demos; + Action[] demos; public SoftDemo() { - demos = new DemoConstructor[] { Init_Cloth, Init_Pressure, Init_Volume, Init_Ropes, Init_RopeAttach, + demos = new Action[] { Init_Cloth, Init_Pressure, Init_Volume, Init_Ropes, Init_RopeAttach, Init_ClothAttach, Init_Sticks, Init_CapsuleCollision, 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,