예제 #1
0
        protected override Multishape CreateWorkingClone()
        {
            TriangleMeshShape clone = new TriangleMeshShape(this.octree);

            clone.sphericalExpansion = this.sphericalExpansion;
            return(clone);
        }
예제 #2
0
        public override void Build()
        {
            model = Demo.Content.Load<Model>("staticmesh");
            boneTransforms = new Matrix[model.Bones.Count];
            model.CopyAbsoluteBoneTransformsTo(boneTransforms);

            List<TriangleVertexIndices> indices = new List<TriangleVertexIndices>();
            List<Vector3> vertices = new List<Vector3>();

            ExtractData(vertices, indices, model);

            List<JVector> jvertices = new List<JVector>(vertices.Count);
            foreach(Vector3 vertex in vertices) jvertices.Add(Conversion.ToJitterVector(vertex));

            Octree octree = new Octree(jvertices, indices);

            TriangleMeshShape tms = new TriangleMeshShape(octree);
            RigidBody body = new RigidBody(tms);
            body.IsStatic = true;
            //body.EnableDebugDraw = true;
            body.Tag = BodyTag.DontDrawMe;

            Demo.World.AddBody(body);

            AddCar(new JVector(-20, 20, 0));
        }
        protected override void LoadContent()
        {
            boxModel = Content.Load<Model>("box");
            torusModel = Content.Load<Model>("torus");

            // Get the vertex information out of the model
            List<JVector> positions = new List<JVector>();
            List<JOctree.TriangleVertexIndices> indices = new List<JOctree.TriangleVertexIndices>();
            ExtractData(positions, indices, torusModel);

            // Build an octree of it
            JOctree octree = new JOctree(positions, indices);
            octree.BuildOctree();

            // Pass it to a new instance of the triangleMeshShape
            TriangleMeshShape triangleMeshShape = new TriangleMeshShape(octree);

            // Create a body, using the triangleMeshShape
            RigidBody triangleBody = new RigidBody(triangleMeshShape);
            triangleBody.Tag = Color.LightGray;
            triangleBody.Position = new JVector(0, 3, 0);
            
            // Add the mesh to the world.
            world.AddBody(triangleBody);

            base.LoadContent();
        }
예제 #4
0
        protected override Multishape CreateWorkingClone()
        {
            var clone = new TriangleMeshShape(octree)
            {
                SphericalExpansion = SphericalExpansion
            };

            return(clone);
        }
예제 #5
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], boxMesh.indices[i + 1], boxMesh.indices[i + 2]));
                }

                Octree oct = new Octree(jvec, tv);
                TriangleMeshShape trimesh = new TriangleMeshShape(oct);*/
                shape = new BoxShape(boundingBox.Extent.x, boundingBox.Extent.y, boundingBox.Extent.z);
            //    shape = trimesh;
            //    tv.Clear();
             //   jvec.Clear();
             //   oct = null;
             //   boxMesh = null;
            }
            meshes.Clear();
            matrices.Clear();
            geom.Clear();
            return shape;
        }
예제 #6
0
 protected override Multishape CreateWorkingClone()
 {
     TriangleMeshShape clone = new TriangleMeshShape(this.octree);
     clone.sphericalExpansion = this.sphericalExpansion;
     return clone;
 }
예제 #7
0
        /// <summary>
        /// Initialize the physic world with the geometric detail of map.
        /// </summary>
        /// <param name="map">The base to create the physic world.</param>
        private void Initialize(Map.Map map)
        {
            List<JVector> vertices = new List<JVector>();
            List<TriangleVertexIndices> indices = new List<TriangleVertexIndices>();

            for (uint i = 0; i < map.Width; i++)
                for (uint j = 0; j < map.Length; j++)
                    for (uint k = 0; k < map.Height; k++)
                    {
                        int pos = 0;
                        Block block = map.GetBlock(new Vector3(i, j, k));
                        block.CreateColisions();
                        foreach (JVector vertice in block.CollisionVertices)
                        {
                            vertices.Add(vertice);
                            pos++;
                        }

                        int newPos = vertices.Count - pos;
                        foreach (TriangleVertexIndices indice in block.CollisionTriangles)
                        {
                            TriangleVertexIndices t = new TriangleVertexIndices(indice.I0 + newPos, indice.I1 + newPos, indice.I2 + newPos);
                            indices.Add(t);
                        }
                    }

            //ToDo: The vertices list has duplicated vertices. In the worst case each vertices has 4 different instantiation.
            //Probably some performance can be expected by remove the duplicated ones.
            //However it is also necessary to update the indies List with the correct positions.

            Octree tree = new Octree(vertices, indices);
            TriangleMeshShape shape = new TriangleMeshShape(tree);
            RigidBody body = new RigidBody(shape);
            body.IsStatic = true;
            world.AddBody(body);
        }
