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)); }
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; }
/// <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); }
void IConstructable.Construct(IDictionary<string, string> param) { var physData = ResourceFactory.LoadAsset<PhysicsData>(param["physData"]); materials = ResourceFactory.LoadAsset<MaterialData>(param["physData"]); octree = physData.Octree; }
public static Octree BuildOctree(Model model) { List<JVector> vertices = new List<JVector>(); List<TriangleVertexIndices> indices = new List<TriangleVertexIndices>(); Matrix[] bones = new Matrix[model.Bones.Count]; model.CopyAbsoluteBoneTransformsTo(bones); foreach (ModelMesh modelMesh in model.Meshes) { JMatrix boneTransform = PhysicsSystem.toJMatrix(bones[modelMesh.ParentBone.Index]); foreach (ModelMeshPart meshPart in modelMesh.MeshParts) { int offset = vertices.Count; var meshVertices = meshPart.VertexBuffer.Resource.Buffer.GetData<JVector>(); for (int i = 0; i < meshVertices.Length; ++i) { JVector.Transform(ref meshVertices[i], ref boneTransform, out meshVertices[i]); } vertices.AddRange(meshVertices); // append transformed vertices // there should DEFINITELY be a check here to ensure that the indices used in the model // don't exceed 65535 (max short int). If the model doesn't use shorts, then they get cast all weird. var indexElements = meshPart.IndexBuffer.Resource.GetData<short>(); // Each TriangleVertexIndices holds the indices that constitute a triangle primitive TriangleVertexIndices[] tvi = new TriangleVertexIndices[indexElements.Length]; for (int i = 0; i <= tvi.Length - 2; i += 3) { tvi[i].I0 = indexElements[i + 0] + offset; tvi[i].I1 = indexElements[i + 1] + offset; tvi[i].I2 = indexElements[i + 2] + offset; } indices.AddRange(tvi); // append triangles } } Octree ot = new Octree(vertices, indices); //ot.BuildOctree(); // (already happens in Octree constructor) return ot; }
void OnInitialize(InitializeMessage msg) { CreateResources(msg.Device); Entity level = Owner.CreateEntity(); ContentResource<Model> levelmodel = (ContentResource<Model>)Loader.GetResource(levelmodelname); var meshdata = levelmodel.Get<Model>().ExtractData(); Octree leveltree = new Octree(meshdata.Vertices.Select(item => item.ToJitter()).ToList(), meshdata.Indices); level.AddComponent(new RigidBodyComponent( new Jitter.Dynamics.RigidBody( new Jitter.Collision.Shapes.TriangleMeshShape(leveltree)) { IsStatic = true })); Entity levelVisuals = level.CreateChild(); levelVisuals.AddComponent(new Handle<Model>(levelmodelname)); Entity placeholder = Owner.CreateEntity(); placeholder.Transform.Local = Matrix.CreateTranslation(10 * Vector3.UnitY); CreatePlaceholder(placeholder); Entity box = Owner.CreateEntity(); box.Transform.Translate(new Vector3(5, 5, 5)); CreateBox(box); box = Owner.CreateEntity(); box.Transform.Translate(new Vector3(-5, 5, 5)); CreateBox(box); box = Owner.CreateEntity(); box.Transform.Translate(new Vector3(-5, 5, -5)); CreateBox(box); Entity sphere = Owner.CreateEntity(); sphere.Transform.Translate(new Vector3(0, 5, 0)); CreateSphere(sphere); sphere = Owner.CreateEntity(); sphere.Transform.Translate(new Vector3(0, 5, 10)); CreateSphere(sphere); sphere = Owner.CreateEntity(); sphere.Transform.Translate(new Vector3(10, 5, 0)); CreateSphere(sphere); //Entity placeholdertwo = Owner.CreateEntity(); //placeholdertwo.Transform = Matrix.CreateTranslation(5 * Vector3.UnitY + 2 * Vector3.UnitZ); //CreatePlaceholder(placeholdertwo); Entity player = Owner.CreateEntity(); player.Transform.Local = Matrix.CreateRotationY(MathHelper.ToRadians(-120)) * Matrix.CreateTranslation(new Vector3(-20, 10, -14)); CreatePredator(player); Entity camroot = player.CreateChild(); camroot.Transform.Local = Matrix.CreateTranslation(new Vector3(0, 3f, 0)); Entity camera = camroot.CreateChild(); camera.AddComponent(new SpeedTilt(10, 150)); camera.AddComponent(new Camera()); camera.AddComponent(new ParticleEffect()); camera.AddComponent(Loader.GetResource("screen_billboard").ToHandle()); camera.AddComponent(new RenderSettings() { Hidden = true }); //so that the screen_billboard mesh isn't rendered }