/// <summary> /// Creates a detector volume. /// </summary> /// <param name="triangleMesh">Arbitrary closed triangle mesh representing the volume.</param> /// <param name="queryAccelerator">System used to find nearby objects.</param> public DetectorVolume(MeshBoundingBoxTreeData triangleMesh, IQueryAccelerator queryAccelerator) { TriangleMesh = new TriangleMesh(triangleMesh); QueryAccelerator = queryAccelerator; collisionRules = new CollisionRules() { group = new CollisionGroup() }; }
/// <summary> /// Create sphere model physics. /// </summary> /// <param name="game">The game.</param> /// <param name="model">The visual model.</param> /// <param name="position">The position of the static mesh entity.</param> /// <param name="mass">The physics mass.</param> public StaticMeshModelPhysics(XiGame game, Model model, Vector3 position, float mass) { this.game = game; StaticTriangleGroup.StaticTriangleGroupVertex[] vertices; int[] indices; model.GetVerticesAndIndices(out vertices, out indices); TriangleMesh triangleMesh = new TriangleMesh(vertices, indices); staticTriangleGroup = new StaticTriangleGroup(triangleMesh); }
///<summary> /// Constructs a new mobile mesh shape. ///</summary> ///<param name="vertices">Vertices of the mesh.</param> ///<param name="indices">Indices of the mesh.</param> ///<param name="localTransform">Local transform to apply to the shape.</param> ///<param name="solidity">Solidity state of the shape.</param> public MobileMeshShape(System.Numerics.Vector3[] vertices, int[] indices, AffineTransform localTransform, MobileMeshSolidity solidity) { this.solidity = solidity; var data = new TransformableMeshData(vertices, indices, localTransform); var shapeDistributionInformation = ComputeVolumeDistribution(data); data.worldTransform.Translation -= shapeDistributionInformation.Center; triangleMesh = new TriangleMesh(data); UpdateEntityShapeVolume(new EntityShapeVolumeDescription { Volume = shapeDistributionInformation.Volume, VolumeDistribution = shapeDistributionInformation.VolumeDistribution }); ComputeSolidSidedness(); UpdateSurfaceVertices(); }
protected override void LoadContent() { // decompose collision hull into triangles StaticTriangleGroup.StaticTriangleGroupVertex[] verts; int[] indices; Model m = Game.Content.Load<Model>(colHullPath); startingPos = (List<Matrix>)m.Tag; StaticTriangleGroup.GetVerticesAndIndicesFromModel(m, out verts, out indices); // convert to static mesh TriangleMesh triMesh = new TriangleMesh(verts, indices); StaticTriangleGroup triGroup = new StaticTriangleGroup(triMesh); //triGroup.WorldMatrix = this.worldMatrix; // add it to physics: Engine.currentWorld.Space.Add(triGroup); // invoke renderer loading: renderer.LoadModels(); base.LoadContent(); }
/// <summary> /// Creates a detector volume. /// </summary> /// <param name="triangleMesh">Closed and consistently wound mesh defining the volume.</param> public DetectorVolume(TriangleMesh triangleMesh) { TriangleMesh = triangleMesh; UpdateBoundingBox(); }
protected override void OnShapeChanged(CollisionShape collisionShape) { if (!IgnoreShapeChanges) { mesh = new TriangleMesh(Shape.TriangleMeshData); UpdateBoundingBox(); } }
///<summary> /// Constructs a new mobile mesh shape. ///</summary> ///<param name="vertices">Vertices of the mesh.</param> ///<param name="indices">Indices of the mesh.</param> ///<param name="localTransform">Local transform to apply to the shape.</param> ///<param name="solidity">Solidity state of the shape.</param> ///<param name="distributionInfo">Information computed about the shape during construction.</param> public MobileMeshShape(Vector3[] vertices, int[] indices, AffineTransform localTransform, MobileMeshSolidity solidity, out ShapeDistributionInformation distributionInfo) { this.solidity = solidity; var data = new TransformableMeshData(vertices, indices, localTransform); ComputeShapeInformation(data, out distributionInfo); for (int i = 0; i < surfaceVertices.count; i++) { Vector3.Subtract(ref surfaceVertices.Elements[i], ref distributionInfo.Center, out surfaceVertices.Elements[i]); } triangleMesh = new TriangleMesh(data); ComputeSolidSidedness(); //ComputeBoundingHull(); }
///<summary> /// Constructs a new instanced mesh shape. ///</summary> ///<param name="vertices">Vertices of the mesh.</param> ///<param name="indices">Indices of the mesh.</param> public InstancedMeshShape(Vector3[] vertices, int[] indices) { TriangleMesh = new TriangleMesh(new StaticMeshData(vertices, indices)); }
///<summary> /// Constructs a new mobile mesh shape from cached data. ///</summary> ///<param name="meshData">Mesh data reprsenting the shape. Should already be properly centered.</param> /// <param name="hullVertices">Outer hull vertices of the mobile mesh shape used to quickly compute the bounding box.</param> ///<param name="solidity">Solidity state of the shape.</param> /// <param name="sidednessWhenSolid">Triangle sidedness to use when the shape is solid.</param> /// <param name="collisionMargin">Collision margin used to expand the mesh triangles.</param> /// <param name="volumeDescription">Description of the volume and its distribution in the shape. Assumed to be correct; no processing or validation is performed.</param> public MobileMeshShape(TransformableMeshData meshData, IList<Vector3> hullVertices, MobileMeshSolidity solidity, TriangleSidedness sidednessWhenSolid, float collisionMargin, EntityShapeVolumeDescription volumeDescription) { triangleMesh = new TriangleMesh(meshData); this.hullVertices = new RawList<Vector3>(hullVertices); meshCollisionMargin = collisionMargin; this.solidity = solidity; SidednessWhenSolid = sidednessWhenSolid; UpdateEntityShapeVolume(volumeDescription); }
public void LoadContent() { #region BEPU Physic #region MultiThread //Give the space some threads to work with. #if XBOX360 //Note that not all four available hardware threads are used. //Currently, BEPUphysics will allocate an equal amount of work to each thread. //If two threads are put on one core, it will bottleneck the engine and run significantly slower than using 3 hardware threads. Space.ThreadManager.AddThread(delegate(object information) { Thread.CurrentThread.SetProcessorAffinity(new[] { 1 }); }, null); Space.ThreadManager.AddThread(delegate(object information) { Thread.CurrentThread.SetProcessorAffinity(new[] { 3 }); }, null); Space.ThreadManager.AddThread(delegate(object information) { Thread.CurrentThread.SetProcessorAffinity(new[] { 5 }); }, null); //Enable the multithreading system! //space.useMultithreadedUpdate = true; #else if (Environment.ProcessorCount > 1) { //On windows, just throw a thread at every processor. The thread scheduler will take care of where to put them. for (int i = 0; i < Environment.ProcessorCount; i++) { space.ThreadManager.AddThread(); } //Enable the multithreading system! space.UseMultithreadedUpdate = true; } #endif #endregion space.SimulationSettings.MotionUpdate.Gravity = new Vector3(0, -98.81f, 0); space.SimulationSettings.CollisionResponse.Iterations = 10; space.SimulationSettings.CollisionResponse.IterationsBeforeEarlyOut = 0; space.SimulationSettings.TimeStep.TimeStepDuration = 1 / 70f; Model CubeModel; CubeModel = Content.Load<Model>("Models/cube"); Wall.BlockModel = Content.Load<Model>("Models/wall_cube"); HardWall.BlockModel = Content.Load<Model>("Models/wall_cube"); Health.BlockModel = Content.Load<Model>("Models/health"); CannonBall.Model = Content.Load<Model>("Models/sphere"); EnemyCannonBall.Model = Content.Load<Model>("Models/sphere"); CannonBall.game = game; EnemyCannonBall.game = game; game.Components.Add(CannonBallManager); // adding walls // Content.Load<Model>("Models/detectorMiddleOuter") for (int i = 0; i < 1001; i = i + 200) { WallStreet.Add(new Wall(game, space, Matrix.Identity, Matrix.CreateTranslation(new Vector3(i - 1210, -380 + (float)i / 60f, -460)), 10, 5, 200, 60, 10)); WallStreet.Add(new Wall(game, space, Matrix.Identity, Matrix.CreateRotationY(1.57f) * Matrix.CreateTranslation(new Vector3(-1010, -380, -1070+i)), 10, 5, 200, 60, 10)); HardWallStreet.Add(new HardWall(game, space, Matrix.Identity, Matrix.CreateTranslation(new Vector3(1810+i, -389, 2960)), 6, 3, 24, 200, 24)); } for (int i = 0; i < WallStreet.Count; i++) { game.Components.Add(WallStreet[i]); WallStreet[i].DrawOrder = 102; } for (int i = 0; i < HardWallStreet.Count; i++) game.Components.Add(HardWallStreet[i]); HardWallStreet.Add(new HardWall(game, space, Matrix.Identity, Matrix.CreateTranslation(new Vector3(-1710, -360, 310)), 1, 5, 24, 200, 24)); HardWallStreet.Add(new HardWall(game, space, Matrix.Identity, Matrix.CreateTranslation(new Vector3(-1710, -360, 190)), 1, 5, 24, 200, 24)); // Components.Add(new Wall(game, space, Matrix.Identity, Matrix.CreateTranslation(new Vector3(10, -350, -360)) * Matrix.CreateRotationY(1.57f), 5, 5, 121, 37, 10)); // Position // entity offset // entity size Building church = new Building(game, space, new Vector3(-2100, -40, 250), new Box( new Vector3(-70, -210, 0), 685, 400, 365),15f); church.BlockModel = Content.Load<Model>("Models/88cathedral"); church.DrawOrder = 102; Components.Add(church); /*Building oldBarn = new Building(game, space, new Vector3(1840, -200, 2390), new Box(new Vector3(-70, -210, 0), 685, 400, 365),120f); oldBarn.BlockModel = Content.Load<Model>("Models/street-lamppost"); Components.Add(oldBarn);*/ healthManager.Health = new Health(game, space, new Vector3(-100, 0, -170)); Components.Add(healthManager.Health); // Border Model BorderCube; BorderCube = Content.Load<Model>("Models/border"); Box entity; for (int i = -3600; i < 3601; i += 7200) { entity = new Box(new Vector3(0, -380, i), 7200, 450, 10); entity.EventManager.InitialCollisionDetected += borderCollision; Box box = entity as Box; Matrix scaling = Matrix.CreateScale(box.Width/10f, box.Height/10f, box.Length/10f); EntityModel model1 = new EntityModel(entity, BorderCube, scaling, game); entity.Tag = model1; space.Add(entity); game.Components.Add(model1); } for (int i = -3600; i < 3601; i += 7200) { entity = new Box(new Vector3(i, -380, 0), 10, 450, 7200); entity.EventManager.InitialCollisionDetected += borderCollision; Box box = entity as Box; Matrix scaling = Matrix.CreateScale(box.Width / 10f, box.Height / 10f, box.Length / 10f); EntityModel model1 = new EntityModel(entity, BorderCube, scaling, game); space.Add(entity); game.Components.Add(model1); } //space.Add(new Box(new Vector3(0, 8, 0), 10, 10, 10, 5)); //space.Add(new Box(new Vector3(0, 12, 0), 10, 10, 10, 5)); //Create a physical environment from a triangle mesh. //First, collect the the mesh data from the model using a helper function. //This special kind of vertex inherits from the TriangleMeshVertex and optionally includes //friction/bounciness data. //The StaticTriangleGroup requires that this special vertex type is used in lieu of a normal TriangleMeshVertex array. StaticTriangleGroup.StaticTriangleGroupVertex[] vertices2; int[] indices; StaticTriangleGroup.GetVerticesAndIndicesFromModel(terrain.Model, out vertices2, out indices); //Give the mesh information to a new TriangleMesh. //TriangleMeshes are internally accelerated structures that //help handle raycasts and various other queries on mesh data. TriangleMesh triangleMesh = new TriangleMesh(vertices2, indices); //Create the StaticTriangleGroup based on the triangle mesh. StaticTriangleGroup triangleGroup = new StaticTriangleGroup(triangleMesh); //Scoot the mesh down so its below the previously created kinematic box. // triangleGroup.WorldMatrix = Matrix.CreateTranslation(new Vector3(0, -40, 0)); //Add it to the space! space.Add(triangleGroup); triangleGroup.Tag = triangleMesh; //Make it visible too. Components.Add(new StaticModel(terrain.Model, triangleGroup.WorldMatrix, game)); tank_box = new Box(tank.Position, 60, 40, 65, 50); //Components.Add(tank_box); space.Add(tank_box); tank_box.Tag = player; tank_box.EventManager.InitialCollisionDetected += tankCollision; CannonBall.CannonBallCollisionGroup = new CollisionGroup(); space.SimulationSettings.CollisionDetection.CollisionGroupRules.Add(new CollisionGroupPair(tank_box.CollisionRules.Group, CannonBall.CannonBallCollisionGroup), CollisionRule.NoPair); triangleGroup.CollisionRules.SpecificEntities.Add(tank_box, CollisionRule.NoPair); space.SimulationSettings.CollisionDetection.CollisionGroupRules.Add(new CollisionGroupPair(Enemy.EnemyTankCollisionGroup, EnemyCannonBall.EnemyCannonBallCollisionGroup), CollisionRule.NoPair); #endregion }