Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public BoundingBox BuildBoundingBox()
- {
- // Create initial variables to hold min and max xyz values for the mesh
- Vector3 meshMax = new Vector3(float.MinValue);
- Vector3 meshMin = new Vector3(float.MaxValue);
- foreach (ModelMesh mesh in model.Meshes)
- {
- foreach (ModelMeshPart part in mesh.MeshParts)
- {
- // The stride is how big, in bytes, one vertex is in the vertex buffer
- // We have to use this as we do not know the make up of the vertex
- int stride = part.VertexBuffer.VertexDeclaration.VertexStride;
- VertexPositionNormalTexture[] vertexData = new VertexPositionNormalTexture[part.NumVertices];
- part.VertexBuffer.GetData(part.VertexOffset * stride, vertexData, 0, part.NumVertices, stride);
- // Find minimum and maximum xyz values for this mesh part
- Vector3 vertPosition = new Vector3();
- for (int i = 0; i < vertexData.Length; i++)
- {
- vertPosition = vertexData[i].Position;
- // update our values from this vertex
- meshMin = Vector3.Min(meshMin, vertPosition);
- meshMax = Vector3.Max(meshMax, vertPosition);
- }
- }
- // transform by mesh bone matrix
- meshMin = Vector3.Transform(meshMin, meshTransform);
- meshMax = Vector3.Transform(meshMax, meshTransform);
- }
- // Create the bounding box
- BoundingBox box1 = new BoundingBox(meshMin, meshMax);
- return box1;
- }
- class MyGame{
- BoundingBox theBox = null;
- public MyGame(){
- theBox = BuildBoundingBox();
- }
- public bool Update(){
- //Do stuff with theBox here:
- theBox.intersects(someOtherBox);
- }
- }
- public class Entity
- {
- private Vector3 boundsSize;
- private Vector3 boundsOrigin;
- private Vector3 position;
- private BoundingBox bounds;
- public Vector3 Position
- {
- get
- {
- return this.position;
- }
- }
- public BoundingBox Bounds
- {
- get
- {
- return this.bounds;
- }
- }
- public Entity(Vector3 pBoundsSize, Vector3 pBoundsOrigin)
- {
- this.boundsSize = pBoundsSize;
- this.boundsOrigin = pBoundsOrigin;
- this.SetPosition(new Vector(0, 0, 0));
- }
- public void SetPosition(Vector3 pPosition)
- {
- this.position = pPosition;
- this.bounds = New BoundingBox(
- new Vector3(
- this.potition.X - this.boundsOrigin.X,
- this.potition.Y - this.boundsOrigin.Y,
- this.potition.Z - this.boundsOrigin.Z),
- new Vector3(
- this.potition.X - this.boundsOrigin.X + this.boundsSize.X,
- this.potition.Y - this.boundsOrigin.Y + this.boundsSize.Y,
- this.potition.Z - this.boundsOrigin.Z + this.boundsSize.Z),
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement