Esempio n. 1
0
        //------------------------------------------------------------------------------------------------------------------------
        //Function: calculatePhysicsLinesForRectangle
        //Parameters:objectInWorld
        //This function calculates the boundaries of the rectangle by calculating the position of the four corners, then calculating
        //the physics lines between each adjacent corner to create the box.
        //------------------------------------------------------------------------------------------------------------------------
        public void calculatePhysicsLinesForRectangle(objectInWorld myObj)
        {
            double startX = Canvas.GetLeft(myObj.rectangle);
            double startY = Canvas.GetTop(myObj.rectangle);
            double height = myObj.rectangle.Height;
            double width = myObj.rectangle.Width;
            double angle = myObj.angle;

            double centerX = startX + (width / 2);
            double centerY = startY + (height / 2);

            double[] x = new double[4];
            double[] y = new double[4];

            // Rectangle:
            // x0,y0    x1,y1
            // x2,y2    x3,y3

            double angleInRadians = Math.PI / 180.0 * angle;

            // Calculate where the corners of the robot are
            x[0] = (startX - centerX) * Math.Cos(angleInRadians) - (startY - centerY) * Math.Sin(angleInRadians) + centerX;
            x[1] = (startX + width - centerX) * Math.Cos(angleInRadians) - (startY - centerY) * Math.Sin(angleInRadians) + centerX;
            x[2] = (startX - centerX) * Math.Cos(angleInRadians) - (startY + height - centerY) * Math.Sin(angleInRadians) + centerX;
            x[3] = (startX + width - centerX) * Math.Cos(angleInRadians) - (startY + height - centerY) * Math.Sin(angleInRadians) + centerX;

            y[0] = (startX - centerX) * Math.Sin(angleInRadians) + (startY - centerY) * Math.Cos(angleInRadians) + centerY;
            y[1] = (startX + width - centerX) * Math.Sin(angleInRadians) + (startY - centerY) * Math.Cos(angleInRadians) + centerY;
            y[2] = (startX - centerX) * Math.Sin(angleInRadians) + (startY + height - centerY) * Math.Cos(angleInRadians) + centerY;
            y[3] = (startX + width - centerX) * Math.Sin(angleInRadians) + (startY + height - centerY) * Math.Cos(angleInRadians) + centerY;

            // These points are to be used in calculating the physics for the world.
            calculatePhysicsLineForTwoPoints(x[0], y[0], x[1], y[1]);
            calculatePhysicsLineForTwoPoints(x[0], y[0], x[2], y[2]);
            calculatePhysicsLineForTwoPoints(x[2], y[2], x[3], y[3]);
            calculatePhysicsLineForTwoPoints(x[1], y[1], x[3], y[3]);
        }
Esempio n. 2
0
        //------------------------------------------------------------------------------------------------------------------------
        //Function: RobotSimulator (constructor)
        //Sets up the simulation, setting up the framerate, web service communication (between robot and simulation), and simulation
        //UI.
        //------------------------------------------------------------------------------------------------------------------------
        public RobotSimulator()
        {
            InitializeComponent();
            // Set the framerate to 30 FPS.
            Application.Current.Host.Settings.MaxFrameRate = 30;

            // Connect to and set up the web service.
            robotService = new ServiceClient();
            robotService.getMotionCompleted += new EventHandler<getMotionCompletedEventArgs>(robotService_getMotionCompleted);
            robotService.getMotionAsync();

            /*broker = new brokerService.Service1SoapClient();
            broker.GetAddressCompleted += new EventHandler<GetAddressCompletedEventArgs>(broker_getAddressCompleted);
            broker.GetAddressAsync(0);*/

            // Create a box around the entire boundary of the simulator area
            Rectangle worldBoundary = new Rectangle();
            worldBoundary.Width = simWorld.getWorldSize() - 2;
            worldBoundary.Height = simWorld.getWorldSize() - 2;
            SolidColorBrush lightGray = new SolidColorBrush(Colors.LightGray);
            worldBoundary.Fill = lightGray;
            worldBoundary.Margin = new Thickness(0, 0, 0, 0);
            worldBoundary.HorizontalAlignment = HorizontalAlignment.Left;
            worldBoundary.VerticalAlignment = VerticalAlignment.Top;
            objectInWorld worldBoundaryObject = new objectInWorld(worldBoundary, 0.0);
            worldObjects.Add(worldBoundaryObject);

            // Calculate all of the physics-related variables for the entire simulated world.
            simWorld.populateWorld(LayoutRoot, worldObjects);  // Now just for the world boundary

            // Start a thread that checks for updates from the web service and sends updates (from the sensors) to the web service.
            runner = new Thread(robotUpdater);
            runner.Start();

            // This function initializes the contents of the maze and other pieces in the simulator.
            simWorld.defaultTestSetup(LayoutRoot, theRobot, ref robotRotation);

            // Populate the list of the combobox.
            populateComboBox(defaultActionComboBox, defaultOptions);
            defaultActionComboBox.SelectedIndex = 0;    // By default, the default action is to move forward.
        }