Exemplo n.º 1
0
        private void lblLargeMap_MouseUp(object sender, MouseButtonEventArgs e)
        {
            _isLargeMap = !_isLargeMap;

            // Come up with new values (and color the control)
            Point3D cameraPos        = new Point3D();
            double  mouseCursorScale = 1d;

            if (_isLargeMap)
            {
                lblLargeMap.Background = _toggleBrush;

                _boundryMin = new Vector3D(-WIDTH_LARGE, -WIDTH_LARGE, -DEPTH);
                _boundryMax = new Vector3D(WIDTH_LARGE, WIDTH_LARGE, DEPTH);
                cameraPos   = new Point3D(0, 0, CAMERAZ_LARGE);

                mouseCursorScale = MOUSECURSORLARGESCALE;
            }
            else
            {
                lblLargeMap.Background = Brushes.Transparent;

                _boundryMin = new Vector3D(-WIDTH_SMALL, -WIDTH_SMALL, -DEPTH);
                _boundryMax = new Vector3D(WIDTH_SMALL, WIDTH_SMALL, DEPTH);
                cameraPos   = new Point3D(0, 0, CAMERAZ_SMALL);
            }

            #region Change Boundry

            foreach (ScreenSpaceLines3D line in _lines)
            {
                _viewport.Children.Remove(line);
            }
            _lines.Clear();

            List <Point3D[]> innerLines, outerLines;
            _world.SetCollisionBoundry(out innerLines, out outerLines, _viewport, _boundryMin, _boundryMax);

            //TODO:  Make a private method that does this
            Color lineColor = Color.FromArgb(255, 200, 200, 180);
            foreach (Point3D[] line in innerLines)
            {
                // Need to wait until the window is loaded to call lineModel.CalculateGeometry
                ScreenSpaceLines3D lineModel = new ScreenSpaceLines3D(false);
                lineModel.Thickness = 1d;
                lineModel.Color     = lineColor;
                lineModel.AddLine(line[0], line[1]);

                _viewport.Children.Add(lineModel);
                _lines.Add(lineModel);
            }

            RecalculateLineGeometries();

            #endregion

            _mouseCursorGeometry.Transform = new ScaleTransform3D(mouseCursorScale, mouseCursorScale, mouseCursorScale);

            _camera.Position = cameraPos;
        }
Exemplo n.º 2
0
        private void SetCollisionBoundry()
        {
            // Remove existing lines from the viewport
            if (_lines.Count > 0)
            {
                foreach (ScreenSpaceLines3D line in _lines)
                {
                    _viewport.Children.Remove(line);
                }
            }

            // Tell the world about the collision boundry (it will clean itself up if this is a second call)
            var boundryLines = _world.SetCollisionBoundry(new Point3D(-100, -100, -100), new Point3D(100, 100, 100));

            // Draw the lines
            Color lineColor = Color.FromArgb(32, 0, 0, 0);

            foreach (var line in boundryLines.innerLines)
            {
                ScreenSpaceLines3D lineModel = new ScreenSpaceLines3D(false);
                lineModel.Thickness = 1d;
                lineModel.Color     = lineColor;
                lineModel.AddLine(line.from, line.to);

                _viewport.Children.Add(lineModel);
                _lines.Add(lineModel);
            }

            RecalculateLineGeometries();
        }
Exemplo n.º 3
0
        private static Visual3D GetShipCompassBlip(Bot ship)
        {
            ScreenSpaceLines3D retVal = new ScreenSpaceLines3D(true);

            retVal.Thickness = 2d;
            retVal.Color     = Colors.DodgerBlue;
            retVal.AddLine(new Point3D(0, 170, 20), new Point3D(0, 1000, 20));

            // Exit Function
            return(retVal);
        }
Exemplo n.º 4
0
        private void ShowLine(Point3D point1, Point3D point2, Color color)
        {
            ScreenSpaceLines3D line = new ScreenSpaceLines3D();

            line.Color     = color;
            line.Thickness = 1;
            line.AddLine(point1, point2);

            _debugVisuals.Add(line);
            _viewport.Children.Add(line);
        }
Exemplo n.º 5
0
        public static Visual3D GetGuideLineDouble(Axis axis, Color color)
        {
            Transform3D transform = GetTransform(axis, true);

            ScreenSpaceLines3D retVal = new ScreenSpaceLines3D(true);

            retVal.Thickness = 1d;
            retVal.Color     = color;
            retVal.AddLine(transform.Transform(new Point3D(-.75d, 0, 0)), transform.Transform(new Point3D(.75d, 0, 0)));

            return(retVal);
        }
Exemplo n.º 6
0
        public static Visual3D GetGuideLine(Axis axis, bool positiveDirection, Color color)
        {
            Transform3D transform = GetTransform(axis, positiveDirection);

            ScreenSpaceLines3D retVal = new ScreenSpaceLines3D(true);

            retVal.Thickness = 1d;
            retVal.Color     = color;
            retVal.AddLine(new Point3D(0, 0, 0), transform.Transform(new Point3D(.75d, 0, 0)));

            return(retVal);
        }
Exemplo n.º 7
0
        private ModelVisual3D GetShipCompassBlip(Ship ship)
        {
            ScreenSpaceLines3D retVal = new ScreenSpaceLines3D(true);

            retVal.Thickness = 2d;
            retVal.Color     = Colors.DodgerBlue;
            retVal.AddLine(new Point3D(0, 170, 20), new Point3D(0, 1000, 20));

            retVal.Transform = new TranslateTransform3D(ship.PositionWorld.ToVector());

            // Exit Function
            return(retVal);
        }
