Exemplo n.º 1
0
        private void DoGravity()
        {
            const double GRAVITATIONALCONSTANT = .001d; //500d;

            //double gravitationalConstantAdjusted = GRAVITATIONALCONSTANT * _gravityMultiplier;

            RadarBlip[] blips = _map.GetAllBlips();

            for (int outerCntr = 0; outerCntr < blips.Length - 1; outerCntr++)
            {
                Ball ball1 = blips[outerCntr].Sphere as Ball;

                for (int innerCntr = outerCntr + 1; innerCntr < blips.Length; innerCntr++)
                {
                    #region Apply Gravity

                    Ball ball2 = blips[innerCntr].Sphere as Ball;

                    MyVector centerMass1, centerMass2;
                    if (ball1 is TorqueBall)
                    {
                        centerMass1 = ball1.Position + ((TorqueBall)ball1).CenterOfMass;
                    }
                    else
                    {
                        centerMass1 = ball1.Position;
                    }

                    if (ball2 is TorqueBall)
                    {
                        centerMass2 = ball2.Position + ((TorqueBall)ball2).CenterOfMass;
                    }
                    else
                    {
                        centerMass2 = ball2.Position;
                    }

                    MyVector gravityLink = centerMass1 - centerMass2;

                    double force = GRAVITATIONALCONSTANT * (ball1.Mass * ball2.Mass) / gravityLink.GetMagnitudeSquared();

                    gravityLink.BecomeUnitVector();
                    gravityLink.Multiply(force);

                    if (blips[innerCntr].CollisionStyle == CollisionStyle.Standard)
                    {
                        ball2.ExternalForce.Add(gravityLink);
                    }

                    if (blips[outerCntr].CollisionStyle == CollisionStyle.Standard)
                    {
                        gravityLink.Multiply(-1d);
                        ball1.ExternalForce.Add(gravityLink);
                    }

                    #endregion
                }
            }
        }
Exemplo n.º 2
0
        private void ResetFieldSprtSwirl(bool goLeft)
        {
            // Init the grid
            _grid = new MyVector[_squaresPerSideX * _squaresPerSideY];

            // I'm going to rotate everything 90 degrees
            MyVector rotateAxis = new MyVector(0, 0, 1);
            double   radians    = Math.PI / 2d;

            if (goLeft)
            {
                radians *= -1d;
            }

            foreach (MyVector center in GetGridCenters())
            {
                // Get the local position
                MyVector localPosition = center - _position;

                // Turn that into a constant length, pointing in or out
                MyVector fieldLine = localPosition.Clone();
                fieldLine.BecomeUnitVector();
                fieldLine.Multiply(_strength);
                fieldLine.RotateAroundAxis(rotateAxis, radians);

                // Store it
                _grid[GetIndexForLocalPosition(localPosition)] = fieldLine;
            }
        }
Exemplo n.º 3
0
        private void DrawBall(Ball ball, Color ballColor, Color dirFacingStandColor, Color dirFacingOrthColor)
        {
            // Standard Facing
            MyVector workingVect = ball.DirectionFacing.Standard.Clone();

            workingVect.BecomeUnitVector();
            workingVect.Multiply(ball.Radius);
            workingVect.Add(ball.Position);

            DrawVector(ball.Position, workingVect, dirFacingStandColor);

            // Orthogonal Facing
            workingVect = ball.DirectionFacing.Orth.Clone();
            workingVect.BecomeUnitVector();
            workingVect.Multiply(ball.Radius);
            workingVect.Add(ball.Position);

            DrawVector(ball.Position, workingVect, dirFacingOrthColor);

            // Ball
            DrawBall(ball, ballColor);
        }
Exemplo n.º 4
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.º 5
0
        private void RunGravityBall(double elapsedTime, Ball gravityBall, Color color)
        {
            const double GRAVITATIONALCONSTANT = 1000d;

            pictureBox1.FillCircle(color, gravityBall.Position, gravityBall.Radius);

            gravityBall.PrepareForNewTimerCycle();

            MyVector gravityLink = _centerMassWorld - gravityBall.Position;

            double force = GRAVITATIONALCONSTANT * (gravityBall.Mass * _ship.Mass) / gravityLink.GetMagnitudeSquared();

            gravityLink.BecomeUnitVector();
            gravityLink.Multiply(force);

            gravityBall.ExternalForce.Add(gravityLink);

            gravityLink.Multiply(-1d);

            _ship.ExternalForce.Add(gravityLink);

            gravityBall.TimerTestPosition(elapsedTime);
            gravityBall.TimerFinish();
        }
