• Main Page
  • Packages
  • Classes
  • Files
  • File List

C:/Doug/CSharp/XnaCollisionLib/XnaCollisionLib/CollisionSource.cs

Go to the documentation of this file.
00001 using System;
00002 using System.Collections.Generic;
00003 using Microsoft.Xna.Framework;
00004 using Microsoft.Xna.Framework.Graphics;
00005 
00006 namespace XnaCollisionLib
00007 {
00008     public class CollisionSource 
00009     {
00010         private List<OctTree> _List;
00011         private int _Count;
00012 
00013         public CollisionSource()
00014         {
00015             _List = new List<OctTree>();
00016             _Count = 0;
00017         }
00018 
00019         public int Count
00020         {
00021             get { return _Count; }
00022         }
00023 
00024         public void Clear()
00025         {
00026             _List.Clear();
00027             _Count = 0;
00028         }
00029 
00030         public Nullable<float> ISect(Ray ray, out Triangle nearestTriangle)
00031         {
00032             Nullable<float> t = null;
00033             float nearestDistance = float.MaxValue;
00034 
00035             nearestTriangle = new Triangle();
00036             foreach (OctTree node in _List)
00037             {
00038                 Nullable<float> t2 = node.ISect(ray, ref nearestDistance, ref nearestTriangle);
00039 
00040                 if (t2.HasValue)
00041                     t = t2;
00042             }
00043             return t;
00044         }
00045 
00046         public void Query(BoundingBox box, List<Triangle> triangles, Matrix transform)
00047         {
00048             foreach (OctTree node in _List)
00049                 node.Query(box, triangles, transform);
00050         }
00051 
00052         public void Add(OctTree octTree)
00053         {
00054             _List.Add(octTree);
00055             _Count += octTree.Count;
00056         }
00057 
00058         public void Add(IEnumerable<Triangle> triangles, int minTrianglesPerNode)
00059         {
00060             Add(OctTree.Create(triangles, minTrianglesPerNode));
00061         }
00062 
00063         public void Add(Model model, int minTrianglesPerNode, Matrix transform)
00064         {
00065             Vector3[] points = model.Tag as Vector3[];
00066             Triangle[] triangles = new Triangle[points.Length / 3];
00067 
00068             for (int i = 0, j = 0; i != points.Length; j++)
00069             {
00070                 Vector3 a = Vector3.Transform(points[i++], transform);
00071                 Vector3 b = Vector3.Transform(points[i++], transform);
00072                 Vector3 c = Vector3.Transform(points[i++], transform);
00073 
00074                 triangles[j] = new Triangle(a, b, c);
00075             }
00076             Add(triangles, minTrianglesPerNode);
00077         }
00078     }
00079 }

Generated on Thu Jul 15 2010 19:56:39 for XnaCollisionLib by  doxygen 1.7.0