/** * Executes methods for certain objects in the scene specified below * @param m * @param l */ private static void ReflectionCallback(Model m, LaserModel l) { if (m is LaserStop) { ((LaserStop)m).LaserIntersection(); } }
/** * Calculates the path of travel of the laser and sets the laser to such a * path * * @param laser * @param models */ public static object[] Reflect(LaserModel laser) { FindIntersects(laser, GameInstance.objectManager.GetModels()); // If there exists at least one valid intersection if (intersects.Count != 0) { closest = GetClosestIntersection(); // Find the closest one // Pythagorean theorem to find length of vector float length = (float)MathExtension.Hypotenuse((float)closest[1] - coords[0], (float)closest[2] - coords[1]); laser.SetLength(length); // Modify the laser to the correct length // Calculates reflected vector using the laser's vector and a new vector representing the side of the Model Vector2d resultantV = ReflectionVector(laser.vect, new Vector2d(10d, 10d * (float)closest[3])); ReflectionCallback((Model)closest[0], laser); return(new Object[] { CreateModel.CreateReflectedLaser((float)closest[1], (float)closest[2], resultantV), closest[0] }); } return(null); }
public override IView <LaserModel> CreateLaser(LaserModel model) { var gameObject = _context.ViewMode == ViewMode.Polygonal ? Object.Instantiate(_context.LaserDataSetter.polygonalPrefab) : Object.Instantiate(_context.LaserDataSetter.spritePrefab); gameObject.name = "Laser" + _context.ViewMode; var view = gameObject.AddComponent <LaserView>(); view.ViewMode = _context.ViewMode; view.Context = _context; view.Model = model; return(view); }
/* * generate json object of lab instacne */ public string GenerateJSON() { // create instances of objecct models LaserModel lm = new LaserModel(laser.transform.position, laser.transform.rotation); RefractableMaterialModel worldRM = new RefractableMaterialModel(rm.GetPresetIndex(), rm.GetN(), rm.transform.position, rm.transform.rotation); RefractableMaterials serializeRMs = new RefractableMaterials(); foreach (RefractableMaterial rm in rms) { serializeRMs.materials.Add(new RefractableMaterialModel(rm.GetPresetIndex(), rm.GetN(), rm.transform.position, rm.transform.rotation)); } LabInstance li = new LabInstance(); li.lm = lm; li.worldRM = worldRM; li.refractableMaterials = serializeRMs; // convert lab instance model object to json string return(JsonUtility.ToJson(li)); }
private LaserModel root; // Root node in the laser list /** * New LaserWrapper with the starting laser init * @param init */ public LaserWrapper(LaserModel init) { laserList.AddLast(init); root = init; }
private static void FindIntersects(LaserModel laser, List <Model> models) { intersects.Clear(); // Remove existing intersects from the list intersects.TrimExcess(); float slope = (float)System.Math.Tan(laser.GetAngle()); coords = laser.GetCoords(); int xDir = laser.xDir; int yDir = laser.yDir; if (slope < .01f && slope > 0) { slope = .01f; } else if (slope > -.01f && slope < 0) { slope = -.01f; } // For all Models in the scene foreach (Model m in models) { // Don't intersect with lasers or UI Elements if (!(m is LaserModel) && !(m is UIElement)) { for (int side = 0; side < m.sideCount; side++) { v = GetY1X1(m, side); sl = GetSlope(v); // If the laser is vertical if (slope == float.PositiveInfinity || slope == float.NegativeInfinity) { // If both are vertical if (sl == float.PositiveInfinity || sl == float.NegativeInfinity) { xIntercept = 100f; // They will never intercept yIntercept = 100f; } else { // Use separate math for vertical laser and non-vertical edge yIntercept = sl * (coords[0] - v[0]) + v[1]; xIntercept = coords[0]; } } else if (sl < float.PositiveInfinity && sl > float.NegativeInfinity) { // Make sure the line isn't vertical if unsure // Calculate intersection points of the two lines xIntercept = (-sl * v[0] + slope * coords[0] + v[1] - coords[1]) / (slope - sl); yIntercept = sl * (xIntercept - v[0]) + v[1]; } else { xIntercept = v[0]; // If vertical, use easier methods for finding intersections yIntercept = slope * (xIntercept - coords[0]) + coords[1]; } // Check if the point of intersection is in the correct direction if (((xIntercept - coords[0]) * xDir >= 0) && ((yIntercept - coords[1]) * yDir >= 0)) { // Check if the point lies on a side of the polygon if (((xIntercept <= v[0]) && (xIntercept >= v[2])) || ((xIntercept >= v[0]) && (xIntercept <= v[2]))) { //TODO: Check if this works for all polygons (it might, not sure) if (((yIntercept <= v[1]) && (yIntercept >= v[3])) || ((yIntercept >= v[1]) && (yIntercept <= v[3]))) { intersects.Add(new object[] { m, xIntercept, yIntercept, sl }); } } } } } } }
public abstract IView <LaserModel> CreateLaser(LaserModel model);