Exemplo n.º 1
0
        /// <summary>
        /// This function prunes the list of positions that are too old.  It then figures out the average velocity
        /// of the remaining positions.
        /// </summary>
        private MyVector TimerSprtCalculateVelocity()
        {
            const int RETENTION = 75;           // milliseconds

            int curTick = Environment.TickCount;

            // Add the current to the list
            _prevMousePositions.Add(new MousePosition(_curMousePoint.Clone()));

            #region Prune List

            int lastIndex = -1;
            for (int cntr = _prevMousePositions.Count - 1; cntr >= 0; cntr--)
            {
                if (curTick - _prevMousePositions[cntr].Tick > RETENTION)
                {
                    // This item is too old.  And since the list is in time order, all the entries before this are also too old
                    lastIndex = cntr;
                    break;
                }
            }

            if (lastIndex >= 0)
            {
                //_prevMousePositions.RemoveRange(lastIndex, _prevMousePositions.Count - lastIndex);
                _prevMousePositions.RemoveRange(0, lastIndex + 1);
            }

            #endregion

            #region Calculate Velocity

            MyVector retVal = new MyVector(0, 0, 0);

            // Add up all the instantaneous velocities
            for (int cntr = 0; cntr < _prevMousePositions.Count - 1; cntr++)
            {
                retVal.Add(_prevMousePositions[cntr + 1].Position - _prevMousePositions[cntr].Position);
            }

            // Take the average
            if (_prevMousePositions.Count > 2)          // if count is 0 or 1, then retVal will still be (0,0,0).  If it's 2, then I would be dividing by 1, which is pointless
            {
                retVal.Divide(_prevMousePositions.Count - 1);
            }

            #endregion

            // Exit Function
            return(retVal);
        }
Exemplo n.º 2
0
        private void button1_Click(object sender, EventArgs e)
        {
            MyVector v1 = new MyVector(3, 4, 5);

            v1.Add(1, 2, 3);

            v1.BecomeUnitVector();

            MyVector v2 = v1.Clone();

            v2.Multiply(3);

            v1.Divide(3);
        }
Exemplo n.º 3
0
        /// <summary>
        /// If the user clicks on a picture box, you need to run those coords through this function to figure out where they
        /// clicked in world coords
        /// </summary>
        /// <param name="position">A point in PictureBox coords</param>
        /// <returns>The point in World Coords</returns>
        public MyVector GetPositionViewToWorld(MyVector position)
        {
            MyVector retVal = position.Clone();

            // Figure out the world coords that the top left of the picturebox represents
            double picLeft = _centerPoint.X - ((this.Width / 2d) / _zoom);
            double picTop  = _centerPoint.Y - ((this.Height / 2d) / _zoom);

            // The point passed in is a distance from the top left of the picture box to where they clicked.  Turn this view
            // coords into world coords
            retVal.Divide(_zoom);

            // Add these world coords to the top left of the picture box
            retVal.X += picLeft;
            retVal.Y += picTop;

            // It's now what it needs to be
            return(retVal);
        }
Exemplo n.º 4
0
        private void ApplyExplosionForce(RadarBlip blip)
        {
            if (!(blip is BallBlip))
            {
                return;
            }

            BallBlip castBlip = (BallBlip)blip;

            MyVector force = castBlip.Ball.Position - this.Ball.Position;

            force.BecomeUnitVector();
            force.Multiply(_explosion.Force);

            // Setting the force does no good, because PrepareForNewTick is called before it can take affect
            //castBlip.Ball.ExternalForce.Add(force);

            force.Divide(castBlip.Ball.Mass);           // F=MA
            castBlip.Ball.Velocity.Add(force);
        }
