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; } }
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 } } }
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); }
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); }
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); }
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; } }
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); }
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); }
private MyVector GetSpinVelocityAtPoint(ref AngularVelocityInfo angularInfo, out MyVector dirToCenterLine, MyVector dirFacingWorld, MyVector lineBetween, MyVector blipPosition) { // Get a line that's orthogonal to lineBetween, and always points toward the dirFacingWorld vector dirToCenterLine = MyVector.Cross(MyVector.Cross(lineBetween, dirFacingWorld), lineBetween); dirToCenterLine.BecomeUnitVector(); if (angularInfo == null) { #region Cache Angular Velocity angularInfo = new AngularVelocityInfo(); if (_ship.TorqueBall != null) { angularInfo.AngularVelocity = _ship.TorqueBall.AngularVelocity.GetMagnitude(); angularInfo.SpinDirection = MyVector.Cross(_ship.TorqueBall.AngularVelocity, _ship.TorqueBall.DirectionFacing.Standard); angularInfo.SpinDirection.BecomeUnitVector(); angularInfo.CenterMass = _ship.TorqueBall.Rotation.GetRotatedVector(_ship.TorqueBall.CenterOfMass, true); angularInfo.CenterMass.Add(_ship.TorqueBall.Position); } else { angularInfo.SpinDirection = dirToCenterLine.Clone(); angularInfo.AngularVelocity = 0d; angularInfo.CenterMass = _ship.Ball.Position.Clone(); } #endregion } // Get the line between the blip and the center of mass MyVector lineBetweenCM = blipPosition - angularInfo.CenterMass; // Figure out my velocity of spin where the blip is return(angularInfo.SpinDirection * (angularInfo.AngularVelocity * lineBetweenCM.GetMagnitude())); }
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(); }
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 }
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); }
private List <Interaction> GetInteractions_Standard(out double totalForce, MyVector centerWorld, MyVector dirFacingWorld) { totalForce = 0d; List <Interaction> retVal = new List <Interaction>(); AngularVelocityInfo tractorAngularInfo = null; // Scan for objects in my path foreach (BallBlip blip in FindBlipsInCone(centerWorld, dirFacingWorld)) { // Get the distance MyVector lineBetween = blip.Sphere.Position - centerWorld; double distance = lineBetween.GetMagnitude(); // Figure out the force to apply double force = UtilityCore.GetScaledValue(_forceAtZero, _forceAtMax, 0d, _maxDistance, distance); force *= _percent; switch (_mode) { case BeamMode.PushPull: #region Push Pull if (!Utility3D.IsNearZero(distance)) { // Turn lineBetween into a unit vector (it will be multiplied by force later) lineBetween.BecomeUnitVector(); if (_isSoft) { force = GetForceForSoft(ref tractorAngularInfo, force, lineBetween, distance, blip.Ball, dirFacingWorld); } // Add this to the return list retVal.Add(new Interaction(blip, lineBetween, force)); totalForce += Math.Abs(force); // percent is negative when in repulse mode } #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)) { // Get a line that's orthogonal to lineBetween, and always points toward the dirFacingWorld vector MyVector dirToCenterLine = MyVector.Cross(lineBetween, MyVector.Cross(lineBetween, dirFacingWorld)); dirToCenterLine.BecomeUnitVector(); // Add to the return list retVal.Add(new Interaction(blip, dirToCenterLine, force)); totalForce += Math.Abs(force); // percent is negative when in repulse mode } #endregion break; default: throw new ApplicationException("Unknown BeamMode: " + _mode.ToString()); } } // Exit Function return(retVal); }
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); } }
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 }
private bool Fire() { // Before I begin, try to grab some ammo //if(_useAmmoClip && _ammoClip.RemoveQuantity(_amtAmmoToPull, true) > 0) //{ // return false; //} // The ammo has been grabbed, bump the elapsed time structure _elapsedTime.InnerFiringRate = _firingModes[_activeFiringMode].InnerFiringRate; _elapsedTime.NumRoundsPerOuter++; // Fire each barrel foreach (Barrel barrel in _barrels) { #region Fire New Projectile // Create the position MyVector position = _ship.Ball.Position.Clone(); if (barrel.Offset != null) { position = _ship.Ball.Rotation.GetRotatedVector(barrel.Offset, true) + position; } // Create the direction facing DoubleVector dirFacing = _ship.Ball.DirectionFacing.Clone(); if (barrel.Rotation != null) { dirFacing = barrel.Rotation.GetRotatedVector(dirFacing, true); } // Make a ball Ball ball = null; if (_useBoundry) { ball = new Ball(position, dirFacing, _projectileSettings.Radius, _projectileSettings.Mass, _boundryLower, _boundryUpper); } else { ball = new Ball(position, dirFacing, _projectileSettings.Radius, _projectileSettings.Mass); } MyVector dirFacingUnit = dirFacing.Standard; dirFacingUnit.BecomeUnitVector(); // Set the velocity ball.Velocity.StoreNewValues(dirFacingUnit * _projectileSettings.Speed); ball.Velocity.Add(_ship.Ball.Velocity); // Make a projectile Projectile projectile = new Projectile(ball, _ship.Token, _projectileSettings.IgnoreOtherProjectiles, _projectileSettings.Pain, _map, _projectileSettings.Qual, TokenGenerator.NextToken()); // Set up explosion and fuse settings if (_projectileSettings.Explosion != null) { projectile.SetExplosion(_projectileSettings.Explosion.Radius, _projectileSettings.Explosion.Duration, _projectileSettings.Explosion.Force); } if (_projectileSettings.Fuse != null) { projectile.SetFuse(_projectileSettings.Fuse.Duration); } // Generate the kick if (_produceKick) { #region Kick MyVector kick = dirFacingUnit * _projectileSettings.Speed * (ball.Mass * -1d); if (_ship.TorqueBall != null) { _ship.TorqueBall.ApplyExternalForce(ball.Position - _ship.Ball.Position, kick); } else { _ship.Ball.ExternalForce.Add(kick); } #endregion } // Hand the projectile to the map _map.Add(projectile); #endregion } // Exit function return(true); }
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 }
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(); }
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 } }