Beispiel #1
0
        public SpaceSim()
            : base()
        {
            Content.RootDirectory = "Content";

            World    = this;
            graphDev = new GraphicsDeviceManager(this);
        }
Beispiel #2
0
        protected override void Update(GameTime gameTime)
        {
            screenCenter = new Vector2((float)(Window.ClientBounds.Width / 2), (float)(Window.ClientBounds.Height / 2));

            // bereken de distance van mouse en screen center
            Vector2 distance = new Vector2(mousePosition.X - screenCenter.X, mousePosition.Y - screenCenter.Y);

            distance.Normalize();

            cameraPosition          = Vector3.Transform(spaceshipFollowPoint, spaceship.Transform);
            cameraLookAt            = Vector3.Transform(spaceshipLookAtPoint, spaceship.Transform);
            cameraOrientationMatrix = spaceshipOrientationMatrix;

            // Helpers for input
            KeyboardState keyboard = Keyboard.GetState();

            wKeyDown = keyboard.IsKeyDown(Keys.W);
            aKeyDown = keyboard.IsKeyDown(Keys.A);
            sKeyDown = keyboard.IsKeyDown(Keys.S);
            dKeyDown = keyboard.IsKeyDown(Keys.D);
            if (keyboard.IsKeyDown(Keys.Escape))
            {
                Exit();
            }
            MouseState mouse = Mouse.GetState();

            mousePosition   = mouse.Position;
            mouseButton     = mouse.LeftButton == ButtonState.Pressed;
            mouseDown       = mouseButton && !lastMouseButton;
            lastMouseButton = mouseButton;

            skybox.Transform = Matrix.CreateScale(1000f) * Matrix.CreateTranslation(cameraPosition);

            distance.X = -distance.X;
            distance.Y = -distance.Y;

            // add earth rotation update
            Matrix earthRotation = Matrix.CreateRotationY((float)gameTime.ElapsedGameTime.TotalSeconds * 0.50f);

            earth.Transform = earth.Transform * earthRotation;
            // add mars rotation update
            Matrix marsRotation = Matrix.CreateRotationY((float)gameTime.ElapsedGameTime.TotalSeconds * 0.30f);

            mars.Transform = mars.Transform * marsRotation;
            // add jupiter rotation update
            Matrix jupiterRotation = Matrix.CreateRotationY((float)gameTime.ElapsedGameTime.TotalSeconds * 0.20f);

            jupiter.Transform = jupiter.Transform * jupiterRotation;
            // add saturn rotation update
            Matrix saturnRotation = Matrix.CreateRotationY((float)gameTime.ElapsedGameTime.TotalSeconds * 0.14f);

            saturn.Transform = saturn.Transform * saturnRotation;
            // add uranus rotation update
            Matrix uranusRotation = Matrix.CreateRotationY((float)gameTime.ElapsedGameTime.TotalSeconds * 0.07f);

            uranus.Transform = uranus.Transform * uranusRotation;

            // moon rotation update
            moonRotation = moonRotation + gameTime.ElapsedGameTime.TotalSeconds * 1.5;
            // scale and position of moon
            moon.Transform  = Matrix.CreateScale(0.5f);
            moon.Transform *= Matrix.CreateTranslation(2f, 0.0f, 0.0f);
            // create rotation on x and y relative to earth
            moon.Transform *= Matrix.CreateRotationY((float)moonRotation);
            moon.Transform *= Matrix.CreateRotationX((float)Math.PI / 4);
            moon.Transform *= Matrix.CreateTranslation(Vector3.Transform(Vector3.Zero, earth.Transform));

            // get user input to add to velocity
            if (aKeyDown && rollVelocity < 200)
            {
                rollVelocity += 0.005f;
            }

            if (dKeyDown && rollVelocity > -200)
            {
                rollVelocity -= 0.005f;
            }

            if (wKeyDown && forwardVelocity < 0.25f)
            {
                forwardVelocity += 0.0005f;
            }

            if (sKeyDown && forwardVelocity > -0.25f)
            {
                forwardVelocity -= 0.0005f;
            }

            if (!wKeyDown && !sKeyDown)
            {
                forwardVelocity *= 0.96f;
            }

            // add velocity to ship
            spaceshipPosition += forwardVelocity * spaceshipOrientationMatrix.Forward;
            // orientate spaceship yaw pitch and roll
            SpaceSim.RotateOrientationMatrixByYawPitchRoll(ref spaceshipOrientationMatrix, distance.X * 0.005f, distance.Y * 0.005f, rollVelocity);
            spaceship.Transform = spaceshipOrientationMatrix * Matrix.CreateTranslation(spaceshipPosition);


            // check if roll velocity goes above limits
            if (rollVelocity > 200f)
            {
                rollVelocity = 200f;
            }
            else if (rollVelocity < -200f)
            {
                rollVelocity = -200f;
            }

            // check if forward velocity goes above limits
            if (forwardVelocity > 100f)
            {
                forwardVelocity = 100f;
            }
            else if (forwardVelocity < -100f)
            {
                forwardVelocity = -100f;
            }

            // add drag if no button is pressed
            if (!aKeyDown && !dKeyDown)
            {
                rollVelocity *= 0.95f;
            }

            // if mouse down shoot bullet
            if (mouseDown)
            {
                bullets.Add(new Bullet(Vector3.Transform(bulletSpawnPosition, spaceship.Transform), spaceshipOrientationMatrix.Forward * (forwardVelocity + 10f)));
            }

            // update each bullet
            foreach (Bullet bullet in bullets)
            {
                bullet.Update((float)gameTime.ElapsedGameTime.TotalSeconds);
            }

            // remove bullet if too far
            for (int i = 0; i > bullets.Count; ++i)
            {
                if ((float)bullets[i].Position.Length() > 200)
                {
                    bullets.RemoveAt(i);
                }
            }

            base.Update(gameTime);
        }
Beispiel #3
0
 static void Main()
 {
     using (SpaceSim game = new SpaceSim())
         game.Run();
 }