예제 #8
0
 public void CalculatePhysicsHull()
 {
     JMeshData meshData = model.ModelData.ExtractData();
     JOctree collisionVerts = new JOctree(meshData.Vertices, meshData.Indices);
     TriangleMeshShape shape = new TriangleMeshShape(collisionVerts);
     RigidBody = new RigidBody(shape);
     RigidBody.IsStatic = true; // terrain is immovable
 }
        protected override void LoadContent()
        {
            models[(int)Models.box] = Content.Load<Model>("box");
            models[(int)Models.sphere] = Content.Load<Model>("sphere");
            models[(int)Models.cylinder] = Content.Load<Model>("cylinder");
            models[(int)Models.cone] = Content.Load<Model>("cone");
            models[(int)Models.capsule] = Content.Load<Model>("capsule");
            models[(int)Models.triangle] = Content.Load<Model>("staticmesh");

            List<JVector> vertices = new List<JVector>();
            List<JOctree.TriangleVertexIndices> indices =
                new List<JOctree.TriangleVertexIndices>();

            ExtractData(vertices, indices, models[(int)Models.triangle]);
            JOctree octree = new JOctree(vertices, indices);

            TriangleMeshShape triangleShape = new TriangleMeshShape(octree);
            RigidBody triangleBody = new RigidBody(triangleShape);
            triangleBody.IsStatic = true; triangleBody.Tag = false;
            triangleBody.Position = new JVector(-20, 10, -10);
            world.AddBody(triangleBody);
        }
예제 #10
0
        public static ItemComponent CreateNewItem(MessageProvider messageProvider, GameState state,
            string name, string imageLocation, string description,
            string modelPath, Vector2i size, Vector3 offset, Quaternion rotation, Shape shape, ItemLocation location,
            AttackClass attackClasses, ItemUsage itemUsages, Protection protection, Material physicsMaterial,
            float mass, float healthDelta, float usageDeltaPerUsage, float attackStrength, float throwPower, float usage)
        {
            var entity = EntityFactory.Instance.CreateWith(name, messageProvider,
                systems: new[] { typeof(ItemSystem), typeof(ModelSystem), typeof(PhysicsSystem) });

            var item = entity.GetComponent<ItemComponent>();
            item.ImageLocation = imageLocation;
            item.Description = description;
            item.Size = size;
            item.Location = location;
            item.AttackClasses = attackClasses;
            item.ItemUsages = itemUsages;
            item.Protection = protection;
            item.HealthDelta = healthDelta;
            item.AttackStrength = attackStrength;
            item.ThrowPower = throwPower;
            item.Usage = usage;
            item.UsageDeltaPerUsage = usageDeltaPerUsage;
            item.Mass = mass;
            item.PhysicsMaterial = physicsMaterial;
            item.PositionOffset = offset;
            item.Rotation = rotation;
            item.ItemUsageHandler = new MazeItemUseHandler();

            var model = new ModelSceneObject(modelPath);
            model.Enabled = true;
            state.Scene.AddObject(model);
            entity.GetComponent<ModelComponent>().Model = model;

            var transform = entity.GetComponent<TransformComponent>();
            var physics = entity.GetComponent<PhysicsComponent> ();

            if (shape == null)
            {
                List<JVector> vertices = new List<JVector>();
                model.Model.Meshes[0].Vertices.ForEach(x => vertices.Add(x.ToJitterVector()));

                List<TriangleVertexIndices> indices = new List<TriangleVertexIndices>();

                for(int i = 0; i < model.Model.Meshes[0].Indices.Length; i+= 3)
                {
                    int i0 = model.Model.Meshes[0].Indices[i+0];
                    int i1 = model.Model.Meshes[0].Indices[i+1];
                    int i2 = model.Model.Meshes[0].Indices[i+2];

                    indices.Add(new TriangleVertexIndices(i0, i1, i2));
                }

                shape = new TriangleMeshShape(new Octree(vertices, indices));
            }

            var body = new RigidBody(shape);
            body.Position = transform.Position.ToJitterVector ();
            if (mass >= 0)
                body.Mass = mass;
            body.Material = physicsMaterial;
            body.AllowDeactivation = true;
            body.Tag = entity;

            state.PhysicsManager.World.AddBody(body);
            physics.RigidBody = body;
            physics.World = state.PhysicsManager.World;
            physics.PhysicsApplying = AffectedByPhysics.Orientation | AffectedByPhysics.Position;
            physics.RigidBody.IsStatic = false;
            physics.RigidBody.IsActive = false;
            physics.RigidBody.Position = transform.Position.ToJitterVector();
            model.Position = transform.Position;

            return item;
        }