Exemplo n.º 8
0
        private Visual3D[] GetStrokeVisual(Tuple <Point3D, Vector3D>[] points)
        {
            Model3DGroup models = new Model3DGroup();

            MaterialGroup materials = new MaterialGroup();

            materials.Children.Add(new DiffuseMaterial(new SolidColorBrush(UtilityWPF.ColorFromHex("EEE"))));
            materials.Children.Add(new SpecularMaterial(new SolidColorBrush(UtilityWPF.ColorFromHex("FFF")), 4));

            ScreenSpaceLines3D lines = new ScreenSpaceLines3D();

            lines.Color     = UtilityWPF.ColorFromHex("EEE");
            lines.Thickness = 2;

            for (int cntr = 0; cntr < points.Length - 1; cntr++)
            {
                #region velocity line

                Point3D secondPoint = points[cntr].Item1;
                if (points[cntr].Item2.IsNearZero())
                {
                    secondPoint += Math3D.GetRandomVector_Spherical(.02);
                }
                else
                {
                    secondPoint += points[cntr].Item2 * .75;
                }

                lines.AddLine(points[cntr].Item1, secondPoint);

                #endregion
                #region dot

                GeometryModel3D geometry = new GeometryModel3D();
                geometry.Material     = materials;
                geometry.BackMaterial = materials;
                geometry.Geometry     = UtilityWPF.GetSphere_Ico(.15, 1, true);

                geometry.Transform = new TranslateTransform3D(points[cntr].Item1.ToVector());

                models.Children.Add(geometry);

                #endregion
            }

            ModelVisual3D dotVisual = new ModelVisual3D();
            dotVisual.Content = models;

            return(new Visual3D[] { lines, dotVisual });
        }
Exemplo n.º 9
0
        private Visual3D GetStrokeVisual_PATH(Tuple <Point3D, Vector3D>[] points)
        {
            //TODO: Draw the first points larger, then taper off.  May want a bezier with the velocities as control points

            ScreenSpaceLines3D retVal = new ScreenSpaceLines3D();

            retVal.Color     = UtilityWPF.ColorFromHex("CCC");
            retVal.Thickness = 2;

            if (points.Length == 1)
            {
                #region single point

                Point3D secondPoint = points[0].Item1;
                if (points[0].Item2.IsNearZero())
                {
                    secondPoint += Math3D.GetRandomVector_Spherical(.02);
                }
                else
                {
                    secondPoint += points[0].Item2;
                }

                retVal.AddLine(points[0].Item1, secondPoint);

                #endregion
            }
            else
            {
                for (int cntr = 0; cntr < points.Length - 1; cntr++)
                {
                    retVal.AddLine(points[cntr].Item1, points[cntr + 1].Item1);
                }
            }

            return(retVal);
        }
Exemplo n.º 10
0
        private void ShowLine(IEnumerable <Tuple <Point3D, Point3D> > lines, Color color)
        {
            ScreenSpaceLines3D line = new ScreenSpaceLines3D();

            line.Color     = color;
            line.Thickness = 1;

            foreach (var segment in lines)
            {
                line.AddLine(segment.Item1, segment.Item2);
            }

            _debugVisuals.Add(line);
            _viewport.Children.Add(line);
        }
Exemplo n.º 11
0
        private void PhysicsBody_ApplyForceAndTorque(object sender, NewtonDynamics.BodyApplyForceAndTorqueArgs e)
        {
            // Clear visual
            if (_springVisual != null)
            {
                _springVisual.Points.Clear();
            }

            // See if there is anything to do
            if (_desiredPoint == null)
            {
                return;
            }

            Point3D current = e.Body.PositionToWorld(this.Offset.ToPoint());
            Point3D desired = _desiredPoint.Value;

            // I decided to add x^2, because a linear spring is too weak
            // f = k x^2
            Vector3D spring = desired - current;
            double   length = spring.Length;

            spring = spring.ToUnit() * (_springConstant * this.SpringForceMultiplier * length * length);

            // Apply the force
            if (_shouldSpringCauseTorque)
            {
                e.Body.AddForceAtPoint(spring, current);
            }
            else
            {
                e.Body.AddForce(spring);
            }

            if (_isDamped)      // this.ShouldDampenWhenSpring is taken into account before setting _isDamped to true
            {
                // Body.LinearDamping isn't strong enough, so do some more
                //Vector3D drag = e.Body.Velocity * (-_springConstant * this.SpringForceMultiplier * .33d);
                Vector3D drag = e.Body.Velocity * (-_springConstant * .33d);
                e.Body.AddForce(drag);
            }

            // Update visual
            if (_springVisual != null)
            {
                _springVisual.AddLine(current, desired);
            }
        }
Exemplo n.º 12
0
        private ScreenSpaceLines3D BuildLinksSprtLine(Point3D from, Point3D to, double weight)
        {
            ScreenSpaceLines3D retVal = new ScreenSpaceLines3D();

            if (weight > 0)
            {
                retVal.Color = UtilityWPF.ColorFromHex(COLOR_POSITIVE);
            }
            else
            {
                retVal.Color = UtilityWPF.ColorFromHex(COLOR_NEGATIVE);
            }
            retVal.Thickness = Math.Abs(weight) * 2d;
            retVal.AddLine(from, to);

            return(retVal);
        }
