Some physics trouble and how to do a fixed camera.

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
Batusay2kX
Posts: 2
Joined: Wed Sep 28, 2011 9:05 am

Some physics trouble and how to do a fixed camera.

Post by Batusay2kX »

Hi! I'm a little noob with 3D programming and I need a little help starting with this wonderful engine.

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();
            }
        }
    }
Batusay2kX
Posts: 2
Joined: Wed Sep 28, 2011 9:05 am

Re: Some physics trouble and how to do a fixed camera.

Post by Batusay2kX »

Allright, problem with the camera is gone...but I still having the slow rendering and the weird problem.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Some physics trouble and how to do a fixed camera.

Post by Norbo »

I tried scaling the model, because my first thought was that the model was bigger than the physic block...but no, wasn't that.
Are you sure? If it is truly the right scale, then it might also be a model offset. If the model is not centered on its center of mass, it won't line up with a physics object whose shape rotates around its center of mass. You can transform the model in local space before rendering to resolve this (or just change the model in a modelling program).

A good way to check for this sort of problem is to use the debug drawer. The phone demos have a version of the drawer for the phone, available on the downloads page: http://bepuphysics.codeplex.com/releases/view/62523

Hopefully it won't require much changing to run in Mango. The phone demos themselves show how to set it up and use it.
Allright, problem with the camera is gone...but I still having the slow rendering
If you mean the FPS is low, slow rendering is a bit out of scope for this forum. It's not related to the physics if you only have a couple of objects. Other places would be better for graphics-related questions. Note that an emulator does not reflect the performance of a device; if you're using an emulator, your computer might just be running it slowly. Per pixel lighting on the device will slow things down, too.

By the way, the most likely reason for your issues with the camera having to face sideways is that your camera's up vector was aligned with the look-at vector. The look-at vector cannot be parallel to the up vector; change the up vector if you want to look straight up or down along (0,1,0).
Post Reply