Exemplo n.º 6
0
        private void StoreMouseMove(int x, int y)
        {
            MyVector safe = new MyVector();

            safe.X = UtilityCore.GetScaledValue(_multiplier * -1d, _multiplier, 0, this.Width, x);
            safe.Y = UtilityCore.GetScaledValue(_multiplier * -1d, _multiplier, 0, this.Height, y);

            double safeMultiplier = _multiplier * SAFEPERCENT;          // I don't want to butt up against the multiplier, or store value will increase it on me

            if (safe.GetMagnitudeSquared() > safeMultiplier * safeMultiplier)
            {
                safe.BecomeUnitVector();
                safe.Multiply(safeMultiplier);
            }

            StoreNewValue(safe.X, safe.Y, 0d);
        }
Exemplo n.º 7
0
        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            _prevRadians    = _currentRadians;
            _currentRadians = Utility3D.GetDegreesToRadians(trackBar1.Value);

            #region Draw Current

            Graphics graphics = pictureBox2.CreateGraphics();
            graphics.Clear(pictureBox1.BackColor);

            DrawVector(graphics, _sphere.Position, _sphere.Position + (_sphere.DirectionFacing.Standard * 100d), Color.White);
            DrawVector(graphics, _sphere.Position, _sphere.Position + (_sphere.DirectionFacing.Orth * 100d), Color.Silver);

            MyVector rotatedOffset = _sphere.Rotation.GetRotatedVector(_offset, true);

            DrawVector(graphics, _sphere.Position, _sphere.Position + rotatedOffset, Color.Orange);
            DrawDot(graphics, _sphere.Position + rotatedOffset, 3, Color.Gold);

            #endregion

            double radians = _currentRadians - _prevRadians;

            if (radOffset.Checked)
            {
                // Remember where the offset is in world coords
                MyVector offsetRotated = _sphere.Rotation.GetRotatedVector(_offset, true);
                MyVector offsetWorld   = _sphere.Position + offsetRotated;

                DrawDot(graphics, offsetWorld, 5, Color.DodgerBlue);

                // Get the opposite of the local offset
                MyVector posRelativeToOffset = offsetRotated.Clone();
                posRelativeToOffset.Multiply(-1d);

                // Rotate the center of position around the center of mass
                posRelativeToOffset.RotateAroundAxis(_rotationAxis, radians);

                // Now figure out the new center of position
                _sphere.Position.X = offsetWorld.X + posRelativeToOffset.X;
                _sphere.Position.Y = offsetWorld.Y + posRelativeToOffset.Y;
                _sphere.Position.Z = offsetWorld.Z + posRelativeToOffset.Z;
            }

            _sphere.RotateAroundAxis(_rotationAxis, radians);
        }
Exemplo n.º 8
0
        private void ResetFieldSprtInOut(double direction)
        {
            // Init the grid
            _grid = new MyVector[_squaresPerSideX * _squaresPerSideY];

            foreach (MyVector center in GetGridCenters())
            {
                // Get the local position
                MyVector localPosition = center - _position;

                // Turn that into a constant length, pointing in or out
                MyVector fieldLine = localPosition.Clone();
                fieldLine.BecomeUnitVector();
                fieldLine.Multiply(_strength * direction);

                // Store it
                _grid[GetIndexForLocalPosition(localPosition)] = fieldLine;
            }
        }
Exemplo n.º 9
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.º 10
0
        private void DrawShip(List <MyVector[]> thrustLines)
        {
            // Fill Circle
            Brush brushShip = new SolidBrush(Color.FromArgb(100, Color.LimeGreen));

            pictureBox1.FillCircle(brushShip, _ship.Sphere.Position, _ship.Sphere.Radius);

            brushShip.Dispose();

            // Draw direction facing
            MyVector dirFacing = _ship.Sphere.DirectionFacing.Standard.Clone();

            dirFacing.BecomeUnitVector();
            dirFacing.Multiply(_ship.Sphere.Radius);
            dirFacing.Add(_ship.Sphere.Position);

            pictureBox1.DrawLine(Color.White, 4, _ship.Sphere.Position, dirFacing);

            // Draw an edge
            pictureBox1.DrawCircle(Color.Black, 25d, _ship.Sphere.Position, _ship.Sphere.Radius);

            // Thrust Lines
            foreach (MyVector[] thrustPair in thrustLines)
            {
                MyVector thrustStart = _ship.TorqueBall.Rotation.GetRotatedVector(thrustPair[0], true);
                thrustStart.Add(_ship.TorqueBall.Position);

                MyVector thrustStop = thrustPair[1] * -250d;
                thrustStop.Add(thrustPair[0]);
                thrustStop = _ship.TorqueBall.Rotation.GetRotatedVector(thrustStop, true);
                thrustStop.Add(_ship.TorqueBall.Position);

                pictureBox1.DrawLine(Color.Coral, 40d, thrustStart, thrustStop);
            }

            // Thrusters
            DrawThruster(_shipThrusterOffset_BottomRight);
            DrawThruster(_shipThrusterOffset_BottomLeft);
            DrawThruster(_shipThrusterOffset_TopRight);
            DrawThruster(_shipThrusterOffset_TopLeft);
        }