Exemplo n.º 13
0
        public Miner2DWindow()
        {
            InitializeComponent();

            #region Init World

            // Set the size of the world to something a bit random (gets boring when it's always the same size)
            double halfSize = 325 + StaticRandom.Next(500);
            _boundryMin = new Vector3D(-halfSize, -halfSize, -10);              // I need to set Z to something.  If the Z plates are too close together, then objects will get stuck and not move (constantly colliding)
            _boundryMax = new Vector3D(halfSize, halfSize, 10);

            _world.InitialiseBodies();
            _world.ShouldForce2D = true;
            _world.Gravity       = 0d;

            _materialManager = new MaterialManager(_world);
            _materialManager.SetCollisionCallback(_materialManager.ShipMaterialID, _materialManager.MineralMaterialID, null, Collision_Ship_Mineral);
            //_materialManager.SetCollisionCallback(_materialManager.ShipMaterialID, _materialManager.AsteroidMaterialID, null, Collision_Ship_Asteroid);

            List <Point3D[]> innerLines, outerLines;
            _world.SetCollisionBoundry(out innerLines, out outerLines, _viewport, _boundryMin, _boundryMax);

            Color lineColor = Color.FromArgb(255, 70, 70, 70); //UtilityWPF.AlphaBlend(Colors.White, Colors.DimGray, .1d);
            foreach (Point3D[] line in innerLines)
            {
                // Need to wait until the window is loaded to call lineModel.CalculateGeometry
                ScreenSpaceLines3D lineModel = new ScreenSpaceLines3D(false);
                lineModel.Thickness = 1d;
                lineModel.Color     = lineColor;
                lineModel.AddLine(line[0], line[1]);

                _viewport.Children.Add(lineModel);
                _lines.Add(lineModel);
            }

            _world.SimulationSpeed = 1d;
            //_world.UnPause();       // wait till all the objects are loaded

            #endregion

            optionsPanel.WorldSize           = halfSize * 2;
            optionsPanel.CameraAlwaysLooksUp = _cameraAlwaysLooksUp;
            optionsPanel.CameraAngle         = _cameraAngle;
            optionsPanel.ShowDebugVisuals    = _showDebugVisuals;
        }
Exemplo n.º 14
0
        private void trkMapSize_ValueChanged(object sender, RoutedPropertyChangedEventArgs <double> e)
        {
            if (_world == null || trkMapSize == null)
            {
                return;
            }

            // Clear old
            foreach (ScreenSpaceLines3D line in _lines)
            {
                _viewport.Children.Remove(line);
                line.Dispose();
            }
            _lines.Clear();

            double boundry = trkMapSize.Value;

            lblMapSize.Content = boundry.ToString("N0");

            // Set the new boundry
            List <Point3D[]> innerLines, outerLines;

            _world.SetCollisionBoundry(out innerLines, out outerLines, _viewport, new Vector3D(-boundry, -boundry, -boundry), new Vector3D(boundry, boundry, boundry));

            // Draw the lines
            Color lineColor = UtilityWPF.AlphaBlend(Colors.White, Colors.Gray, .1d);

            foreach (Point3D[] line in innerLines)
            {
                ScreenSpaceLines3D lineModel = new ScreenSpaceLines3D(false);
                lineModel.Thickness = 1d;
                lineModel.Color     = lineColor;
                lineModel.AddLine(line[0], line[1]);

                _viewport.Children.Add(lineModel);
                _lines.Add(lineModel);
            }

            // NOTE:  If this is called from the constructor, then recalculating doesn't do any good.  They must be recalculated
            // in the window load
            RecalculateLineGeometries();
        }
Exemplo n.º 15
0
        private void ShowHideBlockedCells()
        {
            _isBlockedCellDirty = false;

            // Wipe out the old one
            if (_blockedCellsWireframe != null)
            {
                _viewport.Children.Remove(_blockedCellsWireframe);
                _blockedCellsWireframe = null;
            }

            // See if a new one needs to be created
            if (!chkShowBlockedCells.IsChecked.Value || _field == null)
            {
                return;
            }

            int[] blockedCellIndices = Enumerable.Range(0, _field.Size1D).Where(o => _field.Blocked[o]).ToArray();
            if (blockedCellIndices.Length == 0)
            {
                return;
            }

            Rectangle3DIndexedMapped[] cells = _field.GetCells(_sizeMult * _field.Size);

            // Get a deduped list of blocked cell's edge lines
            var lines = Rectangle3DIndexed.GetEdgeLinesDeduped(blockedCellIndices.Select(o => cells[o]));

            // Create the visual
            _blockedCellsWireframe           = new ScreenSpaceLines3D();
            _blockedCellsWireframe.Color     = UtilityWPF.ColorFromHex("60FFFFFF");
            _blockedCellsWireframe.Thickness = 1;

            Point3D[] allPoints = cells[0].AllPoints;

            foreach (var line in lines)
            {
                _blockedCellsWireframe.AddLine(allPoints[line.Item1], allPoints[line.Item2]);
            }

            _viewport.Children.Add(_blockedCellsWireframe);
        }
