public override void Fire() { Init(); //be sure to leave this here to enable use of the na variable double[] rotation = { -Math.PI / 2, -Math.PI / 24, 0, Math.PI / 24, Math.PI / 2 }; float direction = 0; for (int i = 0; i < na.Width; i++) { if (na.GetNeuronAt(i, 0).LastCharge > 0.9 || na.GetNeuronAt(i, 0).LastCharge > 0.9 && i < rotation.Length) { direction = (float)rotation[i]; } } if (na.GetNeuronAt(2, 0).CurrentCharge != 0) { direction = na.GetNeuronAt(2, 0).CurrentCharge; na.GetNeuronAt(2, 0).SetValue(0); } ModuleRealityModel mrm = (ModuleRealityModel)FindModuleByType(typeof(ModuleRealityModel)); if (mrm != null && direction != 0) { mrm.Rotate(20); } Module2DSim m2D = (Module2DSim)FindModuleByType(typeof(Module2DSim)); if (m2D != null && direction != 0) { m2D.Rotate(direction); } Module3DSim m3D = (Module3DSim)FindModuleByType(typeof(Module3DSim)); if (m3D != null && direction != 0) { m3D.Rotate(direction); } Module2DModel m2DModel = (Module2DModel)FindModuleByType(typeof(Module2DModel)); if (m2DModel != null && direction != 0) { m2DModel.Rotate(direction); } Module2DVision m2DVision = (Module2DVision)FindModuleByType(typeof(Module2DVision)); if (m2DVision != null && direction != 0) { m2DVision.ViewChanged(); } }
//do ray tracing to create the view the Entitiy would see private void HandleVision(int row) { int retinaWidth = GetModuleWidth("Module2DVision"); if (row == 0) { currentView0.Clear(); } else { currentView1.Clear(); } int[] pixels = new int[retinaWidth]; for (int i = 0; i < retinaWidth; i++) { double theta = Module2DVision.GetDirectionFromNeuron(i, retinaWidth); theta = entityDirection1 + theta; //create a segment from the view direction for this pixel (length 100 assumes the size of the universe) Point p2 = entityPosition + new Vector(Cos(theta) * 100.0, Sin(theta) * 100.0); Color theColor = Colors.Pink; double closestDistance = 20; for (int j = 0; j < objects.Count; j++) { Utils.FindIntersection(entityPosition, p2, objects[j].P1, objects[j].P2, out bool lines_intersect, out bool segments_intersect, out Point intersection, out Point close_p1, out Point closep2, out double collisionAngle); if (segments_intersect) { double distance = Point.Subtract(intersection, entityPosition).Length; if (distance < closestDistance) { closestDistance = distance; theColor = objects[j].theColor; if (theColor != Colors.Black && false) //TODO texture is commented out { //we can give the line some line texture PointPlus P1 = new PointPlus(objects[j].P1); PointPlus P2 = new PointPlus(objects[j].P2); PointPlus pIntersection = new PointPlus(intersection); PointPlus delta = P2 - P1; delta.R = .1f; Segment s = new Segment(P1, P2, objects[j].theColor); //this is slow as a snail but exactly matches the display code for (int k = 1; k < 1 + s.Length * 10; k += 2) { PointPlus PStart = new PointPlus((Point)(P1.V + k * delta.V)); PointPlus PEnd = new PointPlus((Point)(P1.V + (k + .5f) * delta.V)); if (pIntersection.Theta >= Min(PStart.Theta, PEnd.Theta) && pIntersection.Theta <= Max(PStart.Theta, PEnd.Theta)) { theColor = Colors.AliceBlue; } } } } } } pixels[i] = Utils.ColorToInt(theColor); Point p3 = entityPosition + new Vector(Cos(theta), Sin(theta)); if (row == 0) { currentView0.Add(new physObject() { P1 = p3, P2 = entityPosition, theColor = theColor }); } //currentView0.Add(new physObject() { P1 = p3, P2 = new Point(0, 0), theColor = theColor }); else { currentView1.Add(new physObject() { P1 = p3, P2 = entityPosition, theColor = theColor }); } } SetNeuronVector("Module2DVision", true, row, pixels); }
public override void Fire() { Init(); //be sure to leave this here to enable use of the na variable float[] dist = { .5f, .1f, 0, -.025f, -.1f }; float motionX = 0; float motionY = 0; if (na.Width < 3) { Initialize(); } for (int i = 0; i < na.Height; i++) { if (na.GetNeuronAt(1, i).LastCharge > 0.9) { motionX = dist[i]; } } if (na.GetNeuronAt(1, 2).CurrentCharge != 0) { motionX = na.GetNeuronAt(1, 2).CurrentCharge; na.GetNeuronAt(1, 2).SetValue(0); } if (na.GetNeuronAt(0, 2).LastCharge > 0.9) { motionY = 0.5f; } if (na.GetNeuronAt(2, 2).LastCharge > 0.9) { motionY = -0.5f; } //obsolete ModuleRealityModel mrm = (ModuleRealityModel)FindModuleByType(typeof(ModuleRealityModel)); if (mrm != null) { mrm.Move(motionX); } Module3DSim m3D = (Module3DSim)FindModuleByType(typeof(Module3DSim)); if (m3D != null && motionX != 0) { m3D.Move(motionX); } bool moved = false; Module2DSim m2D = (Module2DSim)FindModuleByType(typeof(Module2DSim)); if (m2D != null && motionX != 0 || motionY != 0) { moved = m2D.Move(motionX, motionY); } Module2DModel m2DModel = (Module2DModel)FindModuleByType(typeof(Module2DModel)); if (m2DModel != null && moved && motionX != 0 || motionY != 0) { m2DModel.Move(motionX, motionY); } Module2DVision m2DVision = (Module2DVision)FindModuleByType(typeof(Module2DVision)); if (m2DVision != null && motionX != 0) { m2DVision.ViewChanged(); } }