Exemplo n.º 11
0
        public void Draw()
        {
            if (_type == ShipTypeQual.None)
            {
                return;
            }

            // Fill Circle
            _picturebox.FillCircle(Color.FromArgb(100, Color.LimeGreen), _ship.Sphere.Position, _ship.Sphere.Radius);

            // Draw direction facing
            MyVector dirFacing = _ship.Sphere.DirectionFacing.Standard.Clone();

            dirFacing.BecomeUnitVector();
            dirFacing.Multiply(_ship.Sphere.Radius);
            dirFacing.Add(_ship.Sphere.Position);

            _picturebox.DrawLine(Color.White, 4, _ship.Sphere.Position, dirFacing);

            // Draw an edge
            _picturebox.DrawCircle(Color.Black, 25d, _ship.Sphere.Position, _ship.Sphere.Radius);

            #region Thrust Lines

            foreach (MyVector[] thrustPair in _thrustLines)
            {
                MyVector thrustStart = _ship.Ball.Rotation.GetRotatedVector(thrustPair[0], true);
                thrustStart.Add(_ship.Ball.Position);

                MyVector thrustStop = thrustPair[1] * -250d;
                thrustStop.Add(thrustPair[0]);
                thrustStop = _ship.Ball.Rotation.GetRotatedVector(thrustStop, true);
                thrustStop.Add(_ship.Ball.Position);

                _picturebox.DrawLine(Color.Coral, 40d, thrustStart, thrustStop);
            }

            #endregion
            #region Tractor Effect

            if (_isQPressed || _isQLocked || _isEPressed || _isELocked)
            {
                foreach (TractorBeamCone tractor in _tractorBeams)
                {
                    // Figure out the cone tip location
                    MyVector tractorStart = _ship.Ball.Rotation.GetRotatedVector(tractor.Offset, true);
                    tractorStart.Add(_ship.Ball.Position);

                    // Figure out how bright to draw it
                    int alpha = 0;
                    if (_powerLevel == 1)
                    {
                        alpha = 15;
                    }
                    else if (_powerLevel == 2)
                    {
                        alpha = 30;
                    }
                    else if (_powerLevel == 3)
                    {
                        alpha = 60;
                    }
                    else //if (_powerLevel == 4)
                    {
                        alpha = 128;
                    }

                    // Draw Cone
                    if ((_isQPressed || _isQLocked) && (_isEPressed || _isELocked))
                    {
                        _picturebox.FillPie(Color.FromArgb(alpha, Color.White), tractorStart, tractor.MaxDistance, _ship.Ball.DirectionFacing.Standard, tractor.SweepAngle);
                    }
                    else if (_isQPressed || _isQLocked)
                    {
                        _picturebox.FillPie(Color.FromArgb(alpha, Color.Pink), tractorStart, tractor.MaxDistance, _ship.Ball.DirectionFacing.Standard, tractor.SweepAngle);
                    }
                    else if (_isEPressed || _isELocked)
                    {
                        _picturebox.FillPie(Color.FromArgb(alpha, Color.LightSkyBlue), tractorStart, tractor.MaxDistance, _ship.Ball.DirectionFacing.Standard, tractor.SweepAngle);
                    }
                }
            }

            #endregion
            #region Gun Effect



            #endregion

            #region Thrusters

            if (_type == ShipTypeQual.Ball)
            {
                DrawAttatchment(new MyVector(0, 0, 0), AttatchementType.Thruster);
            }
            else if (_type == ShipTypeQual.SolidBall)
            {
                DrawAttatchment(_thrusterOffset_BottomRight, AttatchementType.Thruster);
                DrawAttatchment(_thrusterOffset_BottomLeft, AttatchementType.Thruster);
                DrawAttatchment(_thrusterOffset_TopRight, AttatchementType.Thruster);
                DrawAttatchment(_thrusterOffset_TopLeft, AttatchementType.Thruster);
            }

            #endregion
            #region Gun

            DrawAttatchment(_cannon.Barrels[0].Offset, AttatchementType.Cannon);

            foreach (ProjectileWeapon weapon in _machineGuns)
            {
                DrawAttatchment(weapon.Barrels[0].Offset, AttatchementType.MachineGun);
            }

            #endregion
            #region Tractor Beam

            // This needs to go after thrusters and guns, because the tractor is smaller, and the ball has everything at zero
            foreach (TractorBeamCone tractor in _tractorBeams)
            {
                DrawAttatchment(tractor.Offset, AttatchementType.Tractor);
            }

            #endregion
        }