Exemplo n.º 16
0
        public SwarmBotTester()
        {
            InitializeComponent();

            #region Init World

            _world.InitialiseBodies();
            //_world.ShouldForce2D = true;
            //_world.Gravity = -10d;     // -600d;    //this is set by the trackbar
            //_world.Gravity = 0d;

            List <Point3D[]> innerLines, outerLines;
            _world.SetCollisionBoundry(out innerLines, out outerLines, _viewport, _boundryMin, _boundryMax);

            Color lineColor = Color.FromArgb(255, 200, 200, 180);
            foreach (Point3D[] line in innerLines)
            {
                // Need to wait until the window is loaded to call lineModel.CalculateGeometry
                ScreenSpaceLines3D lineModel = new ScreenSpaceLines3D(false);
                lineModel.Thickness = 1d;
                lineModel.Color     = lineColor;
                lineModel.AddLine(line[0], line[1]);

                _viewport.Children.Add(lineModel);
                _lines.Add(lineModel);
            }

            //_world.SimulationSpeed = .1d;      // this was when I had to set change in velocity instead of applying forces
            _world.SimulationSpeed = 8d;
            _world.UnPause();

            #endregion

            _isInitialized = true;

            // Make the form look right
            trkGravity_ValueChanged(this, new RoutedPropertyChangedEventArgs <double>(trkGravity.Value, trkGravity.Value));
            SetActiveBehavior();
        }
Exemplo n.º 17
0
        public GravityCubes2()
        {
            InitializeComponent();

            grdPanel.Background          = SystemColors.ControlBrush;
            chkAttractedToOrigin.Content = "Attracted to\r\nOrigin";

            #region Init World

            _world.InitialiseBodies();
            _world.Gravity = 0d;

            List <Point3D[]> innerLines, outerLines;
            _world.SetCollisionBoundry(out innerLines, out outerLines, _viewport, new Vector3D(-15, -15, -15), new Vector3D(15, 15, 15));

            Color lineColor = UtilityWPF.AlphaBlend(Colors.White, Colors.Gray, .1d);
            foreach (Point3D[] line in innerLines)
            {
                // Need to wait until the window is loaded to call lineModel.CalculateGeometry
                ScreenSpaceLines3D lineModel = new ScreenSpaceLines3D(false);
                lineModel.Thickness = 1d;
                lineModel.Color     = lineColor;
                lineModel.AddLine(line[0], line[1]);

                _viewport.Children.Add(lineModel);
                _lines.Add(lineModel);
            }

            _world.UnPause();

            #endregion

            _trackball                       = new TrackBallRoam(_camera);
            _trackball.EventSource           = grdViewPort;
            _trackball.AllowZoomOnMouseWheel = true;
            _trackball.Mappings.AddRange(TrackBallMapping.GetPrebuilt(TrackBallMapping.PrebuiltMapping.MouseComplete));
        }
Exemplo n.º 18
0
        private Visual3D[] GetStrokeVisual_VELOCITIES(Tuple <Point3D, Vector3D>[] points)
        {
            ScreenSpaceLines3D retVal = new ScreenSpaceLines3D();

            retVal.Color     = UtilityWPF.ColorFromHex("EEE");
            retVal.Thickness = 2;

            for (int cntr = 0; cntr < points.Length - 1; cntr++)
            {
                Point3D secondPoint = points[cntr].Item1;
                if (points[cntr].Item2.IsNearZero())
                {
                    secondPoint += Math3D.GetRandomVector_Spherical(.02);
                }
                else
                {
                    secondPoint += points[cntr].Item2;
                }

                retVal.AddLine(points[cntr].Item1, secondPoint);
            }

            return(new Visual3D[] { retVal });
        }
Exemplo n.º 19
0
        private void AddAABBLines(Point3D min, Point3D max, Color color)
        {
            ScreenSpaceLines3D lineVisual = new ScreenSpaceLines3D(true);

            lineVisual.Thickness = 1;
            lineVisual.Color     = color;

            // Top
            lineVisual.AddLine(new Point3D(min.X, min.Y, min.Z), new Point3D(max.X, min.Y, min.Z));
            lineVisual.AddLine(new Point3D(max.X, min.Y, min.Z), new Point3D(max.X, max.Y, min.Z));
            lineVisual.AddLine(new Point3D(max.X, max.Y, min.Z), new Point3D(min.X, max.Y, min.Z));
            lineVisual.AddLine(new Point3D(min.X, max.Y, min.Z), new Point3D(min.X, min.Y, min.Z));

            // Bottom
            lineVisual.AddLine(new Point3D(min.X, min.Y, max.Z), new Point3D(max.X, min.Y, max.Z));
            lineVisual.AddLine(new Point3D(max.X, min.Y, max.Z), new Point3D(max.X, max.Y, max.Z));
            lineVisual.AddLine(new Point3D(max.X, max.Y, max.Z), new Point3D(min.X, max.Y, max.Z));
            lineVisual.AddLine(new Point3D(min.X, max.Y, max.Z), new Point3D(min.X, min.Y, max.Z));

            // Sides
            lineVisual.AddLine(new Point3D(min.X, min.Y, min.Z), new Point3D(min.X, min.Y, max.Z));
            lineVisual.AddLine(new Point3D(max.X, min.Y, min.Z), new Point3D(max.X, min.Y, max.Z));
            lineVisual.AddLine(new Point3D(max.X, max.Y, min.Z), new Point3D(max.X, max.Y, max.Z));
            lineVisual.AddLine(new Point3D(min.X, max.Y, min.Z), new Point3D(min.X, max.Y, max.Z));

            _viewport.Children.Add(lineVisual);
            _visuals.Add(lineVisual);
        }
