Example #1
0
 public static double GetDistance3D(MyVector position1, MyVector position2)
 {
     return(MyVector.Subtract(position2, position1).GetMagnitude());
 }
        private void timer1_Tick(object sender, EventArgs e)
        {

            const double ELAPSEDTIME = .5;

            #region Do Physics

            _poolBall.PrepareForNewTimerCycle();


            if (_isMouseJustReleased)
            {
                _isMouseJustReleased = false;

                MyVector force = new MyVector(_mouseDownPoint);
                force.Subtract(new MyVector(_curMousePoint));

                MyVector offset = new MyVector(_mouseDownPoint);
                offset.Subtract(_poolBall.Position);

                _poolBall.ApplyExternalForce(offset, force);
            }


            _poolBall.TimerTestPosition(ELAPSEDTIME);
            _poolBall.TimerFinish();

            #endregion

            #region Draw

            ClearPictureBox();

            if (_isMouseDown)
            {
                DrawVector(_poolBall.Position, new MyVector(_mouseDownPoint.X, _mouseDownPoint.Y, 0), Color.Olive);
                DrawVector(new MyVector(_mouseDownPoint.X, _mouseDownPoint.Y, 0), new MyVector(_curMousePoint.X, _curMousePoint.Y, 0), Color.Gold);
            }

            DrawBall(_poolBall, Color.Silver, Color.MediumPurple, Color.Purple);

            BlitImage();

            #endregion

        }
        protected void GetCollisionNormalAndPointsOfContact_SphereSphere(out MyVector normal, out double normalMagnitude, out MyVector pointOfContact1, out MyVector pointOfContact2, BallBlip ball1, BallBlip ball2)
        {
            // Vector that is perpendicular to the tangent of the collision, and it points in the direction of object 1.  Real
            // easy when dealing with spheres     :)
            normal = ball2.Ball.Position - ball1.Ball.Position;

            // Remember this length
            normalMagnitude = normal.GetMagnitude();

            // This needs to be returned as a unit vector
            normal.Divide(normalMagnitude);

            // Start them off as unit vectors
            pointOfContact1 = normal.Clone();
            pointOfContact2 = normal.Clone();

            // Finish (use the ratio of their radii)
            pointOfContact1.Multiply((ball1.Ball.Radius / (ball1.Ball.Radius + ball2.Ball.Radius)) * normalMagnitude);
            pointOfContact2.Multiply((ball2.Ball.Radius / (ball1.Ball.Radius + ball2.Ball.Radius)) * normalMagnitude * -1);		// I want this one pointing the other direction

            // Now that I have the points of contact relative to the centers of position, I need to make them
            // relative to the centers of mass
            if (ball1.TorqueBall != null)
            {
                pointOfContact1.Subtract(ball1.TorqueBall.CenterOfMass);
            }

            if (ball2.TorqueBall != null)
            {
                pointOfContact2.Subtract(ball2.TorqueBall.CenterOfMass);
            }
        }