public void Touch(Ray ray) { foreach(Component comp in components) { BoundingBox curBox = new BoundingBox(comp.Box); curBox.Center = Vector3.Transform(curBox.Center, this.Transform); curBox.Scale(this.scale); Vector3? intersection = ray.GetIntersectionPoint(curBox); if(intersection != null) { React(); return; } } }
public Field(int dimX, int dimZ, string meshName, string materialName) { this.DimX = dimX; this.DimZ = dimZ; buffer1 = new float[DimX * DimZ]; buffer2 = new float[DimX * DimZ]; Mesh mesh = MeshLoader.GetMesh(meshName); BoundingBox box = new BoundingBox(mesh); for(int i = 0; i < dimX; i++) for(int j = 0; j < dimZ; j++) { Component comp = new Component(meshName, new BoundingBox(box)); extents = box.HalfSize * 2.0f; comp.Position = new Vector3(i * extents.X, 0.0f, j * extents.Z); AddComponent(comp); } AddMaterial(MaterialLoader.GetMaterial(materialName)); }
public BoundingBox(BoundingBox box) { this.min = box.Min; this.max = box.Max; }
public float? GetIntersectionParam(BoundingBox box) { //first test if start in box if (Origin.X >= box.Min.X && Origin.X <= box.Max.X && Origin.Y >= box.Min.Y && Origin.Y <= box.Max.Y && Origin.Z >= box.Min.Z && Origin.Z <= box.Max.Z) return 0.0f;// here we concidere cube is full and origine is in cube so intersect at origine //Second we check each face Vector3 maxT = new Vector3(-1.0f, -1.0f, -1.0f); //Vector3 minT = new Vector3(-1.0f); //calcul intersection with each faces if (Origin.X < box.Min.X && Direction.X != 0.0f) maxT.X = (box.Min.X - Origin.X) / Direction.X; else if (Origin.X > box.Max.X && Direction.X != 0.0f) maxT.X = (box.Max.X - Origin.X) / Direction.X; if (Origin.Y < box.Min.Y && Direction.Y != 0.0f) maxT.Y = (box.Min.Y - Origin.Y) / Direction.Y; else if (Origin.Y > box.Max.Y && Direction.Y != 0.0f) maxT.Y = (box.Max.Y - Origin.Y) / Direction.Y; if (Origin.Z < box.Min.Z && Direction.Z != 0.0f) maxT.Z = (box.Min.Z - Origin.Z) / Direction.Z; else if (Origin.Z > box.Max.Z && Direction.Z != 0.0f) maxT.Z = (box.Max.Z - Origin.Z) / Direction.Z; //get the maximum maxT if (maxT.X > maxT.Y && maxT.X > maxT.Z) { if (maxT.X < 0.0f) return null;// ray go on opposite of face //coordonate of hit point of face of cube float coord = Origin.Z + maxT.X * Direction.Z; // if hit point coord ( intersect face with ray) is out of other plane coord it miss if (coord < box.Min.Z || coord > box.Max.Z) return null; coord = Origin.Y + maxT.X * Direction.Y; if (coord < box.Min.Y || coord > box.Max.Y) return null; return maxT.X; } if (maxT.Y > maxT.X && maxT.Y > maxT.Z) { if (maxT.Y < 0.0f) return null;// ray go on opposite of face //coordonate of hit point of face of cube float coord = Origin.Z + maxT.Y * Direction.Z; // if hit point coord ( intersect face with ray) is out of other plane coord it miss if (coord < box.Min.Z || coord > box.Max.Z) return null; coord = Origin.X + maxT.Y * Direction.X; if (coord < box.Min.X || coord > box.Max.X) return null; return maxT.Y; } else //Z { if (maxT.Z < 0.0f) return null;// ray go on opposite of face //coordonate of hit point of face of cube float coord = Origin.X + maxT.Z * Direction.X; // if hit point coord ( intersect face with ray) is out of other plane coord it miss if (coord < box.Min.X || coord > box.Max.X) return null; coord = Origin.Y + maxT.Z * Direction.Y; if (coord < box.Min.Y || coord > box.Max.Y) return null; return maxT.Z; } }
public Vector3? GetIntersectionPoint(BoundingBox box) { float? t = GetIntersectionParam(box); if(t!=null) return Origin + Direction * (float)t; else return null; }
public Component(Mesh mesh, BoundingBox box) { Mesh = mesh; Box = box; }
public Component( Mesh mesh ) { Mesh = mesh; Box = new BoundingBox(mesh); }
public Component(string meshName, BoundingBox box) { Mesh = MeshLoader.GetMesh(meshName); Box = box; }
public Component(string meshName) { Mesh = MeshLoader.GetMesh(meshName); Box = new BoundingBox(Mesh); }