Exemplo n.º 20
0
        private void ResetField()
        {
            _sceneRemaining = DateTime.MinValue;

            if (_border != null)
            {
                _viewport.Children.Remove(_border);
            }

            if (_marker2D != null)
            {
                _viewport.Children.Remove(_marker2D);
            }

            _velocityLines.Clear();

            _sizeMult     = 1d / _field.Size;
            _velocityMult = 4d;

            // Border Lines
            _border           = new ScreenSpaceLines3D();
            _border.Color     = Colors.DimGray;
            _border.Thickness = 1d;

            double l = (_field.Size * _sizeMult) / 2d;       // using lower case l to represent half (just because it looks a lot like 1)

            _border.AddLine(new Point3D(-l, -l, -l), new Point3D(l, -l, -l));
            _border.AddLine(new Point3D(l, -l, -l), new Point3D(l, l, -l));
            _border.AddLine(new Point3D(l, l, -l), new Point3D(-l, l, -l));
            _border.AddLine(new Point3D(-l, l, -l), new Point3D(-l, -l, -l));

            _border.AddLine(new Point3D(-l, -l, l), new Point3D(l, -l, l));
            _border.AddLine(new Point3D(l, -l, l), new Point3D(l, l, l));
            _border.AddLine(new Point3D(l, l, l), new Point3D(-l, l, l));
            _border.AddLine(new Point3D(-l, l, l), new Point3D(-l, -l, l));

            _border.AddLine(new Point3D(-l, -l, -l), new Point3D(-l, -l, l));
            _border.AddLine(new Point3D(l, -l, -l), new Point3D(l, -l, l));
            _border.AddLine(new Point3D(l, l, -l), new Point3D(l, l, l));
            _border.AddLine(new Point3D(-l, l, -l), new Point3D(-l, l, l));

            _viewport.Children.Add(_border);

            ShowHideBlockedCells();     // this will update _blockedCellsWireframe

            if (_lookDirection != null)
            {
                ViewChanged(_lookDirection.Value);      // this will update _marker2D
            }
        }
Exemplo n.º 21
0
        private void SetCurrentVisual(Visual3D visual)
        {
            if (_currentVisuals != null)
            {
                _viewport.Children.RemoveAll(_currentVisuals);
                _currentVisuals = null;
            }

            List <Visual3D> visuals = new List <Visual3D>();

            if (chkShowNormals.IsChecked.Value)
            {
                #region Show normals

                ModelVisual3D visualCast = visual as ModelVisual3D;
                if (visualCast != null)
                {
                    GeometryModel3D modelCast = visualCast.Content as GeometryModel3D;
                    if (modelCast != null)
                    {
                        MeshGeometry3D geometryCast = modelCast.Geometry as MeshGeometry3D;
                        if (geometryCast != null)
                        {
                            // Mesh point normals
                            if (geometryCast.Positions.Count == geometryCast.Normals.Count)
                            {
                                ScreenSpaceLines3D lines = new ScreenSpaceLines3D();
                                lines.Color     = Colors.HotPink;
                                lines.Thickness = 1d;

                                for (int cntr = 0; cntr < geometryCast.Positions.Count; cntr++)
                                {
                                    lines.AddLine(geometryCast.Positions[cntr], geometryCast.Positions[cntr] + geometryCast.Normals[cntr]);
                                }

                                visuals.Add(lines);
                            }

                            // Triangle normals
                            bool hadTriangle = false;

                            ScreenSpaceLines3D lines2 = new ScreenSpaceLines3D();
                            lines2.Color     = Colors.Gold;
                            lines2.Thickness = 1d;

                            foreach (ITriangle triangle in UtilityWPF.GetTrianglesFromMesh(geometryCast))
                            {
                                hadTriangle = true;
                                lines2.AddLine(triangle.GetCenterPoint(), triangle.GetCenterPoint() + triangle.NormalUnit);
                            }

                            if (hadTriangle)
                            {
                                visuals.Add(lines2);
                            }
                        }
                    }
                }

                #endregion
            }

            visuals.Add(visual);

            _currentVisuals = visuals.ToArray();
            _viewport.Children.AddRange(_currentVisuals);
        }
Exemplo n.º 22
0
        private void World_Updating(object sender, WorldUpdatingArgs e)
        {
            _updateManager.Update_MainThread(e.ElapsedTime);

            if ((SHOWGRAVITYLINES || SHOWGRAVITYMASSES) && (DateTime.UtcNow - _debugGravityVisualsLastBuild).TotalMilliseconds > 150d)
            {
                #region Show gravity lines

                // Clear existing visuals
                if (_debugGravityVisuals == null)
                {
                    _debugGravityVisuals = new List <Visual3D>();
                }

                foreach (Visual3D model in _debugGravityVisuals)
                {
                    _viewport.Children.Remove(model);
                }
                _debugGravityVisuals.Clear();

                if (SHOWGRAVITYLINES)
                {
                    #region Lines

                    ScreenSpaceLines3D line = new ScreenSpaceLines3D(true);
                    line.Color     = UtilityWPF.ColorFromHex("80B8DE2F");
                    line.Thickness = 1d;

                    double multiplier = .01d;

                    foreach (var field in _gravityField.GetLeaves())
                    {
                        line.AddLine(field.Position, field.Position + (field.Force * multiplier));
                    }

                    _debugGravityVisuals.Add(line);
                    _viewport.Children.Add(line);

                    #endregion
                }

                if (SHOWGRAVITYMASSES)
                {
                    #region Boxes

                    Model3DGroup geometries = new Model3DGroup();

                    double multiplier = .5d;
                    double oneThird   = 1d / 3d;

                    foreach (var field in _gravityField.GetLeaves())
                    {
                        MaterialGroup material = new MaterialGroup();
                        material.Children.Add(new DiffuseMaterial(new SolidColorBrush(UtilityWPF.ColorFromHex("40CF835B"))));
                        material.Children.Add(new SpecularMaterial(new SolidColorBrush(UtilityWPF.ColorFromHex("A8FFA170")), 50d));

                        GeometryModel3D geometry = new GeometryModel3D();
                        geometry.Material     = material;
                        geometry.BackMaterial = material;

                        double length = Math.Pow(field.Mass, oneThird) * multiplier;
                        length *= .5d;

                        geometry.Geometry = UtilityWPF.GetCube_IndependentFaces(new Point3D(field.Position.X - length, field.Position.Y - length, field.Position.Z - length), new Point3D(field.Position.X + length, field.Position.Y + length, field.Position.Z + length));

                        geometries.Children.Add(geometry);
                    }

                    ModelVisual3D model = new ModelVisual3D();
                    model.Content = geometries;

                    _debugGravityVisuals.Add(model);
                    _viewport.Children.Add(model);

                    #endregion
                }

                _debugGravityVisualsLastBuild = DateTime.UtcNow;

                #endregion
            }
        }
