Exemplo n.º 1
0
        private Shape CreateShape()
        {
            Shape shape = null;

            boundingBox = new BoundingBox();
            List <Geometry> geom     = RenderUtil.GatherGeometry(GameObject);
            List <Mesh>     meshes   = new List <Mesh>();
            List <Matrix4f> matrices = new List <Matrix4f>();
            Vector3f        mytrans  = GameObject.GetUpdatedWorldTranslation();

            if (GameObject is Geometry)
            {
                Geometry g_obj = (Geometry)GameObject;

                Mesh m = g_obj.Mesh;
                meshes.Add(m);
                Transform ttransform = new Transform();
                ttransform.SetTranslation(g_obj.GetUpdatedWorldTranslation());
                ttransform.SetRotation(g_obj.GetUpdatedWorldRotation());
                ttransform.SetScale(g_obj.GetUpdatedWorldScale());
                Matrix4f    matrix = ttransform.GetMatrix();
                BoundingBox tmpBB  = m.CreateBoundingBox();
                matrices.Add(matrix);
                boundingBox.Extend(tmpBB);
            }
            for (int i = 0; i < geom.Count; i++)
            {
                Geometry g = geom[i];
                if (g != GameObject)
                {
                    Mesh m = g.Mesh;
                    meshes.Add(m);
                    Transform ttransform = new Transform();
                    ttransform.SetTranslation(g.GetUpdatedWorldTranslation().Subtract(mytrans));
                    ttransform.SetRotation(g.GetUpdatedWorldRotation());
                    ttransform.SetScale(g.GetUpdatedWorldScale());
                    Matrix4f    matrix = ttransform.GetMatrix();
                    BoundingBox tmpBB  = m.CreateBoundingBox(matrix);
                    matrices.Add(matrix);
                    boundingBox.Extend(tmpBB);
                }
            }
            if (physicsShape == PhysicsWorld.PhysicsShape.StaticMesh)
            {
                List <JVector> jvec             = new List <JVector>();
                List <TriangleVertexIndices> tv = new List <TriangleVertexIndices>();
                List <Vector3f> vertexPositions = new List <Vector3f>();

                for (int m = 0; m < meshes.Count; m++)
                {
                    for (int v = 0; v < meshes[m].vertices.Count; v++)
                    {
                        Vector3f myvec = meshes[m].vertices[v].GetPosition().Multiply(matrices[m]);
                        jvec.Add(new JVector(myvec.x, myvec.y, myvec.z));
                    }
                    for (int i = 0; i < meshes[m].indices.Count; i += 3)
                    {
                        tv.Add(new TriangleVertexIndices(meshes[m].indices[i + 2], meshes[m].indices[i + 1], meshes[m].indices[i]));
                    }
                }
                Octree            oct     = new Octree(jvec, tv);
                TriangleMeshShape trimesh = new TriangleMeshShape(oct);

                shape = trimesh;
                oct   = null;
                jvec.Clear();
                tv.Clear();
            }
            else if (physicsShape == PhysicsWorld.PhysicsShape.ConvexMesh)
            {
                List <JVector>  jvec            = new List <JVector>();
                List <Vector3f> vertexPositions = new List <Vector3f>();

                for (int m = 0; m < meshes.Count; m++)
                {
                    for (int v = 0; v < meshes[m].indices.Count; v++)
                    {
                        Vector3f vec = meshes[m].vertices[meshes[m].indices[v]].GetPosition().Multiply(matrices[m]);
                        jvec.Add(new JVector(vec.x, vec.y, vec.z));
                    }
                }

                ConvexHullShape hullShape = new ConvexHullShape(jvec);
                shape = hullShape;
            }
            else if (physicsShape == PhysicsWorld.PhysicsShape.Box)
            {
                Mesh           boxMesh          = MeshFactory.CreateCube(boundingBox.Min, boundingBox.Max);
                List <JVector> jvec             = new List <JVector>();
                List <TriangleVertexIndices> tv = new List <TriangleVertexIndices>();
                for (int v = 0; v < boxMesh.vertices.Count; v++)
                {
                    Vector3f myvec = boxMesh.vertices[v].GetPosition();
                    jvec.Add(new JVector(myvec.x, myvec.y, myvec.z));
                }
                for (int i = 0; i < boxMesh.indices.Count; i += 3)
                {
                    tv.Add(new TriangleVertexIndices(boxMesh.indices[i + 2], boxMesh.indices[i + 1], boxMesh.indices[i]));
                }

                Octree            oct     = new Octree(jvec, tv);
                TriangleMeshShape trimesh = new TriangleMeshShape(oct);
                shape = trimesh;
                tv.Clear();
                jvec.Clear();
                oct     = null;
                boxMesh = null;
            }
            meshes.Clear();
            matrices.Clear();
            geom.Clear();
            return(shape);
        }