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 ShakeMotionDemo : StandardDemo
{
///
/// Constructs the demo.
///
/// Game that owns the demo.
public ShakeMotionDemo(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));
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);
}
}
motion = new Motion();
try
{
motion.Start();
motionAvailable = true;
}
catch (Exception e)
{
motionAvailable = false;
}
}
Motion motion;
bool motionAvailable;
public override void Update(float dt)
{
if (motionAvailable)
{
Vector3 gravity = motion.CurrentValue.Gravity;
Vector3 externalAcceleration = motion.CurrentValue.DeviceAcceleration;
//Check the external acceleration magnitude against a threshold to avoid using every single tiny shake.
if (externalAcceleration.LengthSquared() > .3f * .3f)
{
//If the external acceleration is significant, include it for shakey purposes.
Space.ForceUpdater.Gravity = gravity * 20 - externalAcceleration * 300;
}
else
{
//If the external acceleration is tiny, don't bother including it.
Space.ForceUpdater.Gravity = gravity * 20;
}
}
base.Update(dt);
}
public override void DrawUI()
{
if (!motionAvailable)
{
Game.SpriteBatch.DrawString(Game.ButtonFont, "Motion 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()
{
motion.Stop();
base.CleanUp();
}
}
}