Exemplo n.º 23
0
        private void btnCalculateForces_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                double radius;
                if (!double.TryParse(txtRadius.Text, out radius))
                {
                    MessageBox.Show("Couldn't parse radius as a double", this.Title, MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }

                double calcDist;
                if (chkCalcDist.IsChecked.Value)
                {
                    if (!double.TryParse(txtCalcDist.Text, out calcDist))
                    {
                        MessageBox.Show("Couldn't parse calculation distance as a double", this.Title, MessageBoxButton.OK, MessageBoxImage.Warning);
                        return;
                    }
                }
                else
                {
                    calcDist = GetCalcDistance(GetApproximateCount(_dots, radius), radius);
                }

                ClearDebugVisuals();

                if ((chkInteriorPoints.IsChecked.Value && chkInwardForce.IsChecked.Value) || chkRepulsiveForce.IsChecked.Value)
                {
                    Vector3D[] forces = new Vector3D[_dots.Count];

                    if (chkInteriorPoints.IsChecked.Value && chkInwardForce.IsChecked.Value)
                    {
                        // Inward Force
                        GetInwardForces(forces, _dots);
                    }

                    if (chkRepulsiveForce.IsChecked.Value)
                    {
                        // Repulsion Force
                        GetRepulsionForces(forces, _dots, calcDist);
                    }

                    #region Show Lines

                    ScreenSpaceLines3D lines = new ScreenSpaceLines3D();
                    lines.Thickness = 1d;
                    lines.Color     = UtilityWPF.ColorFromHex("A8A8A8");
                    for (int cntr = 0; cntr < _dots.Count; cntr++)
                    {
                        lines.AddLine(_dots[cntr].Position, _dots[cntr].Position + forces[cntr]);
                    }

                    _debugVisuals.Add(lines);
                    _viewport.Children.Add(lines);

                    #endregion
                }

                UpdateReport();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), this.Title, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Exemplo n.º 24
0
        public void ViewChanged(DoubleVector lookDirection)
        {
            if (_marker2D != null)
            {
                _viewport.Children.Remove(_marker2D);
            }

            if (_marker2D == null)
            {
                _marker2D           = new ScreenSpaceLines3D();
                _marker2D.Color     = UtilityWPF.ColorFromHex("60A0A0A0");
                _marker2D.Thickness = 4d;
            }

            _marker2D.Clear();

            RotateTransform3D transform = new RotateTransform3D(new QuaternionRotation3D(Math3D.GetRotation(new DoubleVector(0, 0, 1, 0, -1, 0), lookDirection)));

            #region Marker 2D

            double orthDist   = (_sizeMult * _field.Size * 1.1d) / 2d;
            double cornerDist = (_sizeMult * _field.Size * .2d) / 2d;

            // TopLeft
            Point3D  corner    = transform.Transform(new Point3D(-orthDist, -orthDist, -orthDist));
            Vector3D direction = transform.Transform(new Vector3D(cornerDist, 0, 0));
            _marker2D.AddLine(corner, corner + direction);
            direction = transform.Transform(new Vector3D(0, cornerDist, 0));
            _marker2D.AddLine(corner, corner + direction);

            //TopRight
            corner    = transform.Transform(new Point3D(orthDist, -orthDist, -orthDist));
            direction = transform.Transform(new Vector3D(-cornerDist, 0, 0));
            _marker2D.AddLine(corner, corner + direction);
            direction = transform.Transform(new Vector3D(0, cornerDist, 0));
            _marker2D.AddLine(corner, corner + direction);

            //BottomRight
            corner    = transform.Transform(new Point3D(orthDist, orthDist, -orthDist));
            direction = transform.Transform(new Vector3D(-cornerDist, 0, 0));
            _marker2D.AddLine(corner, corner + direction);
            direction = transform.Transform(new Vector3D(0, -cornerDist, 0));
            _marker2D.AddLine(corner, corner + direction);

            //BottomLeft
            corner    = transform.Transform(new Point3D(-orthDist, orthDist, -orthDist));
            direction = transform.Transform(new Vector3D(cornerDist, 0, 0));
            _marker2D.AddLine(corner, corner + direction);
            direction = transform.Transform(new Vector3D(0, -cornerDist, 0));
            _marker2D.AddLine(corner, corner + direction);

            _viewport.Children.Add(_marker2D);

            #endregion

            // Camera
            _camera.Position      = transform.Transform(new Point3D(0, 0, (_sizeMult * _field.Size * -4d) / 2d));
            _camera.LookDirection = lookDirection.Standard;
            _camera.UpDirection   = lookDirection.Orth;

            // Remember it
            _lookDirection = lookDirection;
        }