Exemplo n.º 12
0
        private List <Interaction> GetInteractions_Static(out double totalForce, MyVector centerWorld, MyVector dirFacingWorld)
        {
            totalForce = 0d;
            List <Interaction> retVal = new List <Interaction>();


            // This is only used for left/right modes (lazy initialization)
            AngularVelocityInfo angularInfo = null;


            // Scan for objects in my path
            foreach (BallBlip blip in FindBlipsInCone(centerWorld, dirFacingWorld))
            {
                // Get a vector from me to the ball
                MyVector lineBetween = blip.Ball.Position - centerWorld;
                double   distance    = lineBetween.GetMagnitude();

                switch (_mode)
                {
                case BeamMode.PushPull:
                    #region Push Pull

                    if (!Utility3D.IsNearZero(distance))
                    {
                        lineBetween.BecomeUnitVector();
                        lineBetween.Multiply(-1);

                        double relativeVelocity = MyVector.Dot(lineBetween, blip.Ball.Velocity - _ship.Ball.Velocity);

                        // Figure out how much force is required to make this relative velocity equal zero
                        double force = relativeVelocity * blip.Ball.Mass;       // Velocity * Mass is impulse force

                        // See if force needs to be limited by the tractor's max force
                        double maxForce = UtilityCore.GetScaledValue(_forceAtZero, _forceAtMax, 0d, _maxDistance, distance);
                        if (Math.Abs(force) > maxForce)
                        {
                            if (force > 0d)
                            {
                                force = maxForce;
                            }
                            else
                            {
                                force = maxForce * -1d;
                            }
                        }

                        // Add to results
                        retVal.Add(new Interaction(blip, lineBetween, force));
                        totalForce += Math.Abs(force);
                    }

                    #endregion
                    break;

                case BeamMode.LeftRight:
                    #region Left Right

                    // Only do something if the lines aren't sitting directly on top of each other (even if they want to repel,
                    // I'd be hesitant to just repel in any random direction)
                    if (!Utility3D.IsNearValue(MyVector.Dot(lineBetween, dirFacingWorld, true), 1d))
                    {
                        // Figure out how fast the ship is spinning where the blip is
                        MyVector dirToCenterLine;
                        MyVector spinVelocity = GetSpinVelocityAtPoint(ref angularInfo, out dirToCenterLine, dirFacingWorld, lineBetween, blip.Ball.Position);

                        // Figure out the relative velocity (between blip and my spin)
                        double relativeVelocity1 = MyVector.Dot(dirToCenterLine, blip.Ball.Velocity - spinVelocity);

                        // Figure out how much force is required to make this relative velocity equal zero
                        double force1 = relativeVelocity1 * blip.Ball.Mass;       // Velocity * Mass is impulse force

                        // See if force needs to be limited by the tractor's max force
                        double maxForce1 = UtilityCore.GetScaledValue(_forceAtZero, _forceAtMax, 0d, _maxDistance, distance);
                        if (Math.Abs(force1) > maxForce1)
                        {
                            if (force1 > 0d)
                            {
                                force1 = maxForce1;
                            }
                            else
                            {
                                force1 = maxForce1 * -1d;
                            }
                        }

                        // Add to results
                        retVal.Add(new Interaction(blip, dirToCenterLine, force1));
                        totalForce += force1;
                    }

                    #endregion
                    break;

                default:
                    throw new ApplicationException("Unknown BeamMode: " + _mode.ToString());
                }
            }

            // Exit Function
            return(retVal);
        }
Exemplo n.º 13
0
        public void Timer()
        {
            if (!(_active && _cursorInMap))
            {
                return;
            }

            _accelLines.Clear();
            _accelColors.Clear();

            Ball   ball;
            double force;

            foreach (RadarBlip blip in _map.GetAllBlips())
            {
                #region Check for exempt blips

                if (blip.Token == _cursorBlip.Token)
                {
                    continue;
                }

                if (blip.CollisionStyle != CollisionStyle.Standard)
                {
                    continue;
                }

                if (!(blip is BallBlip))
                {
                    continue;
                }

                #endregion

                #region Pull Apart

                if (_map.CollisionHandler.IsColliding(blip, _cursorBlip) == CollisionDepth.Penetrating)
                {
                    _map.CollisionHandler.PullItemsApartInstant(blip, _cursorBlip);
                }

                #endregion

                ball = ((BallBlip)blip).Ball;

                #region Calculate Force

                // Apply Force (ball to mouse)
                MyVector ballToMouse = _curMousePoint - ball.Position;

                double distanceSquared = ballToMouse.GetMagnitudeSquared();

                // Force = G * (M1*M2) / R^2
                force = GRAVITATIONALCONSTANT * (ball.Mass / distanceSquared);

                if (_isMouseDown == MouseButtonDown.Left)
                {
                    force *= 10d;
                }
                else if (_isMouseDown == MouseButtonDown.Right)
                {
                    force *= -2d;
                }

                ballToMouse.BecomeUnitVector();
                ballToMouse.Multiply(force);

                ball.ExternalForce.Add(ballToMouse);

                #endregion

                #region Store Accel Line

                double acceleration = force / ball.Mass;
                double absAccel     = Math.Abs(acceleration);

                if (absAccel > .05d)            // otherwise, it's too faint to see anyway
                {
                    _accelLines.Add(ball);

                    int alpha = Convert.ToInt32((absAccel - .05d) * (255d / .95d));
                    if (alpha < 0)
                    {
                        alpha = 0;
                    }
                    if (alpha > 255)
                    {
                        alpha = 255;
                    }

                    if (acceleration > 0d)
                    {
                        _accelColors.Add(Color.FromArgb(alpha, _attractColor));
                    }
                    else
                    {
                        _accelColors.Add(Color.FromArgb(alpha, _repelColor));
                    }
                }

                #endregion
            }
        }