예제 #11
0
 public static TriangleMeshShape BuildTriangleMeshShape(Model model)
 {
     TriangleMeshShape result = new TriangleMeshShape(BuildOctree(model));
     return result;
 }
예제 #12
0
        public void CalculatePhysicsHull()
        {
            List<JOctree.TriangleVertexIndices> collisionIndices = new List<JOctree.TriangleVertexIndices>();
            var indices = surface.getIndices();
            for (int i = 0; i < indices.Count / 3; i++)
            {
                collisionIndices.Add(new JOctree.TriangleVertexIndices((int)indices[i*3], (int)indices[i*3 + 1], (int)indices[i*3 + 2]));
            }

            PositionMaterialNormalVector verts = surface.getVertices();
            List<JVector> collisionVertPositions = verts.Select<PositionMaterialNormal, JVector>(v => v.position.ToJVector()).ToList();

            JOctree collisionVerts = new JOctree(collisionVertPositions, collisionIndices);
            TriangleMeshShape shape = new TriangleMeshShape(collisionVerts);
            RigidBody = new RigidBody(shape);
            RigidBody.IsStatic = true; // terrain is immovable
        }
예제 #13
0
 void IConstructable.Construct(IDictionary<string, string> param)
 {
     var type = param["type"];
     switch (type)
     {
         case "trimesh":
             var physData = ResourceFactory.LoadAsset<PhysicsData>(param["physData"]);
             Shape shape = new TriangleMeshShape(physData.Octree);
             Body = new RigidBody(shape) { Material = { Restitution = 0f, KineticFriction = 0f } };
             break;
         case "hull":
             physData = ResourceFactory.LoadAsset<PhysicsData>(param["physData"]);
             shape = new ConvexHullShape(physData.Vertices);
             Body = new RigidBody(shape);
             break;
         case "sphere":
             shape = new SphereShape(float.Parse(param["radius"], CultureInfo.InvariantCulture));
             Body = new RigidBody(shape);
             break;
         case "box":
             var d = param["size"].ConvertToVector();
             var offset = param.Get("offset", "0;0;0").ConvertToVector();
             shape = new BoxShape(2.0f * d.ToJVector());
             Body = new RigidBody(shape) { Position = offset.ToJVector() };
             break;
         case "capsule":
             var height = float.Parse(param["height"], CultureInfo.InvariantCulture);
             var radius = float.Parse(param["radius"], CultureInfo.InvariantCulture);
             shape = new CapsuleShape(height, radius);
             Body = new RigidBody(shape)
             {
                 Position = JVector.Backward * (0.5f * height + radius),
                 Orientation = JMatrix.CreateRotationX(MathHelper.PiOver2)
             };
             break;
         default:
             throw new Exception("Unknown shape: " + type);
     }
     Body.IsStatic = Convert.ToBoolean(param.Get("static", "false"));
     Body.Material.KineticFriction = 0.5f;
     Body.Material.StaticFriction = 0.5f;
     Body.Material.Restitution = 0.5f;
 }
        private void SetHeightmap(float[,] heightValues)
        {
            // Remove any previous heightmaps
            if (_terrainBody != null)
                World.RemoveBody(_terrainBody);

            _heightValues = heightValues;

            if (heightValues != null)
            {
                //            new TerrainShape()
                JOctree terrainOctree = HeightmapToOctree(heightValues);
                var terrainShape = new TriangleMeshShape(terrainOctree);

                _terrainBody = new RigidBody(terrainShape);
                _terrainBody.IsStatic = true;
                _terrainBody.Friction = 0.5f;

                World.AddBody(_terrainBody);
            }
        }