Example #1
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
              if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();

              KeyboardState state = Keyboard.GetState();
              if (state.IsKeyDown(Keys.D1)) {
            x -= .1f;
              } else if (state.IsKeyDown(Keys.D2)) {
            x += .1f;
              } else if (state.IsKeyDown(Keys.D3)) {
            y -= .1f;
              } else if (state.IsKeyDown(Keys.D4)) {
            y += .1f;
              } else if (state.IsKeyDown(Keys.D5)) {
            z -= .1f;
              } else if (state.IsKeyDown(Keys.D6)) {
            z += .1f;
              } else if (state.IsKeyDown(Keys.Up)) {
            magnitude += .05f;
            if (magnitude > 30) {
              magnitude = 30;
            }
              } else if (state.IsKeyDown(Keys.Down)) {
            magnitude -= .05f;
            if (magnitude < 5) {
              magnitude = 5;
            }
              } else if (state.IsKeyDown(Keys.Left)) {
            angle += .02f;
            if (angle > Math.PI / 2) {
              angle = (float)Math.PI / 2;
            }
              } else if (state.IsKeyDown(Keys.Right)) {
            angle -= .02f;
            if (angle < 0) {
              angle = 0;
            }
              }

              if (prevState.IsKeyUp(Keys.Space) && state.IsKeyDown(Keys.Space)) {
            Model cubeMode = Content.Load<Model>("sphere");
            Body body = new Body(this, cubeMode, toggle ? new Vector3(1, 0, 0) : new Vector3(0, 0, 1), "sphere");
            body.SetWorld(new Vector3(0, 0, -9));
            toggle = !toggle;

            float yMag = (float) Math.Sin(angle) * magnitude;
            float zMag = (float) Math.Cos(angle) * magnitude;

            body.SetVelocity(new Vector3(0, yMag, zMag), Vector3.Zero);
            for (int i = 0; i < body.Skin.Count; i++) {
              body.Skin.SetMaterial(body.Skin[i], new Material(.5f, 1f));
            }
            physicsManager.Add(body);
            bodies.Add(body);
              }

              prevState = state;
              base.Update(gameTime);
        }