Exemplo n.º 25
0
        public void CreateGuildLines()
        {
            const double SIZE = 50d;

            GuideLines = new ScreenSpaceLines3D(true);

            // Get a color based on the part type
            Color color;

            if (Part3D is AmmoBoxDesign)
            {
                color = WorldColors.AmmoBox_Color;
            }
            else if (Part3D is BeamGunDesign || Part3D is GrappleGunDesign || Part3D is ProjectileGunDesign)
            {
                color = UtilityWPF.AlphaBlend(Colors.White, WorldColors.GunBarrel_Color, .125d);                // shift it white, because it's too dark
            }
            else if (Part3D is BrainDesign || Part3D is BrainNEATDesign || Part3D is BrainRGBRecognizerDesign || Part3D is DirectionControllerRingDesign || Part3D is DirectionControllerSphereDesign)
            {
                color = UtilityWPF.AlphaBlend(Colors.Transparent, WorldColors.Brain_Color, .25d);
            }
            else if (Part3D is CargoBayDesign)
            {
                color = WorldColors.CargoBay_Color;
            }
            else if (Part3D is ConverterEnergyToAmmoDesign || Part3D is ConverterEnergyToFuelDesign || Part3D is ConverterEnergyToPlasmaDesign || Part3D is ConverterFuelToEnergyDesign || Part3D is ConverterMatterToAmmoDesign || Part3D is ConverterMatterToEnergyDesign || Part3D is ConverterMatterToFuelDesign || Part3D is ConverterRadiationToEnergyDesign || Part3D is ConverterMatterToPlasmaDesign)
            {
                color = UtilityWPF.AlphaBlend(Colors.White, WorldColors.ConverterBase_Color, .125d);
            }
            else if (Part3D is EnergyTankDesign)
            {
                color = WorldColors.EnergyTank_Color;
            }
            else if (Part3D is EyeDesign || Part3D is CameraColorRGBDesign || Part3D is CameraHardCodedDesign)
            {
                color = UtilityWPF.AlphaBlend(Colors.White, WorldColors.CameraBase_Color, .15d);
            }
            else if (Part3D is SensorCollisionDesign || Part3D is SensorFluidDesign || Part3D is SensorGravityDesign || Part3D is SensorInternalForceDesign || Part3D is SensorNetForceDesign || Part3D is SensorRadiationDesign || Part3D is SensorSpinDesign || Part3D is SensorTractorDesign || Part3D is SensorVelocityDesign || Part3D is SensorHomingDesign)
            {
                color = UtilityWPF.AlphaBlend(Colors.White, WorldColors.SensorBase_Color, .125d);
            }
            else if (Part3D is FuelTankDesign)
            {
                color = WorldColors.FuelTank_Color;
            }
            else if (Part3D is HangarBayDesign)
            {
                color = UtilityWPF.AlphaBlend(Colors.Transparent, WorldColors.HangarBay_Color, .25d);
            }
            else if (Part3D is SelfRepairDesign)
            {
                color = UtilityWPF.AlphaBlend(Colors.Transparent, WorldColors.SelfRepairBase_Color, .25d);
            }
            else if (Part3D is ShieldEnergyDesign || Part3D is ShieldKineticDesign || Part3D is ShieldTractorDesign)
            {
                color = UtilityWPF.AlphaBlend(Colors.Transparent, WorldColors.ShieldBase_Color, .125d);
            }
            else if (Part3D is ThrusterDesign)
            {
                color = WorldColors.Thruster_Color;
            }
            else if (Part3D is TractorBeamDesign)
            {
                color = WorldColors.TractorBeamBase_Color;
            }
            else if (Part3D is ImpulseEngineDesign)
            {
                color = UtilityWPF.AlphaBlend(Colors.White,
                                              UtilityWPF.AlphaBlend(WorldColors.ImpulseEngineGlowball_Color, WorldColors.ImpulseEngine_Color, .25d),
                                              .125d);
            }
            else if (Part3D is PlasmaTankDesign)
            {
                color = WorldColors.PlasmaTank_Color;
            }
            else if (Part3D is SwarmBayDesign)
            {
                color = WorldColors.SwarmBay_Color;
            }
            else
            {
                color = Colors.Black;
            }

            // Tone the color down a bit
            //color = UtilityWPF.AlphaBlend(color, _colors.Background, .9d);
            color = UtilityWPF.AlphaBlend(color, Colors.Transparent, .66d);

            GuideLines.Color = color;

            GuideLines.Thickness = .5d;
            GuideLines.AddLine(new Point3D(-SIZE, 0, 0), new Point3D(SIZE, 0, 0));
            GuideLines.AddLine(new Point3D(0, -SIZE, 0), new Point3D(0, SIZE, 0));
            GuideLines.AddLine(new Point3D(0, 0, -SIZE), new Point3D(0, 0, SIZE));

            GuideLines.Transform = new TranslateTransform3D(Part3D.Position.ToVector());
        }
