Exemple #1
0
        // TODO: redo terrain implementation selection to allow other base types than heightMap.
        private BSTerrainPhys BuildPhysicalTerrain(Vector3 terrainRegionBase, uint id, float[] heightMap, Vector3 minCoords, Vector3 maxCoords)
        {
            PhysicsScene.Logger.DebugFormat("{0} Terrain for {1}/{2} created with {3}",
                                            LogHeader, PhysicsScene.RegionName, terrainRegionBase,
                                            (BSTerrainPhys.TerrainImplementation)BSParam.TerrainImplementation);
            BSTerrainPhys newTerrainPhys = null;

            switch ((int)BSParam.TerrainImplementation)
            {
            case (int)BSTerrainPhys.TerrainImplementation.Heightmap:
                newTerrainPhys = new BSTerrainHeightmap(PhysicsScene, terrainRegionBase, id,
                                                        heightMap, minCoords, maxCoords);
                break;

            case (int)BSTerrainPhys.TerrainImplementation.Mesh:
                newTerrainPhys = new BSTerrainMesh(PhysicsScene, terrainRegionBase, id,
                                                   heightMap, minCoords, maxCoords);
                break;

            default:
                PhysicsScene.Logger.ErrorFormat("{0} Bad terrain implementation specified. Type={1}/{2},Region={3}/{4}",
                                                LogHeader,
                                                (int)BSParam.TerrainImplementation,
                                                BSParam.TerrainImplementation,
                                                PhysicsScene.RegionName, terrainRegionBase);
                break;
            }
            return(newTerrainPhys);
        }
 // TODO: redo terrain implementation selection to allow other base types than heightMap.
 private BSTerrainPhys BuildPhysicalTerrain(Vector3 terrainRegionBase, uint id, float[] heightMap, Vector3 minCoords, Vector3 maxCoords)
 {
     PhysicsScene.Logger.DebugFormat("{0} Terrain for {1}/{2} created with {3}", 
                                         LogHeader, PhysicsScene.RegionName, terrainRegionBase, 
                                         (BSTerrainPhys.TerrainImplementation)BSParam.TerrainImplementation);
     BSTerrainPhys newTerrainPhys = null;
     switch ((int)BSParam.TerrainImplementation)
     {
         case (int)BSTerrainPhys.TerrainImplementation.Heightmap:
             newTerrainPhys = new BSTerrainHeightmap(PhysicsScene, terrainRegionBase, id,
                                         heightMap, minCoords, maxCoords);
             break;
         case (int)BSTerrainPhys.TerrainImplementation.Mesh:
             newTerrainPhys = new BSTerrainMesh(PhysicsScene, terrainRegionBase, id,
                                         heightMap, minCoords, maxCoords);
             break;
         default:
             PhysicsScene.Logger.ErrorFormat("{0} Bad terrain implementation specified. Type={1}/{2},Region={3}/{4}",
                                         LogHeader, 
                                         (int)BSParam.TerrainImplementation, 
                                         BSParam.TerrainImplementation,
                                         PhysicsScene.RegionName, terrainRegionBase);
             break;
     }
     return newTerrainPhys;
 }
Exemple #3
0
        // Create terrain mesh from a heightmap.
        public BSTerrainMesh(BSScene physicsScene, Vector3 regionBase, uint id, float[] initialMap,
                             Vector3 minCoords, Vector3 maxCoords)
            : base(physicsScene, regionBase, id)
        {
            int indicesCount;

            int[] indices;
            int   verticesCount;

            float[] vertices;

            m_savedHeightMap = initialMap;

            m_sizeX = (int)(maxCoords.X - minCoords.X);
            m_sizeY = (int)(maxCoords.Y - minCoords.Y);

            if (!BSTerrainMesh.ConvertHeightmapToMesh(PhysicsScene, initialMap,
                                                      m_sizeX, m_sizeY,
                                                      (float)m_sizeX, (float)m_sizeY,
                                                      Vector3.Zero, 1.0f,
                                                      out indicesCount, out indices, out verticesCount, out vertices))
            {
                // DISASTER!!
                PhysicsScene.DetailLog("{0},BSTerrainMesh.create,failedConversionOfHeightmap", ID);
                PhysicsScene.Logger.ErrorFormat("{0} Failed conversion of heightmap to mesh! base={1}", LogHeader, TerrainBase);
                // Something is very messed up and a crash is in our future.
                return;
            }
            PhysicsScene.DetailLog("{0},BSTerrainMesh.create,meshed,indices={1},indSz={2},vertices={3},vertSz={4}",
                                   ID, indicesCount, indices.Length, verticesCount, vertices.Length);

            m_terrainShape = new BulletShape(BulletSimAPI.CreateMeshShape2(PhysicsScene.World.ptr,
                                                                           indicesCount, indices, verticesCount, vertices),
                                             BSPhysicsShapeType.SHAPE_MESH);
            if (!m_terrainShape.HasPhysicalShape)
            {
                // DISASTER!!
                PhysicsScene.DetailLog("{0},BSTerrainMesh.create,failedCreationOfShape", ID);
                physicsScene.Logger.ErrorFormat("{0} Failed creation of terrain mesh! base={1}", LogHeader, TerrainBase);
                // Something is very messed up and a crash is in our future.
                return;
            }

            Vector3    pos = regionBase;
            Quaternion rot = Quaternion.Identity;

            m_terrainBody = new BulletBody(id, BulletSimAPI.CreateBodyWithDefaultMotionState2(m_terrainShape.ptr, ID, pos, rot));
            if (!m_terrainBody.HasPhysicalBody)
            {
                // DISASTER!!
                physicsScene.Logger.ErrorFormat("{0} Failed creation of terrain body! base={1}", LogHeader, TerrainBase);
                // Something is very messed up and a crash is in our future.
                return;
            }

            // Set current terrain attributes
            BulletSimAPI.SetFriction2(m_terrainBody.ptr, BSParam.TerrainFriction);
            BulletSimAPI.SetHitFraction2(m_terrainBody.ptr, BSParam.TerrainHitFraction);
            BulletSimAPI.SetRestitution2(m_terrainBody.ptr, BSParam.TerrainRestitution);
            BulletSimAPI.SetCollisionFlags2(m_terrainBody.ptr, CollisionFlags.CF_STATIC_OBJECT);

            // Static objects are not very massive.
            BulletSimAPI.SetMassProps2(m_terrainBody.ptr, 0f, Vector3.Zero);

            // Put the new terrain to the world of physical objects
            BulletSimAPI.AddObjectToWorld2(PhysicsScene.World.ptr, m_terrainBody.ptr, pos, rot);

            // Redo its bounding box now that it is in the world
            BulletSimAPI.UpdateSingleAabb2(PhysicsScene.World.ptr, m_terrainBody.ptr);

            m_terrainBody.collisionType = CollisionType.Terrain;
            m_terrainBody.ApplyCollisionMask();

            // Make it so the terrain will not move or be considered for movement.
            BulletSimAPI.ForceActivationState2(m_terrainBody.ptr, ActivationState.DISABLE_SIMULATION);
        }