Exemplo n.º 14
0
        private void ApplyColor(int start, int end, int y)
        {
            Parallel.For(start, end + 1, i =>
            {
                MyVector N = new MyVector(0, 0, 1);
                MyVector L = new MyVector(0, 0, 1);
                MyVector D = new MyVector(0, 0, 0);
                double R, G, B;
                if (lightSourceConstColorRB.Checked)
                {
                    R = (lightColorPbox.BackColor.R / 255.0);
                    G = (lightColorPbox.BackColor.G / 255.0);
                    B = (lightColorPbox.BackColor.B / 255.0);
                }
                else
                {
                    MyVector rR     = new MyVector(w / 2, h / 2, z).Normalize();
                    MyVector RPoint = new MyVector(i, y, z).Normalize();
                    MyVector rG     = new MyVector(w / 2, -h / 2, z).Normalize();
                    MyVector GPoint = new MyVector(w - i, -y, z).Normalize();
                    MyVector rB     = new MyVector(0, h / 2, z).Normalize();
                    MyVector BPoint = new MyVector(w / 2 - i, h - y, z).Normalize();
                    double cosR     = Math4Vectors.Cos(rR, RPoint);
                    double cosG     = Math4Vectors.Cos(rG, GPoint);
                    double cosB     = Math4Vectors.Cos(rB, BPoint);
                    R = Math.Pow(cosR, 20);
                    G = Math.Pow(cosG, 20);
                    B = Math.Pow(cosB, 10);
                }
                //Kolor obiektu
                if (ocTextureRB.Checked)
                {
                    Color toSet = TextureMap.GetPixel(i % (TextureMap.Width - 1) + 1, y % (TextureMap.Height - 1) + 1);
                    R          *= (toSet.R / 255.0);
                    G          *= (toSet.G / 255.0);
                    B          *= (toSet.B / 255.0);
                }
                else
                {
                    R *= (constObjectColor.R / 255.0);
                    G *= (constObjectColor.G / 255.0);
                    B *= (constObjectColor.B / 255.0);
                }
                //N
                if (normalVectorConstRB.Checked == false)
                {
                    Color fromNormal = NormalMap.GetPixel(i % (NormalMap.Width - 1) + 1, y % (NormalMap.Height - 1) + 1);
                    N = new MyVector(fromNormal.R, fromNormal.G, fromNormal.B, true);
                }
                #region CZĘŚĆ LABORATORYJNA
                if (normalMapFromFunctionRB.Checked)
                {
                    double a = (double)aToFunctionUpDown.Value / 1000;
                    double b = (double)bToFunctionUpDown.Value / 1000;
                    N        = new MyVector(i, y, (1 + fXY(a, b, i, y, t)) * 128, true);
                }
                #endregion
                //D
                if (disturbanceNoneRB.Checked == false)
                {
                    Color Xplus      = HeightMap.GetPixel((i + 1) % (HeightMap.Width - 1) + 1, y % (HeightMap.Height - 1) + 1);
                    Color Yplus      = HeightMap.GetPixel(i % (HeightMap.Width - 1) + 1, (y + 1) % (HeightMap.Height - 1) + 1);
                    Color fromHeight = HeightMap.GetPixel(i % (HeightMap.Width - 1) + 1, y % (HeightMap.Height - 1) + 1);
                    int dhX          = (Xplus.R + Xplus.G + Xplus.B - fromHeight.R - fromHeight.G - fromHeight.B) / 3;
                    int dhY          = (Yplus.R + Yplus.G + Yplus.B - fromHeight.R - fromHeight.G - fromHeight.B) / 3;
                    MyVector T       = new MyVector(1, 0, -(Xplus.R + Xplus.G + Xplus.B));
                    MyVector Bb      = new MyVector(0, 1, -(Yplus.R + Yplus.G + Yplus.B));
                    D = Math4Vectors.Add(T.Multiply(dhX), Bb.Multiply(dhY));
                }
                //L
                if (constLVectorRB.Checked == false)
                {
                    L = new MyVector(circlePoint.X - i, -(circlePoint.Y - y), z).Normalize();
                }
                //Cos(N',L)
                var Nprim  = Math4Vectors.Add(N, D);
                double Cos = Math4Vectors.Cos(Nprim, L);
                R         *= Cos * 255;
                G         *= Cos * 255;
                B         *= Cos * 255;

                if (R > 255)
                {
                    R = 255;
                }
                if (R < 0)
                {
                    R = 0;
                }
                if (G > 255)
                {
                    G = 255;
                }
                if (G < 0)
                {
                    G = 0;
                }
                if (B > 255)
                {
                    B = 255;
                }
                if (B < 0)
                {
                    B = 0;
                }
                bitmap.SetPixel(i, y, Color.FromArgb((int)R, (int)G, (int)B));
            });
        }