Exemplo n.º 5
0
        /// <summary>
        /// This function will set the center position to the center of the rectangle passed in, and set the zoom so that I can
        /// see everything inside the rectangle
        /// </summary>
        /// <remarks>
        /// I will be in static mode after this call (as opposed to chase mode)
        /// </remarks>
        public void ZoomRectangle(MyVector cornerLower, MyVector cornerUpper)
        {
            // Figure out the position.  By setting it to a new vector, I've gone into static mode
            _centerPoint = cornerLower + cornerUpper;
            _centerPoint.Divide(2);

            // Set temp X and Y to the zoom rectangle width and height
            double tempX = cornerUpper.X - cornerLower.X;
            double tempY = cornerUpper.Y - cornerLower.Y;

            // Now set temp X and Y to the corresponding zoom factors
            tempX = this.Width / tempX;
            tempY = this.Height / tempY;

            // Store the lower of the two
            if (tempX < tempY)
            {
                _zoom = tempX;
            }
            else
            {
                _zoom = tempY;
            }
        }
Exemplo n.º 6
0
        private void Copy()
        {
            if (_mode != SelectionMode.Selected || _selectedObjects.Count == 0)
            {
                return;
            }

            _myClipboard.Clear();
            _clipboardPositionCenter = new MyVector();

            foreach (RadarBlip blip in _map.GetAllBlips())
            {
                if (_selectedObjects.Contains(blip.Token))
                {
                    _myClipboard.Add(Scenes.CloneBlip(blip, _map));
                    _clipboardPositionCenter.Add(blip.Sphere.Position);
                }
            }

            if (_myClipboard.Count > 0)
            {
                _clipboardPositionCenter.Divide(_myClipboard.Count);
            }
        }
Exemplo n.º 7
0
        void picturebox_MouseUp(object sender, MouseEventArgs e)
        {
            if (!_active)
            {
                return;
            }

            if (_isMouseDown == MouseButtonDown.Left && e.Button == MouseButtons.Left)
            {
                #region Left

                _isMouseDown   = MouseButtonDown.None;
                _curMousePoint = _picturebox.GetPositionViewToWorld(new MyVector(e.X, e.Y, 0));

                switch (_mode)
                {
                case SelectionMode.Rectangle:
                    #region Add Objects Inside Rectangle

                    // Add any objects that were in the rectangle (I don't need to look at ctrl or shift here, I'm just adding)
                    foreach (RadarBlip blip in _map.GetAllBlips())
                    {
                        if (!_selectedObjects.Contains(blip.Token))
                        {
                            if (SelectionTest(blip, _mouseDownPoint, _curMousePoint))
                            {
                                _selectedObjects.Add(blip.Token);
                            }
                        }
                    }

                    #endregion
                    break;

                case SelectionMode.Selected:
                    #region Clear temp stationary objects

                    // While dragging, their velocities were manipulated, now just let them fly.  (they're still selected)

                    // Turn any temp stationary objects back into standard objects
                    foreach (RadarBlip blip in _map.GetAllBlips())
                    {
                        if (_tempStationaryObjects.Contains(blip.Token))
                        {
                            blip.CollisionStyle = CollisionStyle.Standard;
                        }
                    }

                    _tempStationaryObjects.Clear();

                    #endregion
                    break;

                default:
                    throw new ApplicationException("Unknown SelectionMode: " + _mode);
                }

                // Set my mode
                if (_selectedObjects.Count > 0)
                {
                    _mode = SelectionMode.Selected;
                }
                else
                {
                    _mode = SelectionMode.None;
                }

                #endregion
            }
            else if (_isMouseDown == MouseButtonDown.Right && e.Button == MouseButtons.Right)
            {
                #region Right

                _isMouseDown   = MouseButtonDown.None;
                _curMousePoint = _picturebox.GetPositionViewToWorld(new MyVector(e.X, e.Y, 0));

                // Apply the velocity to all the selected objects
                MyVector newVelocity = _curMousePoint - _mouseDownPoint;
                newVelocity.Divide(10);

                foreach (RadarBlip blip in _map.GetAllBlips())
                {
                    if (_selectedObjects.Contains(blip.Token))
                    {
                        if (blip is BallBlip)
                        {
                            ((BallBlip)blip).Ball.Velocity.StoreNewValues(newVelocity);
                        }
                    }
                }

                #endregion
            }
        }