Exemplo n.º 1
0
        private void Cube_ApplyForce(Body sender, BodyForceEventArgs e)
        {
            if (_shouldRandomizeVelocities)
            {
                #region Set Velocities

                _didRandomizeVelocities = true;

                Vector3D newVelocity = Math3D.GetRandomVector_Spherical(MAXRANDVELOCITY * _randVelMultiplier);

                //sender.Velocity.X = newVelocity.X;
                //sender.Velocity.Y = newVelocity.Y;
                //sender.Velocity.Z = newVelocity.Z;

                e.AddImpulse(newVelocity, sender.CenterOfMass.ToVector());

                #endregion
            }

            #region Do Gravity

            // Calculate force between the two
            //TODO:  Calculate these forces in one place and remember the results


            Point3D blueCenterWorld = GetWorldCenterMass(_blueCube);
            Point3D redCenterWorld  = GetWorldCenterMass(_redCube);

            Vector3D gravityLink = blueCenterWorld - redCenterWorld;

            double force = GRAVITATIONALCONSTANT * (_blueCube.Mass * _redCube.Mass) / gravityLink.LengthSquared;

            gravityLink.Normalize();
            gravityLink = Vector3D.Multiply(force, gravityLink);

            // Apply the force
            if (sender == _blueCube)
            {
                e.AddForce(Vector3D.Multiply(-1d, gravityLink));
            }
            else if (sender == _redCube)
            {
                e.AddForce(gravityLink);
            }
            else
            {
                MessageBox.Show("Unknown Sender: " + sender.ToString(), this.Title, MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            #endregion
        }
Exemplo n.º 2
0
        private void Body_ApplyForce(Body sender, BodyForceEventArgs e)
        {
            if (sender != _physicsBody)
            {
                return;
            }

            if (!_hasSetInitialVelocity)
            {
                e.AddImpulse(_initialVelocity, new Vector3D(0, 0, 0));
                _hasSetInitialVelocity = true;

                // No need to listen to this even anymore
                _physicsBody.ApplyForce -= new BodyForceEventHandler(Body_ApplyForce);
            }
        }
Exemplo n.º 3
0
        private void Body_ApplyForce(Body sender, BodyForceEventArgs e)
        {
            if (sender != _physicsBody)
            {
                return;
            }

            if (!_hasSetInitialVelocity)
            {
                e.AddImpulse(_initialVelocity, Math3D.GetRandomVector_Spherical(this.Radius * .5d));     // whacking it at an offset, so it has some rotation as well
                _hasSetInitialVelocity = true;

                // No need to listen to this even anymore
                _physicsBody.ApplyForce -= new BodyForceEventHandler(Body_ApplyForce);
            }
        }
Exemplo n.º 4
0
        private void Body_ApplyForce(Body sender, BodyForceEventArgs e)
        {
            if (_shouldRandomizeVelocities)
            {
                #region Set Velocities

                _didRandomizeVelocities = true;

                Vector3D newVelocity = Math3D.GetRandomVector_Spherical(MAXRANDVELOCITY * _randVelMultiplier);

                e.AddImpulse(newVelocity, sender.CenterOfMass.ToVector());

                #endregion
            }

            // Apply Gravity
            if (!_cubeForces.ContainsKey(sender))
            {
                return;
            }

            e.AddForce(_cubeForces[sender]);
        }