using BEPUphysics.Entities.Prefabs; using Microsoft.Xna.Framework; using Microsoft.Devices.Sensors; using System; namespace BEPUphysicsPhoneDemo.Demos { /// /// Demo of a wall of blocks. /// public class ShakeDemo : StandardDemo { /// /// Constructs the demo. /// /// Game that owns the demo. public ShakeDemo(PhoneGame game) : base(game) { } /// /// Gets the name of the simulation. /// public override string Name { get { return "Shakyshake"; } } /// /// Initializes the demo. /// public override void Initialize() { base.Initialize(); Game.Camera.Position = new Vector3(0, 6, 18); Space.Add(new Box(Vector3.Zero, 30, 10, 30)); Space.Add(new Box(new Vector3(0, 30, 0), 30, 10, 30)); Space.Add(new Box(new Vector3(15, 15, 0), 10, 30, 30)); Space.Add(new Box(new Vector3(-15, 15, 0), 10, 30, 30)); Space.Add(new Box(new Vector3(0, 15, 15), 30, 30, 10)); Space.Add(new Box(new Vector3(0, 15, -15), 30, 30, 10)); Space.ForceUpdater.Gravity = new Vector3(); Space.TimeStepSettings.TimeStepDuration = 1 / 120f; float horizontalSpacing = 2; float verticalSpacing = 2f; int rowCount = 3; int columnCount = 3; for (int i = 0; i < rowCount; i++) { for (int j = 0; j < columnCount; j++) { var toAdd = new Box(new Vector3( j * horizontalSpacing - (columnCount - i % 2) * horizontalSpacing * .5f, verticalSpacing * (i + 1), 0), 1, 1, 1, 20); toAdd.ActivityInformation.IsAlwaysActive = true; Space.Add(toAdd); } } accelerometer = new Accelerometer(); try { accelerometer.Start(); accelerometerAvailable = true; } catch (AccelerometerFailedException e) { accelerometerAvailable = false; } catch (UnauthorizedAccessException e) { accelerometerAvailable = false; } } Accelerometer accelerometer; bool accelerometerAvailable; public override void Update(float dt) { if (accelerometerAvailable) { Space.ForceUpdater.Gravity = accelerometer.CurrentValue.Acceleration * 500; } //Do internal time stepping. Space.Update(dt); //base.Update(dt); } public override void DrawUI() { if (!accelerometerAvailable) { Game.SpriteBatch.DrawString(Game.ButtonFont, "Accelerometer unavailable.", new Vector2(100, 100), Color.White); } else { Game.SpriteBatch.DrawString(Game.ButtonFont, Space.ForceUpdater.Gravity.X.ToString(), new Vector2(0, 100), Color.White); Game.SpriteBatch.DrawString(Game.ButtonFont, Space.ForceUpdater.Gravity.Y.ToString(), new Vector2(0, 120), Color.White); Game.SpriteBatch.DrawString(Game.ButtonFont, Space.ForceUpdater.Gravity.Z.ToString(), new Vector2(0, 140), Color.White); } base.DrawUI(); } public override void CleanUp() { accelerometer.Stop(); base.CleanUp(); } } }