AndresTraks / BulletSharpPInvoke

.NET wrapper for the Bullet physics library using Platform Invoke

Home Page:http://andrestraks.github.io/BulletSharp/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Double Precision BulletSharp In Unity.

CornLizard opened this issue · comments

Hello, I'm trying to get Bullet Physics in double precision working in Unity and I've had success with getting most of the basic shapes working. Some of them are causing me grief though. Maybe I'm not turning on some features.

1 • If I create a box high above a static ground box. The box will tunnel through and keep falling. I also tested this in BulletSharpUnity and it didn't tunnel through. Not sure what I'm doing differently.

2 • If I replace the BoxShape with a StaticPlaneShape any rigid body that falls from a decent height will burrow into the plane and pop back out sometimes spinning or ending up at an odd rotation.

Video of StaticPlaneShape
https://streamable.com/b1fh3

2 • The HeightfieldTerrainShape seems pretty broken in my solution. Works flawlessly in the BulletSharpUnity project. Its as if every value is set to 0 no matter what heights I set. A cube will hit it and for a moment it will bounce, wiggle, rotate. Then some of the boxes fall right through while some stay on top.

Video of HeightfieldTerrainShape
https://streamable.com/8s5z9

In the photo, there is an infinite plane under the terrain. As you can see there are some cubes on the terrain but only at a height of 0.
Physics

Bullet World Creation

        collisionConfiguration = new DefaultCollisionConfiguration();
        dispatcher = new CollisionDispatcherMultiThreaded(collisionConfiguration);
        broadphase = new DbvtBroadphase();
        constraintSolver = new ConstraintSolverPoolMultiThreaded(Environment.ProcessorCount);
        bWorld = new DiscreteDynamicsWorldMultiThreaded(
            dispatcher,
            broadphase,
            constraintSolver,
            null,
            collisionConfiguration);
        bWorld.SolverInfo.SolverMode |= SolverModes.Simd | SolverModes.UseWarmStarting | SolverModes.Use2FrictionDirections | SolverModes.RandomizeOrder;
        bWorld.SolverInfo.NumIterations = 20;
        bWorld.DispatchInfo.UseContinuous = true;

        Threads.TaskScheduler = Threads.GetPplTaskScheduler();
        Threads.TaskScheduler.NumThreads = Threads.TaskScheduler.MaxNumThreads;
        bWorld.Gravity = new BulletSharp.Math.Vector3(
            Physics.gravity.x,
            Physics.gravity.z,
            Physics.gravity.y);

HeightfieldTerrainShape creation

               TerrainData terrainData = terrain.terrainData;
                int width = terrainData.heightmapResolution;
                int length = terrainData.heightmapResolution;
                float maxHeight = terrainData.size.y;

                //generate procedural data
                byte[] terr = new byte[width * length * sizeof(float)];
                System.IO.MemoryStream file = new System.IO.MemoryStream(terr);
                System.IO.BinaryWriter writer = new System.IO.BinaryWriter(file);

                for (int i2 = 0; i2 < length; i2++)
                {
                    float[,] row = terrainData.GetHeights(0, i2, width, 1);
                    for (int j = 0; j < width; j++)
                    {
                        writer.Write((float)row[0, j] * maxHeight);
                    }
                }

                writer.Flush();
                file.Position = 0;

                GCHandle handle = GCHandle.Alloc(terr, GCHandleType.Pinned);

                HeightfieldTerrainShape heightfield = new HeightfieldTerrainShape(
                    width,
                    length,
                    handle.AddrOfPinnedObject(),
                    1,
                    0,
                    maxHeight,
                    2,
                    PhyScalarType.Single,
                    false);
                heightfield.SetUseDiamondSubdivision(true);
                heightfield.LocalScaling = new BulletSharp.Math.Vector3(
                    terrainData.heightmapScale.x,
                    terrainData.heightmapScale.z,
                    1);
                var shape = new CompoundShape();
                shape.AddChildShape(Matrix.Translation(
                    terrainData.size.x / 2,
                    terrainData.size.z / 2,
                    terrainData.size.y / 2), heightfield);
                body.collisionShape = shape;

Hi, did you find the problem?
The code looks fine.