I was doing a little testing and informing about this theme and I come to a death end. I see all the demos and everything that I found about Bepu and 3D XNA programming, and I understand how all the thing works in some way, but I been unable to do what I want, so I ask for help.
My objective is make an application for WP7 "Mango" with the new Silverlight/XNA implementation (what makes a little more dificult to follow the demos because in this new "framework" there's no Game class, and some Demos use it to the methods for drawing and camera) that is basically a dice coming out of the screen, then fall in the "board" and roll with simple physic simulation (basically is the GettingStarted demo with fixed camera looking down to the board and a cube coming from outside of the image).
In my testing, I been able to set up a "ground", what is the cube scaled, and another box that will fall in it for testing. This was made in the Z axis...because I was unable to make a fixed camera that looks downside, so I put the gravity in the Z axis instead of Y axis...and it work, when I put the box it fall to the ground, and it simulates than the camera its up in the "sky" instead in a "lateral".
But then come the weird part...I don't know why when the box fall to the ground, it get stuck in it and no do any physics...it get stuck a little in the model and no do anything...I tried scaling the model, because my first thought was that the model was bigger than the physic block...but no, wasn't that.
I put here the code so you can take a look and see what is the problem, and I will thanks if someone can explain my how to do the camera I want. Thank you very much for helping me and sorry for the bad writing, I'm not english ^^.
Edit: Forgot to mention, I was getting slow rendering too...any ideas?
Code: Select all
public partial class GamePage : PhoneApplicationPage
{
#region Handlers
ContentManager contentManager;
GameTimer timer;
SpriteBatch spriteBatch;
#endregion
#region Variables Utiles
SharedGraphicsDeviceManager sharedGraphics = SharedGraphicsDeviceManager.Current;
#endregion
#region Modelos y Texturas
//Modelos
Model tabModel;
//Texturas
Texture2D texTabActual;
Texture2D texTab1;
#endregion
#region 3D Engine
//Matrices
Matrix world;
Matrix view;
Matrix projection;
Matrix[] transform;
Matrix modelScaling;
Matrix modelScalingTemp;
//Geometria de Fisicas
Box tableroBox;
Box pruebaBox;
//Espacio
Space space;
#endregion
public GamePage()
{
InitializeComponent();
// Get the content manager from the application
contentManager = (Application.Current as App).Content;
sharedGraphics.PreferredBackBufferWidth =
sharedGraphics.GraphicsDevice.Adapter.CurrentDisplayMode.Width;
sharedGraphics.PreferredBackBufferHeight =
sharedGraphics.GraphicsDevice.Adapter.CurrentDisplayMode.Height;
sharedGraphics.ApplyChanges();
// Create a timer for this page
timer = new GameTimer();
timer.UpdateInterval = TimeSpan.FromSeconds(1.0f / 60.0f);
timer.Update += OnUpdate;
timer.Draw += OnDraw;
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
// Set the sharing mode of the graphics device to turn on XNA rendering
SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(true);
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(SharedGraphicsDeviceManager.Current.GraphicsDevice);
// TODO: use this.content to load your game content here
if (tabModel == null)
{
space = new Space();
space.ForceUpdater.Gravity = new Vector3(0, 0, -9.81f);
tableroBox = new Box(Vector3.Zero, 2, 4, 0.1f);
pruebaBox = new Box(new Vector3(0, 0, 9), 0.5f, 0.5f, 0.5f, 1);
pruebaBox.LinearVelocity = new Vector3(0, 1, 0);
space.Add(tableroBox);
space.Add(pruebaBox);
tabModel = contentManager.Load<Model>("Modelos//Tablero//cube");
texTab1 = contentManager.Load<Texture2D>("Texturas//Tablero//texTab1");
texTabActual = texTab1;
modelScaling = Matrix.CreateScale(2, 4, 0.1f);
modelScalingTemp = Matrix.CreateScale(0.5f, 0.5f, 0.5f);
world = Matrix.CreateRotationY(1);
view = Matrix.CreateLookAt(new Vector3(0, 0, 10), Vector3.Zero, Vector3.Up);
projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45),
sharedGraphics.GraphicsDevice.Viewport.AspectRatio, 0.1f, 100f);
transform = new Matrix[tabModel.Bones.Count];
}
// Start the timer
timer.Start();
base.OnNavigatedTo(e);
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
// Stop the timer
timer.Stop();
// Set the sharing mode of the graphics device to turn off XNA rendering
SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(false);
base.OnNavigatedFrom(e);
}
/// <summary>
/// Allows the page to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
private void OnUpdate(object sender, GameTimerEventArgs e)
{
// TODO: Add your update logic here
space.Update();
}
/// <summary>
/// Allows the page to draw itself.
/// </summary>
private void OnDraw(object sender, GameTimerEventArgs e)
{
SharedGraphicsDeviceManager.Current.GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
DrawTablero(world, modelScaling, view, projection, transform);
DrawDado(world, modelScalingTemp, view, projection, transform);
}
private void DrawTablero(Matrix world, Matrix scale, Matrix view, Matrix projection, Matrix[] transforms)
{
tabModel.CopyAbsoluteBoneTransformsTo(transform);
foreach (ModelMesh mesh in tabModel.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
effect.DiffuseColor = Color.Blue.ToVector3();
effect.View = view;
effect.Projection = projection;
effect.World =
transforms[mesh.ParentBone.Index] * scale * tableroBox.WorldTransform;
}
mesh.Draw();
}
}
private void DrawDado(Matrix world, Matrix scale, Matrix view, Matrix projection,
Matrix[] transforms)
{
foreach (ModelMesh mesh in tabModel.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.PreferPerPixelLighting = true;
effect.EnableDefaultLighting();
effect.DiffuseColor = Color.White.ToVector3();
effect.View = view;
effect.Projection = projection;
effect.World =
transforms[mesh.ParentBone.Index] * scale * world * pruebaBox.WorldTransform;
}
mesh.Draw();
}
}
}