/// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { // TODO: Add your initialization logic here base.Initialize(); physicsManager.LinearErrorTolerance = .2f; Model cupBottomModel = Content.Load<Model>("cup_bottom"); Model cupTopModel = Content.Load<Model>("cup_top"); Body cupBottom = new Body(this, cupBottomModel, "cup_bottom"); Body cupTop = new Body(this, cupTopModel, "cup_top"); cupBottom.OnCollision += OnCollide; for (int i = 0; i < cupBottom.Skin.Count; i++) { cupBottom.Skin.SetMaterial(cupBottom.Skin[i], new Material(1, .5f)); } cupBottom.MassProperties = MassProperties.Immovable; cupBottom.SetWorld(10, Vector3.Zero, Quaternion.Identity); bodies.Add(cupBottom); for (int i = 0; i < cupTop.Skin.Count; i++) { cupTop.Skin.SetMaterial(cupTop.Skin[i], new Material(1, .5f)); } cupTop.MassProperties = MassProperties.Immovable; cupTop.SetWorld(10, Vector3.Zero, Quaternion.Identity); bodies.Add(cupTop); BodySkin skin = new BodySkin(); skin.DefaultMaterial = new Material(.4f, .5f); skin.Add( new PlanePart(new Vector3(0, 0, -1f), Vector3.UnitZ), new PlanePart(new Vector3(0, 1, 0), -Vector3.UnitY), new PlanePart(new Vector3(0, 0, 0), Vector3.UnitY), new PlanePart(new Vector3(1, 0, 0), -Vector3.UnitX), new PlanePart(new Vector3(-1, 0, 0), Vector3.UnitX) ); wall = new RigidBody(skin); wall.SetWorld(10, Vector3.Zero, Quaternion.Identity); physicsManager.Gravity = new Vector3(0,-9.8f, 0); physicsManager.Add(cupBottom); physicsManager.Add(cupTop); physicsManager.Add(wall); prevState = Keyboard.GetState(); }
/// <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); }