public void TestPhysicsMeshPoolConvexMesh() { XNAGame game = new XNAGame(); var mesh = new RAMMesh(); var data = mesh.GetCollisionData(); var convex = new MeshCollisionData.Convex(); convex.Positions = new List <Vector3>(); Vector3[] pos = CreatePyramidPositions(); Vector3[] transpos = new Vector3[pos.Length]; var mat = Matrix.CreateTranslation(20, 5, 20); Vector3.Transform(pos, ref mat, transpos); convex.Positions.AddRange(transpos); data.ConvexMeshes.Add(convex); PhysicsEngine engine = new PhysicsEngine(); PhysicsDebugRendererXNA debugRenderer = null; game.InitializeEvent += delegate { engine.Initialize(); debugRenderer = new PhysicsDebugRendererXNA(game, engine.Scene); debugRenderer.Initialize(game); var pool = new MeshPhysicsPool(); var tMesh1 = pool.CreateConvexMesh(engine.Scene, mesh.GetCollisionData().ConvexMeshes[0]); var tMesh2 = pool.CreateConvexMesh(engine.Scene, mesh.GetCollisionData().ConvexMeshes[0]); Assert.AreEqual(tMesh1, tMesh2); var actorDesc = new ActorDescription(new ConvexShapeDescription() { ConvexMesh = tMesh1 }); engine.Scene.CreateActor(actorDesc); }; game.DrawEvent += delegate { debugRenderer.Render(game); }; int frameNum = 0; game.UpdateEvent += delegate { engine.Update(game.Elapsed); //if (frameNum > 2) game.Exit(); frameNum++; }; game.Run(); }
private void testMeshPhysicsActorBuilder(RAMMesh mesh) { XNAGame game = new XNAGame(); BoundingBox boundingBox = new BoundingBox(); PhysicsEngine engine = new PhysicsEngine(); PhysicsDebugRendererXNA debugRenderer = null; Matrix mirrorMatrix = Matrix.CreateScale(-1, 1, 1); game.InitializeEvent += delegate { engine.Initialize(); debugRenderer = new PhysicsDebugRendererXNA(game, engine.Scene); debugRenderer.Initialize(game); var builder = new MeshPhysicsActorBuilder(new MeshPhysicsPool()); builder.CreateActorStatic(engine.Scene, mesh.GetCollisionData(), Matrix.Identity); boundingBox = builder.CalculateBoundingBox(mesh.GetCollisionData()); builder.CreateActorStatic(engine.Scene, mesh.GetCollisionData(), mirrorMatrix); }; game.DrawEvent += delegate { debugRenderer.Render(game); game.LineManager3D.WorldMatrix = Matrix.Identity; game.LineManager3D.AddBox(boundingBox, Color.Orange); game.LineManager3D.WorldMatrix = mirrorMatrix; game.LineManager3D.AddBox(boundingBox, Color.Yellow); game.LineManager3D.WorldMatrix = Matrix.Identity; }; game.UpdateEvent += delegate { engine.Update(game.Elapsed); if (game.Keyboard.IsKeyPressed(Microsoft.Xna.Framework.Input.Keys.F)) { Actor actor = PhysicsHelper.CreateDynamicSphereActor(engine.Scene, 1, 1); actor.GlobalPosition = game.SpectaterCamera.CameraPosition + game.SpectaterCamera.CameraDirection * 5; actor.LinearVelocity = game.SpectaterCamera.CameraDirection * 5; } }; game.Run(); }
private void convertCollisionData(ObjImporter importer, RAMMesh mesh) { //var boxMaterial = importer.Materials.Find(o => o.Name == "TW-Physics-Box"); for (int i = 0; i < importer.Groups.Count; i++) { var group = importer.Groups[i]; for (int j = 0; j < group.SubObjects.Count; j++) { var subObj = group.SubObjects[j]; if (subObj.Material.Name == materialNamePhysicsBox) { convertSubObjectPhysicsBox(importer, subObj, mesh); } } } var data = mesh.GetCollisionData(); if (!meshHasCollisionData(mesh)) { //Load triangle mesh from positions MeshCollisionData.TriangleMeshData tm = createTriangleMeshForAllObjects(importer); data.TriangleMesh = tm; } }
public List <RAMMesh> CreateMeshesFromObjects(ObjImporter importer) { var meshes = new List <RAMMesh>(); Dictionary <OBJMaterial, MeshCoreData.Material> materials = convertMaterials(importer); for (int i = 0; i < importer.Groups.Count; i++) { var group = importer.Groups[i]; var mesh = new RAMMesh(); meshes.Add(mesh); for (int j = 0; j < group.SubObjects.Count; j++) { var sub = group.SubObjects[j]; convertSubObject(importer, sub, materials, mesh); } if (!meshHasCollisionData(mesh)) { mesh.GetCollisionData().TriangleMesh = createTriangleMeshForGroup(importer, group); } } return(meshes); }
public void TestMeshPhysicsActorBuilderTriangleMesh() { var mesh = new RAMMesh(); var data = mesh.GetCollisionData(); var triangleMesh = new MeshCollisionData.TriangleMeshData(); Vector3[] pos = CreatePyramidPositions(); Vector3[] transpos = new Vector3[pos.Length]; var mat = Matrix.CreateTranslation(20, 5, 20); Vector3.Transform(pos, ref mat, transpos); triangleMesh.Positions = transpos; triangleMesh.Indices = new int[triangleMesh.Positions.Length]; for (int i = 0; i < triangleMesh.Positions.Length; i++) { triangleMesh.Indices[i] = i; } data.TriangleMesh = triangleMesh; testMeshPhysicsActorBuilder(mesh); }
private void convertSubObjectPhysicsTriangleMesh(ObjImporter importer, OBJGroup.SubObject sub, RAMMesh mesh) { if (mesh.GetCollisionData().TriangleMesh != null) { throw new InvalidOperationException("Multiple Physics triangle meshes found in an object!"); } var positions = new List <Vector3>(); addPositionsFromSubObject(importer, sub, positions); var indices = createIndices(positions.Count * 3); var tm = new MeshCollisionData.TriangleMeshData(); tm.Positions = positions.ToArray(); tm.Indices = indices; mesh.GetCollisionData().TriangleMesh = tm; }
public void TestPhysicsMeshPoolTriangleMeshPreload() { XNAGame game = new XNAGame(); RAMMesh mesh = createTriangleMesh(); PhysicsEngine engine = new PhysicsEngine(); MeshPhysicsPool pool = null; game.InitializeEvent += delegate { engine.Initialize(); pool = new MeshPhysicsPool(); pool.PreloadTriangleMesh(engine.Scene, mesh.GetCollisionData().TriangleMesh); }; game.DrawEvent += delegate { }; int frameNum = 0; game.UpdateEvent += delegate { System.Threading.Thread.Sleep(1000); pool.Update(engine.Scene); // This should finish instantly var tMesh1 = pool.CreateTriangleMesh(engine.Scene, mesh.GetCollisionData().TriangleMesh); engine.Update(game.Elapsed); if (frameNum > 2) { game.Exit(); } frameNum++; }; game.Run(); }
private void convertSubObjectPhysicsConvexMesh(ObjImporter importer, OBJGroup.SubObject sub, RAMMesh mesh) { var positions = new List <Vector3>(); addPositionsFromSubObject(importer, sub, positions); var convex = new MeshCollisionData.Convex(); convex.Positions = positions; mesh.GetCollisionData().ConvexMeshes.Add(convex); }
private RAMMesh createTwoBoxMesh() { var mesh = new RAMMesh(); var data = mesh.GetCollisionData(); var box = new MeshCollisionData.Box(); box.Dimensions = Vector3.One * 2; box.Orientation = Matrix.Identity; data.Boxes.Add(box); box = new MeshCollisionData.Box(); box.Dimensions = Vector3.One * 4; box.Orientation = Matrix.CreateTranslation(new Vector3(2, 2, 2)); data.Boxes.Add(box); return(mesh); }
public void TestMeshPhysicsActorBuilderConvex() { var mesh = new RAMMesh(); var data = mesh.GetCollisionData(); var convexMesh = new MeshCollisionData.Convex(); convexMesh.Positions = new List <Vector3>(); Vector3[] pos = CreatePyramidPositions(); Vector3[] transpos = new Vector3[pos.Length]; var mat = Matrix.CreateTranslation(20, 5, 20); Vector3.Transform(pos, ref mat, transpos); convexMesh.Positions.AddRange(transpos); data.ConvexMeshes.Add(convexMesh); testMeshPhysicsActorBuilder(mesh); }
public MeshCollisionData.Box CreateCollisionBox(OBJGroup.SubObject subObj, RAMMesh mesh) { var data = mesh.GetCollisionData(); var positions = new List <Vector3>(); for (int i = 0; i < subObj.Faces.Count; i++) { var face = subObj.Faces[i]; positions.Add(importer.Vertices[face.V1.Position]); positions.Add(importer.Vertices[face.V2.Position]); positions.Add(importer.Vertices[face.V3.Position]); } var bb = BoundingBox.CreateFromPoints(positions); var box = new MeshCollisionData.Box(); box.Dimensions = bb.Max - bb.Min; box.Orientation = Matrix.CreateTranslation((bb.Max + bb.Min) * 0.5f); return(box); }
public void TestMeshPhysicsActorBuilderBoxes() { var mesh = new RAMMesh(); var data = mesh.GetCollisionData(); var box = new MeshCollisionData.Box(); box.Dimensions = Vector3.One; box.Orientation = Matrix.Identity; data.Boxes.Add(box); box = new MeshCollisionData.Box(); box.Dimensions = Vector3.One * 3; box.Orientation = Matrix.CreateTranslation(new Vector3(2, 2, 2)); data.Boxes.Add(box); testMeshPhysicsActorBuilder(mesh); }
private bool meshHasCollisionData(RAMMesh mesh) { var data = mesh.GetCollisionData(); return(!(data.Boxes.Count == 0 && data.TriangleMesh == null && data.ConvexMeshes.Count == 0)); }
public void TestMeshDynamicPhysicsElement() { XNAGame game = new XNAGame(); var mesh = new RAMMesh(); var data = mesh.GetCollisionData(); var box = new MeshCollisionData.Box(); box.Dimensions = Vector3.One * 2; box.Orientation = Matrix.Identity; data.Boxes.Add(box); box = new MeshCollisionData.Box(); box.Dimensions = Vector3.One * 4; box.Orientation = Matrix.CreateTranslation(new Vector3(2, 2, 2)); data.Boxes.Add(box); BoundingBox bb = new BoundingBox(); PhysicsEngine engine = new PhysicsEngine(); PhysicsDebugRendererXNA debugRenderer = null; TheWizards.Client.ClientPhysicsQuadTreeNode root; root = new ClientPhysicsQuadTreeNode( new BoundingBox( new Vector3(-16 * 16 / 2f, -100, -16 * 16 / 2f), new Vector3(16 * 16 / 2f, 100, 16 * 16 / 2f))); QuadTree.Split(root, 4); var builder = new MeshPhysicsActorBuilder(new MeshPhysicsPool()); var visualizer = new QuadTreeVisualizerXNA(); float time = 0; var spheres = new List <MeshDynamicPhysicsElement>(); var meshes = new List <MeshStaticPhysicsElement>(); var physicsElementFactoryXNA = new MeshPhysicsFactoryXNA(engine, root); var factory = physicsElementFactoryXNA.Factory; game.AddXNAObject(physicsElementFactoryXNA); game.InitializeEvent += delegate { engine.Initialize(); debugRenderer = new PhysicsDebugRendererXNA(game, engine.Scene); debugRenderer.Initialize(game); }; game.DrawEvent += delegate { debugRenderer.Render(game); visualizer.RenderNodeGroundBoundig(game, root, delegate(ClientPhysicsQuadTreeNode node, out Color col) { col = Color.Green; return(node.PhysicsObjects.Count == 0); }); visualizer.RenderNodeGroundBoundig(game, root, delegate(ClientPhysicsQuadTreeNode node, out Color col) { col = Color.Orange; return(node.PhysicsObjects.Count > 0); }); for (int i = 0; i < meshes.Count; i++) { game.LineManager3D.AddCenteredBox(meshes[i].BoundingSphere.Center, meshes[i].BoundingSphere.Radius * 2, Color.Black); } }; game.UpdateEvent += delegate { time += game.Elapsed; if (game.Keyboard.IsKeyPressed(Microsoft.Xna.Framework.Input.Keys.F)) { var dEl = new MeshDynamicPhysicsElement(mesh, Matrix.CreateTranslation( game.SpectaterCamera.CameraPosition + game.SpectaterCamera.CameraDirection), builder); dEl.InitDynamic(engine.Scene); dEl.Actor.LinearVelocity = game.SpectaterCamera.CameraDirection * 10; spheres.Add(dEl); } for (int i = 0; i < spheres.Count; i++) { spheres[i].Update(root); } engine.Update(game.Elapsed); }; game.Run(); }
public void TestPhysicsMeshPoolTriangleMesh() { XNAGame game = new XNAGame(); var mesh = new RAMMesh(); var data = mesh.GetCollisionData(); var triangleMesh = new MeshCollisionData.TriangleMeshData(); Vector3[] pos = CreatePyramidPositions(); Vector3[] transpos = new Vector3[pos.Length]; var mat = Matrix.CreateTranslation(20, 5, 20); Vector3.Transform(pos, ref mat, transpos); triangleMesh.Positions = transpos; triangleMesh.Indices = new int[triangleMesh.Positions.Length]; for (int i = 0; i < triangleMesh.Positions.Length; i++) { triangleMesh.Indices[i] = i; } data.TriangleMesh = triangleMesh; PhysicsEngine engine = new PhysicsEngine(); PhysicsDebugRendererXNA debugRenderer = null; game.InitializeEvent += delegate { engine.Initialize(); debugRenderer = new PhysicsDebugRendererXNA(game, engine.Scene); debugRenderer.Initialize(game); var pool = new MeshPhysicsPool(); var tMesh1 = pool.CreateTriangleMesh(engine.Scene, mesh.GetCollisionData().TriangleMesh); var tMesh2 = pool.CreateTriangleMesh(engine.Scene, mesh.GetCollisionData().TriangleMesh); Assert.AreEqual(tMesh1, tMesh2); var actorDesc = new ActorDescription(new TriangleMeshShapeDescription() { TriangleMesh = tMesh1 }); engine.Scene.CreateActor(actorDesc); }; game.DrawEvent += delegate { debugRenderer.Render(game); }; int frameNum = 0; game.UpdateEvent += delegate { engine.Update(game.Elapsed); //if (frameNum > 2) game.Exit(); frameNum++; }; game.Run(); }