private List <BallBlip> FindBlipsInCone(MyVector centerWorld, MyVector dirFacingWorld) { List <BallBlip> retVal = new List <BallBlip>(); // Cache some stuff for use inside the loop double halfSweepAngle = _sweepAngle / 2d; double maxDistSquared = _maxDistance * _maxDistance; // Scan for objects in my path foreach (RadarBlip blip in _map.GetAllBlips()) { if (blip.Token == _ship.Token) { // Can't manipulate me continue; } if (blip.CollisionStyle == CollisionStyle.Ghost) { // Ghost blips don't interact with anything continue; } if (!(blip is BallBlip)) { // The blip needs to be at least a ball continue; } MyVector lineBetween = blip.Sphere.Position - centerWorld; if (lineBetween.GetMagnitudeSquared() > maxDistSquared) { // The object is too far away continue; } MyVector rotationAxis; double rotationRadians; dirFacingWorld.GetAngleAroundAxis(out rotationAxis, out rotationRadians, lineBetween); if (rotationRadians > halfSweepAngle) { // The sweep angle is too great (not inside the cone) continue; } // It's inside the cone retVal.Add(blip as BallBlip); } // Exit Function return(retVal); }
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 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(); }
private void DoGravityEachOther() { const double GRAVITATIONALCONSTANT = .0001d; //500d; double gravitationalConstantAdjusted = GRAVITATIONALCONSTANT * _gravityMultiplier; RadarBlip[] blips = _map.GetAllBlips(); for (int outerCntr = 0; outerCntr < blips.Length - 1; outerCntr++) { if (blips[outerCntr].CollisionStyle == CollisionStyle.Ghost) { continue; } Ball ball1 = blips[outerCntr].Sphere as Ball; for (int innerCntr = outerCntr + 1; innerCntr < blips.Length; innerCntr++) { if (blips[innerCntr].CollisionStyle == CollisionStyle.Ghost) { continue; } #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 = gravitationalConstantAdjusted * (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 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 } }