public BallTester() { InitializeComponent(); //System.Windows.Forms.Integration.EnableWindowsFormsInterop(); _bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height); _graphics = Graphics.FromImage(_bitmap); // Make the ship _ship = new Ball(new MyVector(300, 300, 0), new DoubleVector(new MyVector(0, 1, 0), new MyVector(1, 0, 0)), 60, 100, new MyVector(0, 0, -1), new MyVector(pictureBox1.Width, pictureBox1.Height, 1)); _gravitationalConstant = trackBar1.Value; }
private void button1_Click(object sender, EventArgs e) { Ball ball = new Ball(new MyVector(100, 100, 0), new DoubleVector(1, 0, 0, 0, 1, 0), 10, 5); ball = new Ball(new MyVector(100, 100, 0), new DoubleVector(1, 0, 0, 0, 1, 0), 10, 5, new MyVector(-100, -100, -100), new MyVector(100, 100, 100)); }
/// <summary> /// This method will adjust the force to keep the relative velocity relativaly small /// </summary> /// <remarks> /// This method needs work. It would help if I exposed velocity and force lines to be drawn for debugging /// </remarks> private double GetForceForSoft(ref AngularVelocityInfo angularInfo, double maxForce, MyVector forceDirection, double distance, Ball ball, MyVector dirFacingWorld) { const double MINVELOCITY = 20d; double minVelocity = UtilityCore.GetScaledValue(0, MINVELOCITY, 0, _maxDistance, distance); MyVector dummy; MyVector tractorVelocity = GetSpinVelocityAtPoint(ref angularInfo, out dummy, dirFacingWorld, _offset, _ship.Ball.Position + _offset); tractorVelocity = tractorVelocity + _ship.Ball.Velocity; double relativeVelocity = MyVector.Dot(forceDirection, ball.Velocity - tractorVelocity); double retVal = maxForce; if (maxForce > 0) { #region Pulling In // Positive force means the relative velocity will need to be negative (pulling the object in) if (Utility3D.IsNearValue(relativeVelocity, minVelocity * -1d)) { // It's going the right speed. No force needed return 0; } else if (relativeVelocity < (minVelocity * -1d)) { // It's coming in too fast. Slow it down retVal = Math.Abs(relativeVelocity) - Math.Abs(minVelocity) * ball.Mass; // Velocity * Mass is impulse force } #endregion } else { #region Pushing Away // Negative force means the relative velocity will need to be positive (pushing the object away) if (Utility3D.IsNearValue(relativeVelocity, minVelocity)) { // It's going the right speed. No force needed return 0; } else if (relativeVelocity > minVelocity) { // It's going away too fast. Slow it down retVal = Math.Abs(relativeVelocity) - Math.Abs(minVelocity) * ball.Mass; // Velocity * Mass is impulse force retVal *= -1d; } //if (relativeVelocity > MINVELOCITY) //{ // // It's going fast enough, no need to apply any more force // return 0; //} // Figure out how much force is required to make this relative velocity equal MINVELOCITY //retVal = (relativeVelocity - MINVELOCITY) * ball.Mass; // Velocity * Mass is impulse force #endregion } // Cap the return the max force if (Math.Abs(retVal) > Math.Abs(maxForce)) { if (retVal > 0) { retVal = Math.Abs(maxForce); } else { retVal = Math.Abs(maxForce) * -1d; } } // Exit Function return retVal; }
/// <summary> /// This is the basic constructor. These are the only things that need to be passed in. /// </summary> public BallBlip(Ball ball, CollisionStyle collisionStyle, RadarBlipQual blipQual, long token) : this(ball, collisionStyle, blipQual, token, Guid.NewGuid()) { }
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(); }
/// <summary> /// The opacity passed in will be applied after figuring out the opacity of mode (if mode says 128, and opacity is .5, then /// the final opacity is 64) /// </summary> public void DrawBall(Ball ball, DrawMode mode, CollisionStyle collisionStyle, bool drawRed, double opacity) { Color color; #region Figure out the color if (drawRed) { color = REDCOLOR; } else { color = Color.Gold; } #endregion int finalOpacity; #region Figure out the opacity switch (mode) { case DrawMode.Building: finalOpacity = BUILDINGBALLFILLOPACTIY; break; case DrawMode.Selected: case DrawMode.Standard: finalOpacity = STANDARDBALLFILLOPACTIY; break; default: throw new ApplicationException("Unknown DrawMode: " + mode.ToString()); } finalOpacity = Convert.ToInt32(finalOpacity * opacity); if (finalOpacity < 0) { finalOpacity = 0; } else if (finalOpacity > 255) { finalOpacity = 255; } #endregion // Collision Style DrawCollisionStyle(ball.Position, ball.Radius, collisionStyle); // Fill the circle using (Brush brush = new SolidBrush(Color.FromArgb(finalOpacity, color))) { _viewer.FillCircle(brush, ball.Position, ball.Radius); } #region Draw the edge switch (mode) { case DrawMode.Building: _viewer.DrawCircle(Color.FromArgb(finalOpacity, _buildingPenColor), STANDARDOUTLINEWIDTH, ball.Position, ball.Radius); break; case DrawMode.Standard: _viewer.DrawCircle(Color.FromArgb(finalOpacity, _standardPenColor), STANDARDOUTLINEWIDTH, ball.Position, ball.Radius); break; case DrawMode.Selected: _viewer.DrawCircle_Selected(ball.Position, ball.Radius); break; } #endregion //TODO: Show Stats }
private void HurtBlip(Projectile projectile, RadarBlip blip) { #region See if a split should occur if (blip.CollisionStyle != CollisionStyle.Standard) { return; } if (!(blip is BallBlip)) { return; } BallBlip castBlip = (BallBlip)blip; double ratio = projectile.Pain / castBlip.Ball.Mass; double rand = _rand.NextDouble(); if (ratio < rand) { return; } #endregion #region Calculate Split Percents int numParts = 2 + _rand.Next(3); // between 2 and 4 parts double[] splitPercents = new double[numParts]; double remainder = 1d; for (int cntr = 0; cntr < numParts - 1; cntr++) { splitPercents[cntr] = _rand.NextDouble() * remainder; remainder -= splitPercents[cntr]; } splitPercents[numParts - 1] = remainder; #endregion #region Build Objects _map.Remove(blip.Token); foreach (double percent in splitPercents) { double size = castBlip.Ball.Mass * percent; if (size < 20) { continue; } Ball ball; if (castBlip.Ball is SolidBall) { ball = new SolidBall(castBlip.Ball.Position.Clone(), castBlip.Ball.OriginalDirectionFacing.Clone(), size, size, castBlip.Ball.Elasticity, castBlip.Ball.KineticFriction, castBlip.Ball.StaticFriction, _boundryLower, _boundryUpper); } else { ball = new Ball(castBlip.Ball.Position.Clone(), castBlip.Ball.OriginalDirectionFacing.Clone(), size, size, castBlip.Ball.Elasticity, castBlip.Ball.KineticFriction, castBlip.Ball.StaticFriction, _boundryLower, _boundryUpper); } ball.Velocity.StoreNewValues(castBlip.Ball.Velocity); //TODO: Lay them out so they aren't touching each other. The smallest ones should be closest // to the point of impact (maybe give them slightly tweaked velocities as well so they explode // outward) _map.Add(new BallBlip(ball, blip.CollisionStyle, blip.Qual, TokenGenerator.NextToken())); } #endregion }
private MyVector GetVelocityEnd(MyVector position, Ball ball) { return position + ball.Velocity; }
private void CommitObject() { Ball newObject; #region Get the base object to add if (_drawingBall != null) { newObject = _drawingBall; _drawingBall = null; } else { newObject = BuildObject(); } #endregion RadarBlipQual blipQual; #region Figure out the blipqual //TODO: Define these as constants somewhere switch (_mode) { case AddingMode.AddBall: blipQual = RadarBlipQual.BallUserDefined00; break; case AddingMode.AddSolidBall: blipQual = RadarBlipQual.BallUserDefined01; break; case AddingMode.AddRigidBody: blipQual = RadarBlipQual.BallUserDefined02; break; default: throw new ApplicationException("Unknown AddingMode: " + _mode.ToString()); } #endregion // Create a blip to contain this object BallBlip blip = new BallBlip(newObject, _newBallProps.CollisionStyle, blipQual, TokenGenerator.NextToken()); // If this is a torqueball, then it will get the angular velocity. I have to wait until now, because // the size could change during a draw (you don't see it spin during the drag anyway, because I // don't call it's timer, so there is no reason to set angular velocity before now.) StoreAngularVelocity(blip.Ball); // Add it to the map _map.Add(blip); if (_newBallProps.Temporary) { _tempObjects.Add(blip.Token); } }
void picturebox_MouseMove(object sender, MouseEventArgs e) { if (_mode == AddingMode.Inactive || !_isMouseDown) { return; } // Remember the mouse position _curMousePoint = _picturebox.GetPositionViewToWorld(new MyVector(e.X, e.Y, 0)); switch (_newBallProps.SizeMode) { case BallProps.SizeModes.Draw: ResizeDrawingObject(); break; case BallProps.SizeModes.Fixed: case BallProps.SizeModes.Random: if (Environment.TickCount - _lastCreateTime > _creationRate) { // Enough time has elapsed. Try to create a new object if (_drawingBall == null) { _drawingBall = BuildObject(); } _drawingBall.Position.StoreNewValues(_curMousePoint); // See if this will collide with anything if (!WillCollide(_drawingBall)) { CommitObject(); _lastCreateTime = Environment.TickCount; _createdBallDuringMouseDrag = true; _drawingBall = null; } } break; default: throw new ApplicationException("Unknown BallProps.SizeModes: " + _newBallProps.SizeMode.ToString()); } }
void picturebox_MouseUp(object sender, MouseEventArgs e) { if (_mode == AddingMode.Inactive) { return; } if (_isMouseDown && e.Button == MouseButtons.Left) { _isMouseDown = false; _mouseDownPoint = _picturebox.GetPositionViewToWorld(new MyVector(e.X, e.Y, 0)); if (_drawingBall == null) { _drawingBall = BuildObject(); } if (_newBallProps.SizeMode == BallProps.SizeModes.Draw) { CommitObject(); } else { _drawingBall.Position.StoreNewValues(_curMousePoint); if (!WillCollide(_drawingBall) || !_createdBallDuringMouseDrag) { // It won't collide, or nothing has been created yet CommitObject(); // I don't care about the draw rate during mouse up } } _drawingBall = null; // even if I didn't commit it, I still need to kill it now } }
void picturebox_MouseDown(object sender, MouseEventArgs e) { if (_mode == AddingMode.Inactive) { return; } if (e.Button == MouseButtons.Left) { _isMouseDown = true; _mouseDownPoint = _picturebox.GetPositionViewToWorld(new MyVector(e.X, e.Y, 0)); _curMousePoint = _mouseDownPoint.Clone(); _lastCreateTime = Environment.TickCount; _createdBallDuringMouseDrag = false; //_diminishPercent = 1d; if (_newBallProps.SizeMode == BallProps.SizeModes.Draw) { // I need to create an object now (but don't commit it to the map), so that the user can see // it while they drag the size _drawingBall = BuildObject(); } } }
private void PropsChangedSprtNew() { MyVector position = null; // Kill Existing if (_ship != null) { position = _ship.Ball.Position.Clone(); _map.Remove(_ship.Token); _ship = null; } else { position = Utility3D.GetRandomVector(_boundryLower, _boundryUpper); } _tractorBeams.Clear(); _cannon = null; _machineGuns.Clear(); #region New Ship //TODO: Listen to global props double elasticity = .75d; double kineticFriction = .75d; double staticFriction = 1d; // Build New Ball newBall; RadarBlipQual blipQual; // logic came from BallAdder.CommitObject switch (_type) { case ShipTypeQual.None: return; case ShipTypeQual.Ball: #region Ball newBall = new Ball(position, new DoubleVector(0, 1, 0, 1, 0, 0), _shipSize, UtilityCore.GetMassForRadius(_shipSize, 1d), elasticity, kineticFriction, staticFriction, _boundryLower, _boundryUpper); blipQual = RadarBlipQual.BallUserDefined00; _thrustForce = GetThrustForce(newBall.Mass); _torqueballLeftRightThrusterForce = _thrustForce; #endregion break; case ShipTypeQual.SolidBall: #region Solid Ball newBall = new SolidBall(position, new DoubleVector(0, 1, 0, 1, 0, 0), _shipSize, UtilityCore.GetMassForRadius(_shipSize, 1d), elasticity, kineticFriction, staticFriction, _boundryLower, _boundryUpper); blipQual = RadarBlipQual.BallUserDefined01; _thrustForce = GetThrustForce(newBall.Mass); _torqueballLeftRightThrusterForce = GetLeftRightThrusterMagnitude(((SolidBall)newBall).InertialTensorBody); #endregion break; default: throw new ApplicationException("Unknown ShipTypeQual: " + _type.ToString()); } newBall.RotateAroundAxis(new MyVector(0, 0, 1), Math.PI); // Finish Building _ship = new BallBlip(newBall, CollisionStyle.Standard, blipQual, _blipToken); _map.Add(_ship); #endregion if (this.CreateNewTractorBeams != null) { this.CreateNewTractorBeams(this, new EventArgs()); } #region Guns _cannon = new ProjectileWeapon(300, 150, UtilityCore.GetMassForRadius(150, 1d), 25, true, _ignoreOtherProjectiles, RadarBlipQual.Projectile, false, _map, _boundryLower, _boundryUpper); _cannon.AddBarrel(_ship.Ball.OriginalDirectionFacing.Standard.Clone()); _cannon.AddFiringMode(20); _cannon.SetProjectileExplosion(450, 2, 10000); _cannon.SeProjectileFuse(500); _cannon.SetShip(_ship); for (int cntr = 0; cntr < 2; cntr++) { ProjectileWeapon weapon = new ProjectileWeapon(30, 20, UtilityCore.GetMassForRadius(20, 1d), 100, true, _ignoreOtherProjectiles, RadarBlipQual.Projectile, false, _map, _boundryLower, _boundryUpper); weapon.AddBarrel(new MyVector(), new MyQuaternion()); weapon.AddFiringMode(2); weapon.SetProjectileExplosion(40, 2, 300); weapon.SeProjectileFuse(500); weapon.SetShip(_ship); _machineGuns.Add(weapon); } #endregion }
public Projectile(Ball ball, long shipToken, bool ignoreOtherProjectiles, double pain, SimpleMap map, RadarBlipQual blipQual, long token, Guid objectID) : base(ball, CollisionStyle.Standard, blipQual, token, objectID) { _shipToken = shipToken; _ignoreOtherProjectiles = ignoreOtherProjectiles; _pain = pain; _map = map; _map.Collisions += new CollisionsDelegate(Map_Collisions); }
public Projectile(Ball ball, long shipToken, bool ignoreOtherProjectiles, double pain, SimpleMap map, RadarBlipQual blipQual, long token) : this(ball, shipToken, ignoreOtherProjectiles, pain, map, blipQual, token, Guid.NewGuid()) { }
private void radBallType_CheckedChanged(object sender, EventArgs e) { grpTriangleZ.Visible = false; grpTriangleZ2.Visible = false; _triangle1 = null; _triangle2 = null; _polygon1 = null; _polygon2 = null; if (radBallBall.Checked || radSolidBallSolidBall.Checked) { #region Balls Ball newBall1, newBall2; if (radBallBall.Checked) { // Switch them out with a standard ball newBall1 = new Ball(_ball1.Position.Clone(), new DoubleVector(0, 1, 0, 1, 0, 0), _ball1.Radius, _ball1.Mass, _ball1.Elasticity, _ball1.KineticFriction, _ball1.StaticFriction, _boundryLower, _boundryUpper); newBall1.Velocity.StoreNewValues(_ball1.Velocity.Clone()); newBall2 = new Ball(_ball2.Position.Clone(), new DoubleVector(0, 1, 0, 1, 0, 0), _ball2.Radius, _ball2.Mass, _ball2.Elasticity, _ball2.KineticFriction, _ball2.StaticFriction, _boundryLower, _boundryUpper); newBall2.Velocity.StoreNewValues(_ball2.Velocity.Clone()); } else if (radSolidBallSolidBall.Checked) { // Switch them out with solidballs newBall1 = new SolidBall(_ball1.Position.Clone(), new DoubleVector(0, 1, 0, 1, 0, 0), _ball1.Radius, _ball1.Mass, _ball1.Elasticity, _ball1.KineticFriction, _ball1.StaticFriction, _boundryLower, _boundryUpper); newBall1.Velocity.StoreNewValues(_ball1.Velocity.Clone()); ((SolidBall)newBall1).AngularVelocity.StoreNewValues(new MyVector(0, 0, 200)); newBall2 = new SolidBall(_ball2.Position.Clone(), new DoubleVector(0, 1, 0, 1, 0, 0), _ball2.Radius, _ball2.Mass, _ball2.Elasticity, _ball2.KineticFriction, _ball2.StaticFriction, _boundryLower, _boundryUpper); newBall2.Velocity.StoreNewValues(_ball2.Velocity.Clone()); ((SolidBall)newBall2).AngularVelocity.StoreNewValues(new MyVector(0, 0, 200)); } else { MessageBox.Show("Unknown radio button", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } _ball1 = newBall1; _ball2 = newBall2; #endregion } else if (radSphereSphere.Checked) { #region Spheres // leave the balls alone? #endregion } else if (radLineTriangle.Checked || radSphereTriangle.Checked || radTriangleTriangle.Checked) { #region Triangles if (radLineTriangle.Checked) { #region Line Triangle // I will leave ball1 alone. I will use that one's velocity as the line grpTriangleZ.Visible = true; // Make the the triangle if (chkTrianglePerpendicular.Checked) { _triangle1 = new TriangleTest(_ball2.Position.Clone(), new DoubleVector(1, 0, 0, 0, 1, 0), new Triangle(0, 0, -100, 0, 100, 0, 0, -100, 100)); radPoint1Neg.Enabled = radPoint1Pos.Enabled = radPoint1Zero.Enabled = false; radPoint2Neg.Enabled = radPoint2Pos.Enabled = radPoint2Zero.Enabled = false; radPoint3Neg.Enabled = radPoint3Pos.Enabled = radPoint3Zero.Enabled = false; } else { _triangle1 = new TriangleTest(_ball2.Position.Clone(), new DoubleVector(1, 0, 0, 0, 1, 0), new Triangle(-150, -150, 0, 150, -150, 0, 0, 150, 0)); radPoint1Neg.Enabled = radPoint1Pos.Enabled = radPoint1Zero.Enabled = true; radPoint2Neg.Enabled = radPoint2Pos.Enabled = radPoint2Zero.Enabled = true; radPoint3Neg.Enabled = radPoint3Pos.Enabled = radPoint3Zero.Enabled = true; radPoint1_CheckedChanged(this, new EventArgs()); radPoint2_CheckedChanged(this, new EventArgs()); radPoint3_CheckedChanged(this, new EventArgs()); } #endregion } else if (radSphereTriangle.Checked) { #region Sphere Triangle // I will leave ball1 alone. I won't use its velocity, just radius grpTriangleZ.Visible = true; // Make the the triangle if (chkTrianglePerpendicular.Checked) { _triangle1 = new TriangleTest(_ball2.Position.Clone(), new DoubleVector(1, 0, 0, 0, 1, 0), new Triangle(0, 0, -100, 0, 100, 0, 0, -100, 100)); radPoint1Neg.Enabled = radPoint1Pos.Enabled = radPoint1Zero.Enabled = false; radPoint2Neg.Enabled = radPoint2Pos.Enabled = radPoint2Zero.Enabled = false; radPoint3Neg.Enabled = radPoint3Pos.Enabled = radPoint3Zero.Enabled = false; } else { _triangle1 = new TriangleTest(_ball2.Position.Clone(), new DoubleVector(1, 0, 0, 0, 1, 0), new Triangle(-150, -150, 0, 150, -150, 0, 0, 150, 0)); radPoint1Neg.Enabled = radPoint1Pos.Enabled = radPoint1Zero.Enabled = true; radPoint2Neg.Enabled = radPoint2Pos.Enabled = radPoint2Zero.Enabled = true; radPoint3Neg.Enabled = radPoint3Pos.Enabled = radPoint3Zero.Enabled = true; radPoint1_CheckedChanged(this, new EventArgs()); radPoint2_CheckedChanged(this, new EventArgs()); radPoint3_CheckedChanged(this, new EventArgs()); } #endregion } else if (radTriangleTriangle.Checked) { #region Triangle Triangle grpTriangleZ.Visible = true; grpTriangleZ2.Visible = true; #region Triangle1 (right side) // Make the the triangle if (chkTrianglePerpendicular.Checked) { _triangle1 = new TriangleTest(_ball2.Position.Clone(), new DoubleVector(1, 0, 0, 0, 1, 0), new Triangle(0, 0, -100, 0, 100, 0, 0, -100, 100)); radPoint1Neg.Enabled = radPoint1Pos.Enabled = radPoint1Zero.Enabled = false; radPoint2Neg.Enabled = radPoint2Pos.Enabled = radPoint2Zero.Enabled = false; radPoint3Neg.Enabled = radPoint3Pos.Enabled = radPoint3Zero.Enabled = false; } else { _triangle1 = new TriangleTest(_ball2.Position.Clone(), new DoubleVector(1, 0, 0, 0, 1, 0), new Triangle(-150, -150, 0, 150, -150, 0, 0, 150, 0)); radPoint1Neg.Enabled = radPoint1Pos.Enabled = radPoint1Zero.Enabled = true; radPoint2Neg.Enabled = radPoint2Pos.Enabled = radPoint2Zero.Enabled = true; radPoint3Neg.Enabled = radPoint3Pos.Enabled = radPoint3Zero.Enabled = true; radPoint1_CheckedChanged(this, new EventArgs()); radPoint2_CheckedChanged(this, new EventArgs()); radPoint3_CheckedChanged(this, new EventArgs()); } #endregion #region Triangle2 (left side) // Make the the triangle if (chkTrianglePerpendicular2.Checked) { _triangle2 = new TriangleTest(_ball1.Position.Clone(), new DoubleVector(1, 0, 0, 0, 1, 0), new Triangle(0, 0, -100, 0, 100, 0, 0, -100, 100)); radPoint1Neg2.Enabled = radPoint1Pos2.Enabled = radPoint1Zero2.Enabled = false; radPoint2Neg2.Enabled = radPoint2Pos2.Enabled = radPoint2Zero2.Enabled = false; radPoint3Neg2.Enabled = radPoint3Pos2.Enabled = radPoint3Zero2.Enabled = false; } else { _triangle2 = new TriangleTest(_ball1.Position.Clone(), new DoubleVector(1, 0, 0, 0, 1, 0), new Triangle(-150, -150, 0, 150, -150, 0, 0, 150, 0)); radPoint1Neg2.Enabled = radPoint1Pos2.Enabled = radPoint1Zero2.Enabled = true; radPoint2Neg2.Enabled = radPoint2Pos2.Enabled = radPoint2Zero2.Enabled = true; radPoint3Neg2.Enabled = radPoint3Pos2.Enabled = radPoint3Zero2.Enabled = true; radPoint1Changed2_CheckedChanged(this, new EventArgs()); radPoint2Changed2_CheckedChanged(this, new EventArgs()); radPoint3Changed2_CheckedChanged(this, new EventArgs()); } #endregion #endregion } else { MessageBox.Show("Unknown radio button", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } #endregion } else { #region Polygons if (radSpherePolygon.Checked) { #region Sphere Polygon // I will leave ball1 alone. I won't use its velocity, just radius // Make the the polygon //_polygon1 = new PolygonTest(_ball2.Position.Clone(), new DoubleVector(1, 0, 0, 0, 1, 0), Polygon.CreateTetrahedron(_ball2.Radius * 2, true), _ball2.Radius * 2); _polygon1 = new PolygonTest(_ball2.Position.Clone(), new DoubleVector(1, 0, 0, 0, 1, 0), MyPolygon.CreateCube(_ball2.Radius * 5, true), _ball2.Radius * 4); #endregion } else if (radPolygonPolygon.Checked) { #region Polygon Polygon // Polygon1 is on the right (so it uses ball2 as its source) _polygon1 = new PolygonTest(_ball2.Position.Clone(), new DoubleVector(1, 0, 0, 0, 1, 0), MyPolygon.CreateTetrahedron(_ball2.Radius * 2, true), _ball2.Radius * 2); _polygon2 = new PolygonTest(_ball1.Position.Clone(), new DoubleVector(1, 0, 0, 0, 1, 0), MyPolygon.CreateCube(_ball1.Radius * 2, true), _ball2.Radius * 2); #endregion } else { MessageBox.Show("Unknown radio button", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } #endregion } }
private MyVector GetVelocityEnd(Ball ball) { return GetVelocityEnd(ball.Position, ball); }
private Ball BuildObject() { double radius; #region Calculate Radius switch (_newBallProps.SizeMode) { case BallProps.SizeModes.Draw: radius = .01d; // this function will only get called during mouse down if it's in draw mode, so I need to start with an arbitrarily small radius break; case BallProps.SizeModes.Fixed: radius = _newBallProps.SizeIfFixed; break; case BallProps.SizeModes.Random: radius = _rand.Next(Convert.ToInt32(_newBallProps.MinRandSize), Convert.ToInt32(_newBallProps.MaxRandSize)); break; default: throw new ApplicationException("Unknown BallProps.SizeModes: " + _newBallProps.SizeMode); } if (radius < MINRADIUS) { radius = MINRADIUS; } #endregion double mass = UtilityCore.GetMassForRadius(radius, 1d); MyVector velocity; #region Calculate Velocity if (_newBallProps.RandomVelocity) { velocity = Utility3D.GetRandomVectorSpherical2D(_newBallProps.MaxVelocity); } else { velocity = _newBallProps.Velocity; // no need to clone it. I won't manipulate it } #endregion //TODO: Listen to global props double elasticity = .75d; double kineticFriction = .75d; double staticFriction = 1d; Ball retVal; #region Build the ball switch (_mode) { case AddingMode.AddBall: #region Create Ball retVal = new Ball(_curMousePoint.Clone(), new DoubleVector(0, 1, 0, 1, 0, 0), radius, mass, elasticity, kineticFriction, staticFriction, _boundryLower, _boundryUpper); retVal.Velocity.Add(velocity); #endregion break; case AddingMode.AddSolidBall: #region Create Solid Ball retVal = new SolidBall(_curMousePoint.Clone(), new DoubleVector(0, 1, 0, 1, 0, 0), radius, mass, elasticity, kineticFriction, staticFriction, _boundryLower, _boundryUpper); retVal.Velocity.Add(velocity); //StoreAngularVelocity(retVal); // no reason to do this here. it will be applied during commit (if I'm in draw mode, the size will change, and the angular velocity will need to be reapplied anyway) #endregion break; default: throw new ApplicationException("Unsupported AddingMode: " + _mode.ToString()); } #endregion // Exit Function return retVal; }
private MyVector _offset = new MyVector(); // this is the offset from the center of the object that they clicked #endregion #endregion #region Constructor public CollisionTester() { InitializeComponent(); // Balls _ball1 = new Ball(new MyVector(-500, 0, 0), new DoubleVector(0, 1, 0, 1, 0, 0), 150, 150, 1, 1, 1, _boundryLower, _boundryUpper); _ball1.Velocity.X = 200; _ball1.Velocity.Y = 50; _ball2 = new Ball(new MyVector(500, 0, 0), new DoubleVector(0, 1, 0, 1, 0, 0), 150, 150, 1, 1, 1, _boundryLower, _boundryUpper); _ball2.Velocity.X = -200; _ball2.Velocity.Y = -50; // Viewer pictureBox1.SetBorder(_boundryLower, _boundryUpper); pictureBox1.ShowBorder(Color.LightSteelBlue, 10d); // Raise events pictureBox1_Resize(this, new EventArgs()); timer1.Enabled = true; }
private bool WillCollide(Ball ball) { // Make a temp blip to be a wrapper for this RadarBlip blip = new RadarBlip(ball, CollisionStyle.Standard, RadarBlipQual.BallUserDefined10, TokenGenerator.NextToken()); foreach (RadarBlip existingBlip in _map.GetAllBlips()) { if (_map.CollisionHandler.IsColliding(blip, existingBlip) != CollisionDepth.NotColliding) { // It's colliding return true; } } return false; }
public void DrawBall(Ball ball, DrawMode mode, CollisionStyle collisionStyle, bool drawRed) { DrawBall(ball, mode, collisionStyle, drawRed, 1d); }
private void StoreAngularVelocity(Ball ball) { if (!(ball is TorqueBall)) { return; } MyVector angularVelocity; #region Calculate Angular Velocity switch (_newBallProps.AngularVelocityMode) { case BallProps.AngularVelocityModes.Fixed: angularVelocity = new MyVector(0, 0, _newBallProps.AngularVelocityIfFixed); break; case BallProps.AngularVelocityModes.Random: angularVelocity = new MyVector(0, 0, UtilityCore.GetScaledValue(_newBallProps.MinRandAngularVelocity, _newBallProps.MaxRandAngularVelocity, 0, 1, _rand.NextDouble())); break; default: throw new ApplicationException("Unknown BallProps.AngularVelocityModes: " + _newBallProps.AngularVelocityMode.ToString()); } #endregion // Apply Angular Velocity ((TorqueBall)ball).SetAngularVelocity(angularVelocity); }
private void AddMultiBall(double radius, double mass) { // Physical Ball MyVector pos = Utility3D.GetRandomVector(BOUNDRY); DoubleVector dirFacing = new DoubleVector(1, 0, 0, 0, 1, 0); Ball ball = new Ball(pos, dirFacing, radius, mass, ELASTICITY, KINETICFRICTION, STATICFRICTION, _boundryLower, _boundryUpper); BallBlip blip = new BallBlip(ball, CollisionStyle.Standard, RadarBlipQual.BallUserDefined00, TokenGenerator.NextToken()); _map.Add(blip); // WPF Rendering Geometry3D geometry = UtilityWPF.GetSphere(5, radius); Material material = new DiffuseMaterial(new SolidColorBrush(Color.FromArgb(255, Convert.ToByte(_rand.Next(256)), Convert.ToByte(_rand.Next(256)), Convert.ToByte(_rand.Next(256))))); GeometryModel3D geometryModel = new GeometryModel3D(geometry, material); geometryModel.Transform = new Transform3DGroup(); //TODO: Tie this transform directly to the ball's velocity (the shpere class should take the transform group) Transform3DGroup group = geometryModel.Transform as Transform3DGroup; group.Children.Clear(); group.Children.Add(new TranslateTransform3D(pos.X, pos.Y, pos.Z)); _geometries.Add(geometryModel); _modelGroup.Children.Add(geometryModel); }
private void DrawBall(Ball ball, Color ballColor) { // Turn it into ints MyVector topLeft = ball.Position.Clone(); topLeft.X -= ball.Radius; topLeft.Y -= ball.Radius; Point topLeftInt = topLeft.ToPoint(); int size = Convert.ToInt32(ball.Radius * 2); // Draw the ball _graphics.DrawEllipse(new Pen(ballColor, 3), topLeftInt.X, topLeftInt.Y, size, size); }
private void btnAdd_Click(object sender, EventArgs e) { double radius = _rand.Next(Convert.ToInt32(MINRADIUSMASS), Convert.ToInt32(MAXRADIUSMASS)); double mass = GetMass(radius); double elasticity = GetElasticity(); Ball ball = new Ball(Utility3D.GetRandomVector(_boundryLower, _boundryUpper), new DoubleVector(0, 1, 0, 1, 0, 0), radius, mass, elasticity, 1, 1, _boundryLower, _boundryUpper); ball.Velocity.Add(Utility3D.GetRandomVector(MAXVELOCITY)); ball.Velocity.Z = 0; BallBlip blip = new BallBlip(ball, CollisionStyle.Standard, RadarBlipQual.BallUserDefined00, TokenGenerator.NextToken()); _map.Add(blip); }
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); }
/// <summary> /// The only reason you would pass in your own guid is when loading a previously saved scene (the token /// works good for processing in ram, but when stuff needs to go to file, use the guid) /// </summary> public BallBlip(Ball ball, CollisionStyle collisionStyle, RadarBlipQual blipQual, long token, Guid objectID) : base(ball, collisionStyle, blipQual, token, objectID) { _ball = ball; _torqueBall = ball as TorqueBall; // if it's not, null will be stored }
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; }