Exemplo n.º 26
0
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                //_camera.Position = new Point3D(0, 0, -1000);
                //_camera.LookDirection = new Vector3D(0, 0, 1);
                //_camera.UpDirection = new Vector3D(0, 1, 0);

                #region Init World

                // Set the size of the world to something a bit random (gets boring when it's always the same size)
                double halfSize = 325 + StaticRandom.Next(500);
                halfSize   *= 1d + (StaticRandom.NextDouble() * 5d);
                _boundryMin = new Point3D(-halfSize, -halfSize, halfSize * -.25);
                _boundryMax = new Point3D(halfSize, halfSize, halfSize * .25);

                _world           = new World();
                _world.Updating += new EventHandler <WorldUpdatingArgs>(World_Updating);

                var boundryLines = _world.SetCollisionBoundry(_boundryMin, _boundryMax);

                // Draw the lines
                _boundryLines           = new ScreenSpaceLines3D(true);
                _boundryLines.Thickness = 1d;
                _boundryLines.Color     = WorldColors.BoundryLines;
                _viewport.Children.Add(_boundryLines);

                foreach (var line in boundryLines.innerLines)
                {
                    _boundryLines.AddLine(line.from, line.to);
                }

                #endregion
                #region Materials

                _materialManager = new MaterialManager(_world);

                // Asteroid
                Game.Newt.v2.NewtonDynamics.Material material = new Game.Newt.v2.NewtonDynamics.Material();
                material.Elasticity      = .25d;
                material.StaticFriction  = .9d;
                material.KineticFriction = .75d;
                _material_Asteroid       = _materialManager.AddMaterial(material);

                // Mineral
                material                 = new Game.Newt.v2.NewtonDynamics.Material();
                material.Elasticity      = .5d;
                material.StaticFriction  = .9d;
                material.KineticFriction = .4d;
                _material_Mineral        = _materialManager.AddMaterial(material);

                // Space Station (force field)
                material                 = new Game.Newt.v2.NewtonDynamics.Material();
                material.Elasticity      = .99d;
                material.StaticFriction  = .02d;
                material.KineticFriction = .01d;
                _material_SpaceStation   = _materialManager.AddMaterial(material);

                _materialManager.RegisterCollisionEvent(_material_SpaceStation, _material_Asteroid, Collision_SpaceStation);
                _materialManager.RegisterCollisionEvent(_material_SpaceStation, _material_Mineral, Collision_SpaceStation);

                #endregion
                #region Trackball

                // Trackball
                _trackball                       = new TrackBallRoam(_camera);
                _trackball.KeyPanScale           = 15d;
                _trackball.EventSource           = grdViewPort; //NOTE:  If this control doesn't have a background color set, the trackball won't see events (I think transparent is ok, just not null)
                _trackball.AllowZoomOnMouseWheel = true;
                _trackball.Mappings.AddRange(TrackBallMapping.GetPrebuilt(TrackBallMapping.PrebuiltMapping.MouseComplete_NoLeft_RightRotateInPlace));
                _trackball.Mappings.AddRange(TrackBallMapping.GetPrebuilt(TrackBallMapping.PrebuiltMapping.Keyboard_ASDW_In));
                //_trackball.GetOrbitRadius += new GetOrbitRadiusHandler(Trackball_GetOrbitRadius);
                _trackball.ShouldHitTestOnOrbit = true;

                #endregion
                #region Map

                _map = new Map(_viewport, null, _world);
                _map.SnapshotFequency_Milliseconds = 125;
                _map.SnapshotMaxItemsPerNode       = 10;
                _map.ShouldBuildSnapshots          = true;
                _map.ShouldShowSnapshotLines       = _optionsPanel.OctreeShowLines;
                _map.ShouldSnapshotCentersDrift    = _optionsPanel.OctreeCentersDrift;

                #endregion
                #region UpdateManager

                _updateManager = new UpdateManager(
                    new Type[] { typeof(SpaceStation) },
                    new Type[0],
                    _map);

                #endregion
                CreateFields();

                //TODO:  Add these during a timer to minimize the load time
                if (_optionsPanel.ShowStars)
                {
                    CreateStars();
                }
                CreateAsteroids();
                CreateMinerals();
                CreateSpaceStations();          // creating these last so stuff shows up behind them

                #region Start Position

                #region Farthest Station

                //// The center is too chaotic, so choose the farthest out space station
                //Point3D farthestStationPoint = new Point3D(0, 0, 0);
                //double farthestStationDistance = 0d;
                //foreach (Point3D stationPosition in _map.GetAllObjects().Where(o => o is SpaceStation).Select(o => o.PositionWorld))
                //{
                //    double distance = stationPosition.ToVector().LengthSquared;
                //    if (distance > farthestStationDistance)
                //    {
                //        farthestStationPoint = stationPosition;
                //        farthestStationDistance = distance;
                //    }
                //}

                //// Set the camera near there
                //_camera.Position = farthestStationPoint + (farthestStationPoint.ToVector().ToUnit() * 75d) + Math3D.GetRandomVectorSphericalShell(25d);
                //_camera.LookDirection = _camera.Position.ToVector() * -1d;
                //_camera.UpDirection = new Vector3D(0, 0, 1);

                #endregion
                #region Random Station

                List <Point3D> stationPoints = _map.GetAllItems().Where(o => o is SpaceStation).Select(o => o.PositionWorld).ToList();
                Point3D        stationPoint  = stationPoints[StaticRandom.Next(stationPoints.Count)];

                // Set the camera near there
                _camera.Position      = stationPoint + (stationPoint.ToVector().ToUnit() * 75d) + Math3D.GetRandomVector_Spherical_Shell(25d);
                _camera.LookDirection = _camera.Position.ToVector() * -1d;
                _camera.UpDirection   = new Vector3D(0, 0, 1);

                #endregion

                #endregion

                //TODO:  kuler for dialogs

                _world.UnPause();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), this.Title, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }