Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. public BoundingBox BuildBoundingBox()
  2. {
  3. // Create initial variables to hold min and max xyz values for the mesh
  4. Vector3 meshMax = new Vector3(float.MinValue);
  5. Vector3 meshMin = new Vector3(float.MaxValue);
  6. foreach (ModelMesh mesh in model.Meshes)
  7. {
  8. foreach (ModelMeshPart part in mesh.MeshParts)
  9. {
  10. // The stride is how big, in bytes, one vertex is in the vertex buffer
  11. // We have to use this as we do not know the make up of the vertex
  12. int stride = part.VertexBuffer.VertexDeclaration.VertexStride;
  13.  
  14. VertexPositionNormalTexture[] vertexData = new VertexPositionNormalTexture[part.NumVertices];
  15. part.VertexBuffer.GetData(part.VertexOffset * stride, vertexData, 0, part.NumVertices, stride);
  16.  
  17. // Find minimum and maximum xyz values for this mesh part
  18. Vector3 vertPosition = new Vector3();
  19.  
  20. for (int i = 0; i < vertexData.Length; i++)
  21. {
  22. vertPosition = vertexData[i].Position;
  23.  
  24. // update our values from this vertex
  25. meshMin = Vector3.Min(meshMin, vertPosition);
  26. meshMax = Vector3.Max(meshMax, vertPosition);
  27. }
  28. }
  29.  
  30. // transform by mesh bone matrix
  31. meshMin = Vector3.Transform(meshMin, meshTransform);
  32. meshMax = Vector3.Transform(meshMax, meshTransform);
  33. }
  34. // Create the bounding box
  35. BoundingBox box1 = new BoundingBox(meshMin, meshMax);
  36. return box1;
  37.  
  38. }
  39.  
  40. class MyGame{
  41. BoundingBox theBox = null;
  42.  
  43. public MyGame(){
  44. theBox = BuildBoundingBox();
  45. }
  46.  
  47. public bool Update(){
  48. //Do stuff with theBox here:
  49. theBox.intersects(someOtherBox);
  50. }
  51. }
  52.  
  53. public class Entity
  54. {
  55. private Vector3 boundsSize;
  56. private Vector3 boundsOrigin;
  57. private Vector3 position;
  58. private BoundingBox bounds;
  59.  
  60. public Vector3 Position
  61. {
  62. get
  63. {
  64. return this.position;
  65. }
  66. }
  67. public BoundingBox Bounds
  68. {
  69. get
  70. {
  71. return this.bounds;
  72. }
  73. }
  74.  
  75. public Entity(Vector3 pBoundsSize, Vector3 pBoundsOrigin)
  76. {
  77. this.boundsSize = pBoundsSize;
  78. this.boundsOrigin = pBoundsOrigin;
  79. this.SetPosition(new Vector(0, 0, 0));
  80. }
  81.  
  82. public void SetPosition(Vector3 pPosition)
  83. {
  84. this.position = pPosition;
  85. this.bounds = New BoundingBox(
  86. new Vector3(
  87. this.potition.X - this.boundsOrigin.X,
  88. this.potition.Y - this.boundsOrigin.Y,
  89. this.potition.Z - this.boundsOrigin.Z),
  90. new Vector3(
  91. this.potition.X - this.boundsOrigin.X + this.boundsSize.X,
  92. this.potition.Y - this.boundsOrigin.Y + this.boundsSize.Y,
  93. this.potition.Z - this.boundsOrigin.Z + this.boundsSize.Z),
  94. }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement