Exemplo n.º 1
0
 public override CollisionShape GetCollisionShape()
 {
     if (collisionShapePtr == null)
     {
         collisionShapePtr = new ConeShape(radius, height);
     }
     return collisionShapePtr;
 }
Exemplo n.º 2
0
 public override CollisionShape GetCollisionShape()
 {
     if (collisionShapePtr == null)
     {
         collisionShapePtr = new ConeShape(radius, height);
         ((ConeShape)collisionShapePtr).LocalScaling = m_localScaling.ToBullet();
     }
     return collisionShapePtr;
 }
Exemplo n.º 3
0
 Mesh CreateConeShape(ConeShape shape)
 {
     Mesh mesh = Mesh.CreateCylinder(device, 0, shape.Radius, shape.Height, 16, 1);
     shapes.Add(shape, mesh);
     return mesh;
 }
Exemplo n.º 4
0
 public static void CreateConeShape(ConeShape shape, Mesh mesh)
 {
     ProceduralPrimitives.CreateMeshCone(mesh, shape.Height, shape.Radius, 0f, 10);
 }
Exemplo n.º 5
0
        public static UnityEngine.Vector3[] CreateCone(ConeShape shape, out int[] indices)
        {
            int up = shape.ConeUpIndex;
            float radius = shape.Radius;
            float halfHeight = shape.Height / 2 + shape.Margin;

            const int numSteps = 10;
            const float angleStep = (2 * (float)Math.PI) / numSteps;

            const int vertexCount = 2 + 6 * numSteps;
            const int indexCount = (4 * numSteps + 2) * 3;

            UnityEngine.Vector3[] vertices = new UnityEngine.Vector3[vertexCount * 2];
            indices = new int[indexCount];

            int i = 0, v = 0;
            int index = 0;
            int baseIndex;
            UnityEngine.Vector3 normal;

            // Draw the base
            normal = GetVectorByAxis(-UnityEngine.Vector3.up, up);

            baseIndex = index;
            vertices[v++] = GetVectorByAxis(0, -halfHeight, 0, up);
            vertices[v++] = normal;

            vertices[v++] = GetVectorByAxis(0, -halfHeight, radius, up);
            vertices[v++] = normal;
            index += 2;

            for (int j = 1; j < numSteps; j++)
            {
                float x = radius * (float)Math.Sin(j * angleStep);
                float z = radius * (float)Math.Cos(j * angleStep);

                vertices[v++] = GetVectorByAxis(x, -halfHeight, z, up);
                vertices[v++] = normal;

                indices[i++] = baseIndex;
                indices[i++] = index - 1;
                indices[i++] = index++;
            }
            indices[i++] = baseIndex;
            indices[i++] = index - 1;
            indices[i++] = baseIndex + 1;

            normal = GetVectorByAxis(0, 0, radius, up);
            normal.Normalize();

            baseIndex = index;
            vertices[v++] = GetVectorByAxis(0, halfHeight, 0, up);
            vertices[v++] = normal;

            vertices[v++] = GetVectorByAxis(0, -halfHeight, radius, up);
            vertices[v++] = normal;
            index += 2;

            for (int j = 1; j < numSteps + 1; j++)
            {
                float x = radius * (float)Math.Sin(j * angleStep);
                float z = radius * (float)Math.Cos(j * angleStep);

                normal = GetVectorByAxis(x, 0, z, up);
                normal.Normalize();

                vertices[v++] = GetVectorByAxis(0, halfHeight, 0, up);
                vertices[v++] = normal;

                vertices[v++] = GetVectorByAxis(x, -halfHeight, z, up);
                vertices[v++] = normal;

                indices[i++] = index - 2;
                indices[i++] = index - 1;
                indices[i++] = index;
                indices[i++] = index;
                indices[i++] = index - 1;
                indices[i++] = index + 1;
                index += 2;
            }
            indices[i++] = index - 2;
            indices[i++] = index - 1;
            indices[i++] = baseIndex;
            indices[i++] = baseIndex;
            indices[i++] = index - 1;
            indices[i] = baseIndex + 1;

            return vertices;
        }
 public CollisionShape CreateConeShapeY(float radius, float height)
 {
     ConeShape shape = new ConeShape(radius, height);
     _allocatedCollisionShapes.Add(shape);
     return shape;
 }
        /// <summary>
        /// Creates a collision shape for a shape component
        /// </summary>
        private CollisionShape CreateShapeForComponent(ShapeComponent component)
        {
            switch (component.Type) {
                case ThingEnum.Box:
                    return new BoxShape(component.Dimensions);
                case ThingEnum.Cylinder:
                    return new CylinderShape(component.Dimensions);
                case ThingEnum.Cone:
                    var cone = new ConeShape(component.Radius, component.Height);
                    cone.ConeUpIndex = 1;
                    return cone;
                case ThingEnum.Capsule:
                    return new CapsuleShape(component.Radius, component.Height);
                case ThingEnum.Sphere:
                    return new SphereShape(component.Radius);
                case ThingEnum.Hull: {
                        CollisionShape shape;

                        string name = Path.GetFileNameWithoutExtension(component.Mesh);
                        string bulletFilePath = GetBulletFile(name);

                        if (bulletFilePath != null) {
                            // so it has a file
                            shape = ImportCollisionShape(name);
                        }
                        else {
                            /*var sceneMgr = LKernel.GetG<SceneManager>();
                            // get our entity if we have one, create it if we don't
                            Entity ent = sceneMgr.HasEntity(component.Mesh) ? sceneMgr.GetEntity(component.Mesh) : sceneMgr.CreateEntity(component.Mesh, component.Mesh);

                            ConvexHullShape hull = OgreToBulletMesh.ConvertToHull(
                                ent.GetMesh(),
                                component.Transform.GetTrans(),
                                component.Transform.ExtractQuaternion(),
                                Vector3.UNIT_SCALE);
                            shape = hull;

                            // TODO: figure out how to deal with concave triangle mesh shapes since apparently they aren't being exported
                            SerializeShape(shape, name);*/
                            throw new FileNotFoundException("Your \"Mesh\" property did not point to an existing .bullet file!", component.Mesh);
                        }
                        return shape;
                    }
                case ThingEnum.Mesh: {
                        CollisionShape shape;
                        // example
                        // physics/example.bullet
                        string name = Path.GetFileNameWithoutExtension(component.Mesh);
                        string bulletFilePath = GetBulletFile(name);

                        // right, so what we do is test to see if this shape has a .bullet file, and if it doesn't, create one
                        if (bulletFilePath != null) {
                            // so it has a file
                            shape = ImportCollisionShape(name);
                        }
                        else {
                            /*Launch.Log("[CollisionShapeManager] " + bulletFilePath + " does not exist, converting Ogre mesh into physics trimesh and exporting new .bullet file...");

                            // it does not have a file, so we need to convert our ogre mesh
                            var sceneMgr = LKernel.GetG<SceneManager>();
                            Entity ent = sceneMgr.HasEntity(component.Mesh) ? sceneMgr.GetEntity(component.Mesh) : sceneMgr.CreateEntity(component.Mesh, component.Mesh);

                            shape = new BvhTriangleMeshShape(
                                OgreToBulletMesh.Convert(
                                    ent.GetMesh(),
                                    component.Transform.GetTrans(),
                                    component.Transform.ExtractQuaternion(),
                                    Vector3.UNIT_SCALE),
                                true,
                                true);

                            (shape as BvhTriangleMeshShape).BuildOptimizedBvh();

                            // and then export it as a .bullet file
                            SerializeShape(shape, name);*/
                            throw new FileNotFoundException("Your \"Mesh\" property did not point to an existing .bullet file!", component.Mesh);
                        }
                        return shape;
                    }
                case ThingEnum.Heightmap: {
                        string filename = "media/" + component.Mesh;
                        //FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.Read);
                        Bitmap bitmap = new Bitmap(filename);

                        int width = 256;
                        int length = 256;

                        byte[] terr = new byte[width * length * 4];
                        MemoryStream file = new MemoryStream(terr);
                        BinaryWriter writer = new BinaryWriter(file);
                        for (int i = 0; i < width; i++) {
                            for (int j = 0; j < length; j++) {
                                writer.Write(bitmap.GetPixel((int) (((float) i / width) * bitmap.Width), (int) (((float) j / length) * bitmap.Height)).R / 255f);
                                //writer.Write(bitmap.GetPixel(i, j).R / 255f);
                            }
                        }
                        writer.Flush();
                        file.Position = 0;

                        float heightScale = component.MaxHeight - component.MinHeight / 255f;
                        Vector3 scale = component.Dimensions;

                        var heightfield = new HeightfieldTerrainShape(width, length, file, heightScale,
                            component.MinHeight, component.MaxHeight, 1, PhyScalarType.PhyFloat, false);

                        //heightfield.SetUseDiamondSubdivision(true);
                        //heightfield.LocalScaling = new Vector3(scale.x / width, scale.y, scale.z / length);

                        //Matrix4 trans = new Matrix4();
                        //trans.MakeTransform(new Vector3(-scale.x / 2f, scale.y / 2f, -scale.z / 2f), new Vector3(scale.x, 1, scale.z), Quaternion.IDENTITY);
                        //component.Transform = trans;

                        return heightfield;
                    }
                default:
                    throw new ApplicationException("ShapeComponent's Type was invalid!");
            }
        }