public override void OnEndDrawMesh(GraphicsDevice graphicsDevice, ModelMesh mesh, Matrix bone, Matrix view, Matrix projection) { if (!Active) { return; } BoundingBoxBuffers boundingBoxBuffers = mesh.Tag as BoundingBoxBuffers; if (boundingBoxBuffers == null) { return; } graphicsDevice.SetVertexBuffer(boundingBoxBuffers.Vertices); graphicsDevice.Indices = boundingBoxBuffers.Indices; _lineEffect.World = bone; _lineEffect.View = view; _lineEffect.Projection = projection; foreach (EffectPass pass in _lineEffect.CurrentTechnique.Passes) { pass.Apply(); graphicsDevice.DrawIndexedPrimitives(PrimitiveType.LineList, 0, 0, boundingBoxBuffers.VertexCount, 0, boundingBoxBuffers.PrimitiveCount); } }
/// <summary> /// Initializes a new instance of the <see cref="BoundingBoxRenderer"/> class. /// </summary> /// <param name="device">The device.</param> /// <param name="model">The model.</param> public BoundingBoxRenderer(GraphicsDevice device, Model model) { this.device = device; BoundingBox boundingBox = CreateBoundingBox(model); buffers = CreateBoundingBoxBuffers(boundingBox); effect = new BasicEffect(device); effect.LightingEnabled = false; effect.TextureEnabled = false; effect.VertexColorEnabled = true; }
public BoundingBoxO(Model m, Color c) { this.model = m; this.boundingBox = CreateBoundingBox(model); _buffers = CreateBoundingBoxBuffers(boundingBox, Settings.gDevice); _effect = new BasicEffect(Settings.gDevice); _effect.LightingEnabled = false; _effect.TextureEnabled = false; _effect.VertexColorEnabled = true; _effect.DiffuseColor = c.ToVector3(); }
//------------------------------------------------------------------------- //Implementing "Gravity Class" *Schuyler, I moved the gravity to the move section /* public void Fall(List<ScreenModel> VisibleObjects) { //Console.WriteLine("Falling "+FallCntr); //local calculation done each time to keep track of the change that has been done (velocity*acceleration) float KeepTrackOfChange = 0f; if (FallCntr == 0) this.Velocity.Y += DataValues.gravity; //acceleration crashes the game....not sure why!!!!!!!!!...not very necessery though //increase velocity due to gravitational acceleration until terminal velocity is reached if ((this.Velocity.Y * DataValues.GravityAcceleration) > DataValues.TerminalVelocity) { this.Velocity.Y = (this.Velocity.Y>0)?this.Velocity.Y/DataValues.GravityAcceleration:this.Velocity.Y*DataValues.GravityAcceleration; if (this.Velocity.Y < .002f) this.Velocity.Y *= -1; //KeepTrackOfChange = this.Velocity.Y; } //Console.WriteLine("Velocity Y: "+this.Velocity.Y); if (Position.Y > 0)//(this.GetBoundingSpheres().Radius)) { Position.Y += KeepTrackOfChange;//this.Velocity.Y; FallCntr++; } /* if(this.CollisionType == ObjectTypes.Sphere) { //gravity moves object down if (Position.Y > 0)//(this.GetBoundingSpheres().Radius)) { Position.Y -= KeepTrackOfChange;//this.Velocity.Y; FallCntr++; } //object has hit the ground or an object underneath it if ((CheckCollision(VisibleObjects) != null) || (Position.Y <= 0)) { //undoes the last iteration CORRECTLY NOW THAT THE ACCELERATION AFFECT HAS BEEN KEPT TRACK OF Position.Y += KeepTrackOfChange;//this.Velocity.Y; //resets the fall cntr FallCntr = 0; //Velocity is no longer effected by gravity //this.Velocity.Y -= DataValues.gravity; this.Velocity.Y *= Bounce * -1; //object can no longer fall if(this.Velocity.Y==0) IsCurrentlyFalling = false; } } if(this.CollisionType == ObjectTypes.Box) { //gravity moves object down if (Position.Y > 0)//(this.GetBoundingSpheres().Radius)) { Position.Y -= KeepTrackOfChange;//this.Velocity.Y; //moves bounding box this.MoveModelBox(new Vector3(0f, -KeepTrackOfChange, 0f)); FallCntr++; } //object has hit the ground or an object underneath it if ((CheckCollision(VisibleObjects) != null)||(Position.Y<=0)) { //undoes the last iteration CORRECTLY NOW THAT THE ACCELERATION AFFECT HAS BEEN KEPT TRACK OF Position.Y += KeepTrackOfChange;//this.Velocity.Y; this.MoveModelBox(new Vector3(0f, KeepTrackOfChange, 0f)); //resets the fall cntr FallCntr = 0; //Velocity is no longer effected by gravity //this.Velocity.Y -= DataValues.gravity; this.Velocity.Y *= Bounce * -1; //object can no longer fall if (this.Velocity.Y == 0) IsCurrentlyFalling = false; } } }*/ //------------------------------------------------------------------------ //Methods that provide support for collision checking //------------------------------------------------------------------------ //sets the velocity to one or zero; hopefully makes it easier to start/stop and object public void BoundingSetup() { //Gets the object type attribute from the List<object> DataFromProcessor = (List<object>)MyModel.Tag; CollisionType = (ObjectTypes)DataFromProcessor[0]; //only allocates space when the bounding box must be checked; //otherwise, then use the sphere to check if (CollisionType == ObjectTypes.Box) { MovingBoxes = new List<OrientedBoundingBox>(0); foreach (ModelMesh mesh in this.MyModel.Meshes) foreach (ModelMeshPart part in mesh.MeshParts) MovingBoxes.Add(new OrientedBoundingBox(Vector3.Zero, Vector3.Zero, new Quaternion())); //allocates the bounding box array based on individual meshes this.CalculateBoundingBox(); //Console.WriteLine(this.ModelName); //Console.WriteLine(this.MovingBoxes.Count); //default velocity as a unit vector...Remember: an object stays at rest until it is acted upon by another force! - Velocity already has this default. Defining it here is useless and problematic //Velocity = Vector3.Zero; //used so any object can have it's bounding box drawn...should be removed later if memory needs to be saved buffers = new BoundingBoxBuffers[this.MovingBoxes.Count]; for (int cntr = 0; cntr < this.MovingBoxes.Count; cntr++) { buffers[cntr] = new BoundingBoxBuffers(this.MovingBoxes[cntr], device); } } }
private BoundingBoxBuffers CreateBoundingBoxBuffers(BoundingBox boundingBox, GraphicsDevice graphicsDevice) { BoundingBoxBuffers boundingBoxBuffers = new BoundingBoxBuffers(); boundingBoxBuffers.PrimitiveCount = 24; boundingBoxBuffers.VertexCount = 48; VertexBuffer vertexBuffer = new VertexBuffer(graphicsDevice, typeof(VertexPositionColor), boundingBoxBuffers.VertexCount, BufferUsage.WriteOnly); List<VertexPositionColor> vertices = new List<VertexPositionColor>(); const float ratio = 5.0f; Vector3 xOffset = new Vector3((boundingBox.Max.X - boundingBox.Min.X) / ratio, 0, 0); Vector3 yOffset = new Vector3(0, (boundingBox.Max.Y - boundingBox.Min.Y) / ratio, 0); Vector3 zOffset = new Vector3(0, 0, (boundingBox.Max.Z - boundingBox.Min.Z) / ratio); Vector3[] corners = boundingBox.GetCorners(); // Corner 1. AddVertex(vertices, corners[0]); AddVertex(vertices, corners[0] + xOffset); AddVertex(vertices, corners[0]); AddVertex(vertices, corners[0] - yOffset); AddVertex(vertices, corners[0]); AddVertex(vertices, corners[0] - zOffset); // Corner 2. AddVertex(vertices, corners[1]); AddVertex(vertices, corners[1] - xOffset); AddVertex(vertices, corners[1]); AddVertex(vertices, corners[1] - yOffset); AddVertex(vertices, corners[1]); AddVertex(vertices, corners[1] - zOffset); // Corner 3. AddVertex(vertices, corners[2]); AddVertex(vertices, corners[2] - xOffset); AddVertex(vertices, corners[2]); AddVertex(vertices, corners[2] + yOffset); AddVertex(vertices, corners[2]); AddVertex(vertices, corners[2] - zOffset); // Corner 4. AddVertex(vertices, corners[3]); AddVertex(vertices, corners[3] + xOffset); AddVertex(vertices, corners[3]); AddVertex(vertices, corners[3] + yOffset); AddVertex(vertices, corners[3]); AddVertex(vertices, corners[3] - zOffset); // Corner 5. AddVertex(vertices, corners[4]); AddVertex(vertices, corners[4] + xOffset); AddVertex(vertices, corners[4]); AddVertex(vertices, corners[4] - yOffset); AddVertex(vertices, corners[4]); AddVertex(vertices, corners[4] + zOffset); // Corner 6. AddVertex(vertices, corners[5]); AddVertex(vertices, corners[5] - xOffset); AddVertex(vertices, corners[5]); AddVertex(vertices, corners[5] - yOffset); AddVertex(vertices, corners[5]); AddVertex(vertices, corners[5] + zOffset); // Corner 7. AddVertex(vertices, corners[6]); AddVertex(vertices, corners[6] - xOffset); AddVertex(vertices, corners[6]); AddVertex(vertices, corners[6] + yOffset); AddVertex(vertices, corners[6]); AddVertex(vertices, corners[6] + zOffset); // Corner 8. AddVertex(vertices, corners[7]); AddVertex(vertices, corners[7] + xOffset); AddVertex(vertices, corners[7]); AddVertex(vertices, corners[7] + yOffset); AddVertex(vertices, corners[7]); AddVertex(vertices, corners[7] + zOffset); vertexBuffer.SetData(vertices.ToArray()); boundingBoxBuffers.Vertices = vertexBuffer; IndexBuffer indexBuffer = new IndexBuffer(graphicsDevice, IndexElementSize.SixteenBits, boundingBoxBuffers.VertexCount, BufferUsage.WriteOnly); indexBuffer.SetData(Enumerable.Range(0, boundingBoxBuffers.VertexCount).Select(i => (short)i).ToArray()); boundingBoxBuffers.Indices = indexBuffer; return boundingBoxBuffers; }
//defaulted moveable object, no velocity //overload that instantiates the BoundingBox; MUST USE COLLISION PROCESSOR public ScreenModel(Game1 GameRefArg, Model SentModel, Vector3 ModelPosition) { GameRef = GameRefArg; Position = ModelPosition; MyModel = SentModel; //Gets the object type attribute from the List<object> DataFromProcessor=(List<object>)MyModel.Tag; CollisionType = (ObjectTypes)DataFromProcessor[0]; //only allocates space when the bounding box must be checked; //otherwise, then use the sphere to check if (CollisionType == ObjectTypes.Box) { //gets box data from the content //Does not check for exception in this case, because this method should be used on models that go through the processor BoundingBox Temp = (BoundingBox)DataFromProcessor[1]; //adds the Position Vector to the Vectors that control the shape of the bounding box //-----> because Vectors aren't as "free floating" in programing as they are in math LocalBox = new BoundingBox(Temp.Min + Position, Temp.Max + Position); //allocates current data from the default spot...but these values will change based on a direction/velocity vector MinVect = LocalBox.Min; MaxVect = LocalBox.Max; MovingBox = new BoundingBox(MinVect, MaxVect); } //case for if this model is a room if (CollisionType == ObjectTypes.Room) { ListOfWalls = (List<Plane>)DataFromProcessor[1]; } //allocates the other related objects if the bool is set to true Moveable = true; //Can be picked up by the user PickUpable = true; //default velocity as a unit vector...Remember: an object stays at rest until it is acted upon by another force! Velocity = Vector3.Zero; //used so any object can have it's bounding box drawn...should be removed later if memory needs to be saved buffers = new BoundingBoxBuffers(this.MovingBox, GameRef); }
//overload that instantiates the BoundingBox; MUST USE COLLISION PROCESSOR public ScreenModel(Game1 GameRefArg, Model SentModel, Vector3 ModelPosition, bool MoveableTrue, bool PickUpTrue) { GameRef = GameRefArg; Position = ModelPosition; MyModel = SentModel; //aspect ratio comes from the game's aspect ratio value //aspectRatio = GameRef.MyPerson.aspectRatio; //gets box data from the content //Does not check for exception in this case, because this method should be used on models that go through the processor List<object> DataFromProcessor = (List<object>)MyModel.Tag; CollisionType = (ObjectTypes)DataFromProcessor[0]; if (CollisionType == ObjectTypes.Box) { BoundingBox Temp = (BoundingBox)DataFromProcessor[1]; //adds the Position Vector to the Vectors that control the shape of the bounding box //-----> because Vectors aren't as "free floating" in programing as they are in math LocalBox = new BoundingBox(Temp.Min + Position, Temp.Max + Position); //allocates current data from the default spot...but these values will change based on a direction/velocity vector MinVect = LocalBox.Min; MaxVect = LocalBox.Max; MovingBox = new BoundingBox(MinVect, MaxVect); } //for room collisions if (CollisionType == ObjectTypes.Room) { ListOfWalls = (List<Plane>)DataFromProcessor[1]; } //allocates the other related objects if the bool is set to true Moveable = MoveableTrue; PickUpable = PickUpTrue; if (Moveable) { //default velocity as a unit vector...Remember: an object stays at rest until it is acted upon by another force! Velocity = Vector3.Zero; } //safely defaults the value of pickupable to false if the object is not moveable, regaurdless of the method input if(!Moveable) { PickUpable = false; } //used so any object can have it's bounding box drawn...should be removed later if memory needs to be saved buffers = new BoundingBoxBuffers(this.MovingBox, GameRef); }
public static BoundingBoxBuffers CreateBoundingBoxBuffers(BoundingBox boundingBox, GraphicsDevice graphicsDevice) { BoundingBoxBuffers boundingBoxBuffers = new BoundingBoxBuffers(); boundingBoxBuffers.PrimitiveCount = 24; boundingBoxBuffers.VertexCount = 48; VertexBuffer vertexBuffer = new VertexBuffer(graphicsDevice, typeof(VertexPositionColor), boundingBoxBuffers.VertexCount, BufferUsage.WriteOnly); List <VertexPositionColor> vertices = new List <VertexPositionColor>(); const float ratio = 5.0f; Vector3 xOffset = new Vector3((boundingBox.Max.X - boundingBox.Min.X) / ratio, 0, 0); Vector3 yOffset = new Vector3(0, (boundingBox.Max.Y - boundingBox.Min.Y) / ratio, 0); Vector3 zOffset = new Vector3(0, 0, (boundingBox.Max.Z - boundingBox.Min.Z) / ratio); Vector3[] corners = boundingBox.GetCorners(); // Corner 1. AddVertex(vertices, corners[0]); AddVertex(vertices, corners[0] + xOffset); AddVertex(vertices, corners[0]); AddVertex(vertices, corners[0] - yOffset); AddVertex(vertices, corners[0]); AddVertex(vertices, corners[0] - zOffset); // Corner 2. AddVertex(vertices, corners[1]); AddVertex(vertices, corners[1] - xOffset); AddVertex(vertices, corners[1]); AddVertex(vertices, corners[1] - yOffset); AddVertex(vertices, corners[1]); AddVertex(vertices, corners[1] - zOffset); // Corner 3. AddVertex(vertices, corners[2]); AddVertex(vertices, corners[2] - xOffset); AddVertex(vertices, corners[2]); AddVertex(vertices, corners[2] + yOffset); AddVertex(vertices, corners[2]); AddVertex(vertices, corners[2] - zOffset); // Corner 4. AddVertex(vertices, corners[3]); AddVertex(vertices, corners[3] + xOffset); AddVertex(vertices, corners[3]); AddVertex(vertices, corners[3] + yOffset); AddVertex(vertices, corners[3]); AddVertex(vertices, corners[3] - zOffset); // Corner 5. AddVertex(vertices, corners[4]); AddVertex(vertices, corners[4] + xOffset); AddVertex(vertices, corners[4]); AddVertex(vertices, corners[4] - yOffset); AddVertex(vertices, corners[4]); AddVertex(vertices, corners[4] + zOffset); // Corner 6. AddVertex(vertices, corners[5]); AddVertex(vertices, corners[5] - xOffset); AddVertex(vertices, corners[5]); AddVertex(vertices, corners[5] - yOffset); AddVertex(vertices, corners[5]); AddVertex(vertices, corners[5] + zOffset); // Corner 7. AddVertex(vertices, corners[6]); AddVertex(vertices, corners[6] - xOffset); AddVertex(vertices, corners[6]); AddVertex(vertices, corners[6] + yOffset); AddVertex(vertices, corners[6]); AddVertex(vertices, corners[6] + zOffset); // Corner 8. AddVertex(vertices, corners[7]); AddVertex(vertices, corners[7] + xOffset); AddVertex(vertices, corners[7]); AddVertex(vertices, corners[7] + yOffset); AddVertex(vertices, corners[7]); AddVertex(vertices, corners[7] + zOffset); vertexBuffer.SetData(vertices.ToArray()); boundingBoxBuffers.Vertices = vertexBuffer; IndexBuffer indexBuffer = new IndexBuffer(graphicsDevice, IndexElementSize.SixteenBits, boundingBoxBuffers.VertexCount, BufferUsage.WriteOnly); indexBuffer.SetData(Enumerable.Range(0, boundingBoxBuffers.VertexCount).Select(i => (short)i).ToArray()); boundingBoxBuffers.Indices = indexBuffer; return(boundingBoxBuffers); }