Exemplo n.º 15
0
        private void timer1_Tick(object sender, EventArgs e)
        {
            const double ELAPSEDTIME        = .1d;
            const double MAXVELOCITY        = 100d;
            const double MAXVELOCITYSQUARED = MAXVELOCITY * MAXVELOCITY;

            bool         drawAccel   = chkDrawAccel.Checked;
            List <Ball>  accelLines  = new List <Ball>();
            List <Color> accelColors = new List <Color>();
            double       force;

            // Tell the balls to move
            //foreach (Ball ball in _balls)
            for (int ballCntr = 0; ballCntr < _balls.Count; ballCntr++)
            {
                _balls[ballCntr].PrepareForNewTimerCycle();

                if (_mouseLocation != null)
                {
                    #region Apply Force (ball to mouse)

                    MyVector ballToMouse = _mouseLocation - _balls[ballCntr].Position;

                    double distanceSquared = ballToMouse.GetMagnitudeSquared();

                    ballToMouse.BecomeUnitVector();
                    // Force = G * (M1*M2) / R^2
                    force = _gravitationalConstant * (_balls[ballCntr].Mass / distanceSquared);
                    ballToMouse.Multiply(force);

                    _balls[ballCntr].ExternalForce.Add(ballToMouse);

                    if (drawAccel)
                    {
                        #region Store Accel Line

                        double acceleration = force / _balls[ballCntr].Mass;

                        if (acceleration > .05d)
                        {
                            accelLines.Add(_balls[ballCntr]);

                            if (acceleration > 1d)
                            {
                                accelColors.Add(Color.Yellow);
                            }
                            else
                            {
                                int color = Convert.ToInt32((acceleration - .05d) * (255d / .95d));
                                if (color < 0)
                                {
                                    color = 0;
                                }
                                if (color > 255)
                                {
                                    color = 255;
                                }
                                accelColors.Add(Color.FromArgb(color, color, 0));
                            }
                        }

                        #endregion
                    }

                    #endregion
                }

                _balls[ballCntr].TimerTestPosition(ELAPSEDTIME);
                _balls[ballCntr].TimerFinish();
                _balls[ballCntr].Velocity.Z = 0;
                _balls[ballCntr].Position.Z = 0;
                if (_balls[ballCntr].Velocity.GetMagnitudeSquared() > MAXVELOCITYSQUARED)
                {
                    _balls[ballCntr].Velocity.BecomeUnitVector();
                    _balls[ballCntr].Velocity.Multiply(MAXVELOCITY);
                }

                _ballTails[ballCntr].AddPoint(_balls[ballCntr].Position.Clone());
            }

            // Clear the view
            ClearPictureBox();

            // Draw the force lines
            if (accelLines.Count > 0)
            {
                for (int lineCntr = 0; lineCntr < accelLines.Count; lineCntr++)
                {
                    DrawVector(_mouseLocation, accelLines[lineCntr].Position, accelColors[lineCntr]);
                }
            }

            if (chkDrawTail.Checked)
            {
                foreach (Trail tail in _ballTails)
                {
                    tail.Draw(_graphics);
                }
            }

            // Draw the balls
            for (int ballCntr = 0; ballCntr < _balls.Count; ballCntr++)
            {
                DrawBall(_balls[ballCntr], _ballColors[ballCntr]);
            }

            BlitImage();
        }
