public BoundingBox SetBoundingBoxWanderer(RobotCameraComponent robot, Matrix world) { Vector3 min = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue); Vector3 max = new Vector3(float.MinValue, float.MinValue, float.MinValue); // For each mesh of the model foreach (ModelMesh mesh in robot.Model.Meshes) { foreach (ModelMeshPart meshPart in mesh.MeshParts) { // Vertex buffer parameters int vertexStride = meshPart.VertexBuffer.VertexDeclaration.VertexStride; int vertexBufferSize = meshPart.NumVertices * vertexStride; // Get vertex data as float float[] vertexData = new float[vertexBufferSize / sizeof(float)]; meshPart.VertexBuffer.GetData <float>(vertexData); // Iterate through vertices (possibly) growing bounding box, all calculations are done in world space for (int i = 0; i < vertexBufferSize / sizeof(float); i += vertexStride / sizeof(float)) { Vector3 transformedPosition = Vector3.Transform(new Vector3(vertexData[i], vertexData[i + 1], vertexData[i + 2]), world); min = Vector3.Min(min, transformedPosition); max = Vector3.Max(max, transformedPosition); } } } return(new BoundingBox(min, max)); }
/// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); Texture2D heightMapTexture2D = Content.Load <Texture2D>("US_Canyon"); Texture2D heightMapGrassTexture = Content.Load <Texture2D>("grass2"); Texture2D heightMapFireTexture = Content.Load <Texture2D>("fire"); Texture2D BrickTexture = Content.Load <Texture2D>("wall_texture"); Model robotModel = Content.Load <Model>("Lab2Model"); Texture2D robotTexture = Content.Load <Texture2D>("robot_texture"); Texture2D roofTexture = Content.Load <Texture2D>("roof"); Texture2D timberWallTexture = Content.Load <Texture2D>("timber_wall"); Texture2D timberRoofTexture = Content.Load <Texture2D>("timber_roof"); heightMapComponent = new HeightMapComponent { HeightMap = heightMapTexture2D, HeightMapTexture = heightMapGrassTexture, GraphicsDevice = graphics.GraphicsDevice, Width = heightMapTexture2D.Width, Height = heightMapTexture2D.Height, HeightMapData = new float[heightMapTexture2D.Width, heightMapTexture2D.Height] }; heightMapCameraComponent = new HeightMapCameraComponent() { ViewMatrix = Matrix.CreateLookAt(new Vector3(-100, 0, 0), Vector3.Zero, Vector3.Up), ProjectionMatrix = Matrix.CreatePerspective(1.2f, 0.9f, 1.0f, 1000.0f), TerrainMatrix = Matrix.CreateTranslation(new Vector3(0, -100, 256)), Position = new Vector3(-100, 0, 0), Direction = Vector3.Zero, Movement = new Vector3(1, 1, 1), Rotation = new Vector3(2, 2, 2) * 0.01f, TerrainPosition = new Vector3(0, -100, 256), }; heightMapCameraComponent.Frustum = new BoundingFrustum(heightMapCameraComponent.ViewMatrix * heightMapCameraComponent.ProjectionMatrix); //var h1 = new HouseComponent(new Vector3(40, 100, 40), new Vector3(100, 50, -100), Matrix.Identity, BrickTexture, roofTexture); //var h2 = new HouseComponent(new Vector3(40, 100, 40), new Vector3(950, 50, -100), Matrix.Identity, BrickTexture, roofTexture); //var h3 = new HouseComponent(new Vector3(40, 100, 40), new Vector3(950, 50, -900), Matrix.Identity, BrickTexture, roofTexture); //int h1Id = EntityComponentManager.GetManager().CreateNewEntityId(); //EntityComponentManager.GetManager().AddComponentToEntity(h1Id, h1); //int h2Id = EntityComponentManager.GetManager().CreateNewEntityId(); //EntityComponentManager.GetManager().AddComponentToEntity(h2Id, h2); //int h3Id = EntityComponentManager.GetManager().CreateNewEntityId(); //EntityComponentManager.GetManager().AddComponentToEntity(h3Id, h3); int heightMapId = EntityComponentManager.GetManager().CreateNewEntityId(); EntityComponentManager.GetManager().AddComponentToEntity(heightMapId, heightMapComponent); EntityComponentManager.GetManager().AddComponentToEntity(heightMapId, heightMapCameraComponent); CreateRandomHouses(10, BrickTexture, roofTexture, timberWallTexture, timberRoofTexture); heightMapSystem.CreateHeightMaps(); robotComponent = new RobotComponent { Speed = 0, Texture = robotTexture, PlaneObjectWorld = Matrix.Identity, TransformMatrices = new Matrix[robotModel.Bones.Count], Effect = new BasicEffect(graphics.GraphicsDevice), Scale = Matrix.CreateScale(0.5f), Position = new Vector3(0f, 1300f, 0f), RobotProjection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver2, 1280 / 720, 0.1f, 500f), RobotView = Matrix.CreateLookAt(new Vector3(70, 50, 30), new Vector3(0, 0, 20), Vector3.Backward) }; robotCameraComponent = new RobotCameraComponent { MaxRotation = MathHelper.PiOver4, RotationSpeed = 0.003f, ModelRotation = 0, Model = robotModel, Direction = true, LeftArmMatrix = robotModel.Bones["LeftArm"].Transform, RightArmMatrix = robotModel.Bones["RightArm"].Transform, LeftLegMatrix = robotModel.Bones["LeftLeg"].Transform, RightLegMatrix = robotModel.Bones["RightLeg"].Transform, Rotation = Quaternion.CreateFromAxisAngle(Vector3.Right, MathHelper.PiOver2) * Quaternion.CreateFromAxisAngle(Vector3.Up, MathHelper.Pi), RotationInDegrees = 0 }; int robotId = EntityComponentManager.GetManager().CreateNewEntityId(); EntityComponentManager.GetManager().AddComponentToEntity(robotId, robotComponent); EntityComponentManager.GetManager().AddComponentToEntity(robotId, robotCameraComponent); //robotSystem.CreateRobots(); }