Beispiel #1
0
 public void Collide(GravitationalField gravityObject)
 {
     if (gravityObject is Blackhole)
     {
         BlackholeBattle.swallowedObjects.Add(this);
         gravityObject.mass += this.mass;
     }
     else
     {
         Vector3 objectVelocity;
         //http://farside.ph.utexas.edu/teaching/301/lectures/node76.html
         //given initial velocities and masses, find final velocity.
         if (gravityObject.updatedInLoop)
         {
             objectVelocity = gravityObject.preVelocity;
         }
         else
         {
             objectVelocity = gravityObject.state.v;
         }
         //find the component vector normal to the collision surface. Remove it from the velocity vector. Then reverse the component normal to the surface and add it back to the velocity vector
         Vector3 normal = gravityObject.state.x - state.x;
         normal.Normalize();
         Vector3 velocityHat = state.v;
         velocityHat.Normalize();
         Vector3 remainingVector = velocityHat - normal;
         remainingVector += -1 * (normal);
         remainingVector.Normalize();
         state.v = (float)((mass - gravityObject.mass) * state.v.Length() / (gravityObject.mass + mass) + (2 * gravityObject.mass * objectVelocity.Length()) / (mass + gravityObject.mass)) * remainingVector;
     }
 }
        private void UpdateGamePad()
        {
            KeyboardState state = Keyboard.GetState();
            MouseState    mouse = Mouse.GetState();
            bool          selectMultipleUnits = false;

            if (state.IsKeyDown(Keys.N) && !holdingN)
            {
                holdingN = true;
                if (cameraState != ViewState.XY)
                {
                    cameraState = ViewState.XY;
                }
                else
                {
                    cameraState = ViewState.Camera;
                }
            }
            else if (!state.IsKeyDown(Keys.N))
            {
                holdingN = false;
            }
            if (state.IsKeyDown(Keys.M) && !holdingM)
            {
                holdingM = true;
                if (cameraState != ViewState.ZX)
                {
                    cameraState = ViewState.ZX;
                }
                else
                {
                    cameraState = ViewState.Camera;
                }
            }
            else if (!state.IsKeyDown(Keys.M))
            {
                holdingM = false;
            }
            if (state.IsKeyDown(Keys.Escape))
            {
                ScreenManager.AddScreen(new PauseMenuScreen(), ControllingPlayer);
            }
            if (state.IsKeyDown(Keys.Down))
            {
                Vector3 cross = Vector3.Cross((cameraDirection - cameraPosition), Vector3.Right);
                cross.Normalize();
                cameraPosition += cross * ((cameraDirection - cameraPosition).Length() / 16);
            }
            if (state.IsKeyDown(Keys.Up))
            {
                Vector3 cross = Vector3.Cross((cameraDirection - cameraPosition), Vector3.Right);
                cross.Normalize();
                cameraPosition -= cross * ((cameraDirection - cameraPosition).Length() / 16);
            }
            if (state.IsKeyDown(Keys.Left))
            {
                Vector3 cross = Vector3.Cross((cameraDirection - cameraPosition), Vector3.UnitY);
                cross.Normalize();
                cameraPosition += cross * ((cameraDirection - cameraPosition).Length() / 16);
            }
            if (state.IsKeyDown(Keys.Right))
            {
                Vector3 cross = Vector3.Cross((cameraDirection - cameraPosition), Vector3.UnitY);
                cross.Normalize();
                cameraPosition -= cross * ((cameraDirection - cameraPosition).Length() / 16);
            }
            if (state.IsKeyDown(Keys.LeftControl))
            {
                //selectMultipleUnits = true;
            }
            if (IsServer != false)
            {
                if (state.IsKeyDown(Keys.W))
                {
                    if (GetCurrentBlackHole() != null)
                    {
                        Vector3 lookingAt = (cameraDirection - cameraPosition);
                        lookingAt.Normalize();
                        GetCurrentBlackHole().Accelerate(lookingAt);
                    }
                }
                if (state.IsKeyDown(Keys.A))
                {
                    if (GetCurrentBlackHole() != null)
                    {
                        Vector3 lookingAt = (cameraDirection - cameraPosition);
                        lookingAt = Vector3.Cross(lookingAt, Vector3.Down);
                        lookingAt.Normalize();
                        GetCurrentBlackHole().Accelerate(lookingAt);
                    }
                }
                if (state.IsKeyDown(Keys.S))
                {
                    if (GetCurrentBlackHole() != null)
                    {
                        Vector3 lookingAt = (cameraDirection - cameraPosition);
                        lookingAt.Normalize();
                        GetCurrentBlackHole().Accelerate(-lookingAt);
                    }
                }
                if (state.IsKeyDown(Keys.D))
                {
                    if (GetCurrentBlackHole() != null)
                    {
                        Vector3 lookingAt = (cameraDirection - cameraPosition);
                        lookingAt = Vector3.Cross(lookingAt, Vector3.Up);
                        lookingAt.Normalize();
                        GetCurrentBlackHole().Accelerate(lookingAt);
                    }
                }
                if (state.IsKeyDown(Keys.LeftShift))
                {
                    if (GetCurrentBlackHole() != null)
                    {
                        Vector3 lookingAt = (cameraDirection - cameraPosition);
                        lookingAt = Vector3.Cross(lookingAt, Vector3.Left);
                        lookingAt.Normalize();
                        GetCurrentBlackHole().Accelerate(lookingAt);
                    }
                }
                if (state.IsKeyDown(Keys.LeftControl))
                {
                    if (GetCurrentBlackHole() != null)
                    {
                        Vector3 lookingAt = (cameraDirection - cameraPosition);
                        lookingAt = Vector3.Cross(lookingAt, Vector3.Right);
                        lookingAt.Normalize();
                        GetCurrentBlackHole().Accelerate(lookingAt);
                    }
                }
                if (state.IsKeyDown(Keys.Space))
                {
                    if (GetCurrentBlackHole() != null)
                    {
                        GetCurrentBlackHole().Brake();
                    }
                }
            }

            if (mouse.RightButton == ButtonState.Pressed)
            {
                //take position of mouse on the screen
                int mouseX = mouse.X;
                int mouseY = mouse.Y;

                Vector3 nearsource = new Vector3((float)mouseX, (float)mouseY, 0f);
                Vector3 farsource  = new Vector3((float)mouseX, (float)mouseY, 1f);

                Matrix world = Matrix.CreateTranslation(0, 0, 0);

                //find out where they are in the worldspace by transforming the matrix back
                Vector3 nearPoint = ScreenManager.GraphicsDevice.Viewport.Unproject(nearsource,
                                                                                    projection, view, world);

                Vector3 farPoint = ScreenManager.GraphicsDevice.Viewport.Unproject(farsource,
                                                                                   projection, view, world);
                Vector3 direction = farPoint - nearPoint;
                direction.Normalize();

                //create a ray from the points

                Ray   pickRay     = new Ray(nearPoint, direction);
                IUnit bestGObject = new GravitationalField();
                //find the closest object to the camera that intersects with the ray
                float maxDistance = float.MaxValue;
                foreach (IUnit u in units.Where(a => !(a is Blackhole) || ((Blackhole)a).Owner() == IsServer))
                {
                    if (u is Blackhole)
                    {
                        Vector3 posOnScreen = ScreenManager.GraphicsDevice.Viewport.Project(u.Position(), projection, view, Matrix.CreateTranslation(0, 0, 0));
                        if (new Rectangle((int)posOnScreen.X, (int)posOnScreen.Y, 50, 50).Contains(new Point(mouse.X, mouse.Y)))
                        {
                            bestGObject = u;
                            maxDistance = (u.Position() - cameraPosition).Length();
                        }
                    }
                    else
                    {
                        float?distanceIntersection = pickRay.Intersects(u.GetBounds());
                        if (distanceIntersection.HasValue)
                        {
                            if (distanceIntersection < maxDistance)
                            {
                                bestGObject = u;
                                maxDistance = distanceIntersection.Value;
                            }
                        }
                    }
                }
                if (maxDistance != float.MaxValue)
                {
                    if (!selectMultipleUnits)
                    {
                        //this allows for control groups and whatever else to be used later
                        selectedUnits.Clear();
                    }

                    if (bestGObject is Blackhole)
                    {
                        selectedUnits = new HashSet <IUnit>(selectedUnits.Where(a => !(a is Blackhole)));
                    }

                    selectedUnits.Add(bestGObject);
                }
            }
            //zoom the camera in based on the scroll wheel
            int mouseScrollValue = mouse.ScrollWheelValue;

            if (mouseScrollValue != scrollValue)
            {
                Vector3 currentCameraDirection = cameraDirection - cameraPosition;
                currentCameraDirection.Normalize();
                cameraPosition += (mouseScrollValue - scrollValue) * 2 * currentCameraDirection;
                camToPosition   = (cameraDirection - cameraPosition).Length();
                scrollValue     = mouseScrollValue;
            }
        }