Exemplo n.º 16
0
        private void contextMenuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (e.ClickedItem == displayItem1)
            {
                // Do nothing
            }
            else if (e.ClickedItem == btnNormalize)
            {
                #region Normalize

                if (_vector.IsZero)
                {
                    MessageBox.Show("The vector is zero length.  It can't be normalized.", "Context Menu Click", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    StoreNewValue(MyVector.BecomeUnitVector(_vector));
                }

                #endregion
            }
            else if (e.ClickedItem == btnMaximize)
            {
                #region Maximize

                if (_vector.IsZero)
                {
                    MessageBox.Show("The vector is zero length.  It can't be maximized.", "Context Menu Click", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    MyVector newVect = MyVector.BecomeUnitVector(_vector);
                    newVect.Multiply(_multiplier);

                    StoreNewValue(newVect);
                }

                #endregion
            }
            else if (e.ClickedItem == btnRandom)
            {
                StoreNewValue(Utility3D.GetRandomVectorSpherical2D(_multiplier * SAFEPERCENT));         // go under just to be safe
            }
            else if (e.ClickedItem == btnNegate)
            {
                StoreNewValue(_vector.X * -1d, _vector.Y * -1d, _vector.Z * -1d);
            }
            else if (e.ClickedItem == btnZero)
            {
                StoreNewValue(0, 0, 0);
            }
            else if (e.ClickedItem == btnZeroX)
            {
                StoreNewValue(0, _vector.Y, _vector.Z);
            }
            else if (e.ClickedItem == btnZeroY)
            {
                StoreNewValue(_vector.X, 0, _vector.Z);
            }
            else if (e.ClickedItem == btnZeroZ)
            {
                StoreNewValue(_vector.X, _vector.Y, 0);
            }
            else if (e.ClickedItem == btnShowToolTip)
            {
                this.ShowToolTip = !btnShowToolTip.Checked;             // note: I turned off CheckOnClick (with that on, I got a feedback loop, and it kept negating itself)
            }
            else if (e.ClickedItem is ToolStripSeparator)
            {
                // Do Nothing
            }
            else
            {
                MessageBox.Show("Menu item is unknown", "Context Menu Click", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Exemplo n.º 17
0
        private void timer2_Tick(object sender, EventArgs e)
        {
            const double GRAVITY       = 200;
            const double FORWARDFORCE  = 400;
            const double BACKWARDFORCE = 150;
            const double TURNRADIANS   = .06;
            const double ELAPSEDTIME   = .1;

            #region Color PictureBoxes

            if (_isUpPressed)
            {
                pctUp.BackColor = SystemColors.ControlDark;
            }
            else
            {
                pctUp.BackColor = SystemColors.Control;
            }

            if (_isDownPressed)
            {
                pctDown.BackColor = SystemColors.ControlDark;
            }
            else
            {
                pctDown.BackColor = SystemColors.Control;
            }

            if (_isLeftPressed)
            {
                pctLeft.BackColor = SystemColors.ControlDark;
            }
            else
            {
                pctLeft.BackColor = SystemColors.Control;
            }

            if (_isRightPressed)
            {
                pctRight.BackColor = SystemColors.ControlDark;
            }
            else
            {
                pctRight.BackColor = SystemColors.Control;
            }

            #endregion

            #region Do Physics

            _ship.PrepareForNewTimerCycle();

            if (_isUpPressed)
            {
                MyVector force = _ship.OriginalDirectionFacing.Standard.Clone();
                force.BecomeUnitVector();
                force.Multiply(FORWARDFORCE);
                _ship.InternalForce.Add(force);
            }

            if (_isDownPressed)
            {
                MyVector force = _ship.OriginalDirectionFacing.Standard.Clone();
                force.BecomeUnitVector();
                force.Multiply(BACKWARDFORCE * -1);
                _ship.InternalForce.Add(force);
            }

            if (_isLeftPressed)
            {
                _ship.RotateAroundAxis(new MyVector(0, 0, 1), TURNRADIANS * -1);
            }

            if (_isRightPressed)
            {
                _ship.RotateAroundAxis(new MyVector(0, 0, 1), TURNRADIANS);
            }

            if (chkApplyGravityShip.Checked)
            {
                _ship.ExternalForce.Add(new MyVector(0, GRAVITY, 0));
            }

            _ship.TimerTestPosition(ELAPSEDTIME);
            _ship.TimerFinish();

            #endregion

            #region Draw Ship

            ClearPictureBox();

            // Ball
            DrawBall(_ship, Color.DarkSeaGreen, Color.Chartreuse, Color.DarkOliveGreen);

            BlitImage();

            #endregion
        }
Exemplo n.º 18
0
        private void timer1_Tick(object sender, EventArgs e)
        {
            const double THRUSTERFORCE = 80;

            List <MyVector[]> thrustLines = new List <MyVector[]>();

            #region Physics

            _map.PrepareForNewTimerCycle();

            switch (_gravityMode)
            {
            case GravityMode.None:
                break;

            case GravityMode.Down:
                DoGravityDown();
                break;

            case GravityMode.EachOther:
                DoGravityEachOther();
                break;
            }

            if (_ship != null)
            {
                #region Ship Thrusters

                if (_isUpPressed)
                {
                    timer1_TickSprtApplyThrust(thrustLines, _shipThrusterOffset_TopRight, new MyVector(0, 1, 0), THRUSTERFORCE);        // down
                    timer1_TickSprtApplyThrust(thrustLines, _shipThrusterOffset_TopLeft, new MyVector(0, 1, 0), THRUSTERFORCE);         // s
                }

                if (_isWPressed)
                {
                    timer1_TickSprtApplyThrust(thrustLines, _shipThrusterOffset_TopRight, new MyVector(0, 1, 0), THRUSTERFORCE * 10d);          // down
                    timer1_TickSprtApplyThrust(thrustLines, _shipThrusterOffset_TopLeft, new MyVector(0, 1, 0), THRUSTERFORCE * 10d);           // s
                }

                if (_isDownPressed)
                {
                    timer1_TickSprtApplyThrust(thrustLines, _shipThrusterOffset_BottomRight, new MyVector(0, -1, 0), THRUSTERFORCE);            // up
                    timer1_TickSprtApplyThrust(thrustLines, _shipThrusterOffset_BottomLeft, new MyVector(0, -1, 0), THRUSTERFORCE);             // w
                }

                if (_isSPressed)
                {
                    timer1_TickSprtApplyThrust(thrustLines, _shipThrusterOffset_BottomRight, new MyVector(0, -1, 0), THRUSTERFORCE * 10d);              // up
                    timer1_TickSprtApplyThrust(thrustLines, _shipThrusterOffset_BottomLeft, new MyVector(0, -1, 0), THRUSTERFORCE * 10d);               // w
                }

                if (_isLeftPressed)
                {
                    timer1_TickSprtApplyThrust(thrustLines, _shipThrusterOffset_BottomRight, new MyVector(0, -1, 0), THRUSTERFORCE);    // up
                    timer1_TickSprtApplyThrust(thrustLines, _shipThrusterOffset_TopLeft, new MyVector(0, 1, 0), THRUSTERFORCE);         // s
                }

                if (_isRightPressed)
                {
                    timer1_TickSprtApplyThrust(thrustLines, _shipThrusterOffset_TopRight, new MyVector(0, 1, 0), THRUSTERFORCE);                // down
                    timer1_TickSprtApplyThrust(thrustLines, _shipThrusterOffset_BottomLeft, new MyVector(0, -1, 0), THRUSTERFORCE);             // w
                }

                #endregion
            }

            DoVectorField();

            Collision[] collisions = _map.Timer(_elapsedTime);

            #endregion

            #region Draw

            // Turn the collision list into a list of tokens
            List <long> collisionTokens = GetCollisionTokens(collisions);

            pictureBox1.PrepareForNewDraw();

            #region Vector Field

            if (_vectorField.FieldMode != VectorField2DMode.None)
            {
                List <MyVector[]> drawLines, fieldLines;
                _vectorField.GetDrawLines(out drawLines, out fieldLines);

                foreach (MyVector[] drawLine in drawLines)
                {
                    pictureBox1.DrawLine(Color.DimGray, 1, drawLine[0], drawLine[1]);
                }

                foreach (MyVector[] fieldLine in fieldLines)
                {
                    pictureBox1.DrawArrow(Color.Chartreuse, 1, fieldLine[0], fieldLine[0] + fieldLine[1]);
                }
            }

            #endregion

            RadarBlip[] blips = _map.GetAllBlips();

            Brush brushBall       = new SolidBrush(Color.FromArgb(160, Color.Gold));
            Brush brushTorqueBall = new SolidBrush(Color.FromArgb(160, Color.RoyalBlue));
            Brush brushRed        = new SolidBrush(Color.FromArgb(160, Color.Tomato));

            Brush curBrush;

            for (int blipCntr = 0; blipCntr < blips.Length; blipCntr++)
            {
                if (blips[blipCntr].Sphere is RigidBody)
                {
                    #region Draw Rigid Body

                    DrawRigidBody(blips[blipCntr].Sphere as RigidBody, Color.MediumSlateBlue, Color.White, _drawCollisionsRed && collisionTokens.Contains(blips[blipCntr].Token));

                    #endregion
                }
                else if (_ship != null && blips[blipCntr].Token == _ship.Token)
                {
                    DrawShip(thrustLines);
                }
                else
                {
                    #region Draw Ball

                    bool isSolidBall = blips[blipCntr].Sphere is SolidBall;

                    // Fill the circle
                    if (_drawCollisionsRed && collisionTokens.Contains(blips[blipCntr].Token))
                    {
                        curBrush = brushRed;
                    }
                    else if (isSolidBall)
                    {
                        curBrush = brushTorqueBall;
                    }
                    else
                    {
                        curBrush = brushBall;
                    }

                    pictureBox1.FillCircle(curBrush, blips[blipCntr].Sphere.Position, blips[blipCntr].Sphere.Radius);

                    // Draw direction facing
                    if (isSolidBall)
                    {
                        MyVector dirFacing = blips[blipCntr].Sphere.DirectionFacing.Standard.Clone();
                        dirFacing.BecomeUnitVector();
                        dirFacing.Multiply(blips[blipCntr].Sphere.Radius);
                        dirFacing.Add(blips[blipCntr].Sphere.Position);

                        pictureBox1.DrawLine(Color.White, 2, blips[blipCntr].Sphere.Position, dirFacing);
                    }

                    // Draw an edge
                    pictureBox1.DrawCircle(Color.Black, 1d, blips[blipCntr].Sphere.Position, blips[blipCntr].Sphere.Radius);

                    #endregion
                }
            }

            brushBall.Dispose();
            brushTorqueBall.Dispose();
            brushRed.Dispose();

            pictureBox1.FinishedDrawing();

            #endregion
        }