Physics Rendering

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
Spykam22
Posts: 13
Joined: Fri Dec 09, 2011 9:48 pm

Physics Rendering

Post by Spykam22 »

Hello!

I created a class like entity model that allows me to render to texture instead using the original class (with DrawableGameComponent). I was having problems with the original class because it was rendering to the back buffer and getting in the way of the HUD and menu. Here is my code:

Code: Select all

public static class EntityModel
    {
        public static List<Entity> Entities = new List<Entity>();

        public static Matrix Transform;

        static Matrix[] boneTransforms;

        static Model currentModel;
        public static Model CurrentModel
        {
            get { return currentModel; }
            set { currentModel = value; }
        }

        public static void Add(Entity EntitytoAdd, Model modelToAdd, Matrix transform)
        {
            Entities.Add(EntitytoAdd);
            currentModel = modelToAdd;
            Transform = transform;

            if (boneTransforms != null)
                boneTransforms = new Matrix[modelToAdd.Bones.Count];
                

            foreach (ModelMesh mesh in modelToAdd.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
                }
            }
        }

        public static void Draw(Matrix viewMatrix, Matrix projectionMatrix)
        {
            for (int i = 0; i < Entities.Count; i++)
            {
                Matrix worldMatrix = Transform * Entities[i].WorldTransform;

                currentModel.CopyAbsoluteBoneTransformsTo(boneTransforms);
                foreach (ModelMesh mesh in currentModel.Meshes)
                {
                    foreach (BasicEffect effect in mesh.Effects)
                    {
                        effect.World = boneTransforms[mesh.ParentBone.Index] * worldMatrix;
                        effect.View = viewMatrix;
                        effect.Projection = projectionMatrix;
                    }

                    mesh.Draw();
                }
            }
        }
    }
It seems to work for my other boxes, but my ground will not show up. If I remove the other boxes, then the ground shows up. I made my ground like this:

Code: Select all

space.Add(new Box(Vector3.Zero, 2000, 1f, Height));
I made my other boxes like this:

Code: Select all

            space.Add(new Box(new Vector3(0, 4, 0), 1, 1, 1, 1));
            space.Add(new Box(new Vector3(0, 8, 0), 1, 1, 1, 1));
            space.Add(new Box(new Vector3(0, 12, 0), 1, 1, 1, 1));
            space.Add(new Box(new Vector3(0, 16, 0), 1, 1, 1, 1));

Code: Select all

foreach (Entity e in space.Entities)
            {
                Box box = e as Box;
                if (box != null) //This won't create any graphics for an entity that isn't a box since the model being used is a box.
                {
                    Matrix scaling = Matrix.CreateScale(box.Width, box.Height, box.Length); //Since the cube model is 1x1x1, it needs to be scaled to match the size of each individual box.
                    //Add the drawable game component for this entity to the game.
                    EntityModel.Add(e, content.Load<Model>("Game/Player/cube"), scaling);
                }
            }
In my draw method, I added this:

Code: Select all

EntityModel.Draw(viewMatrix, projectionMatrix);

But my ground still does not render. All the other boxes are there, and I know my ground is there because the boxes look like they are sitting on an invisible box.

http://img829.imageshack.us/img829/7596/kams36011.png

Any help???
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Physics Rendering

Post by Norbo »

I don't see anything wrong at a glance. Given that they are all using the same code path and the regular size boxes seem to work, my guess would be something to do with a sneaky transform somewhere.

Also, this line:

Code: Select all

space.Add(new Box(Vector3.Zero, 2000, 1f, Height));
does not match the width-height-length convention defined by this line:

Code: Select all

Matrix scaling = Matrix.CreateScale(box.Width, box.Height, box.Length);
That, in isolation, doesn't explain the problem directly, but it might be a hint that there's a more meaningful disagreement somewhere.

The only other difference between the boxes is the IsDynamic state. If the boxes are ever distinguished by their IsDynamic or IsActive states in rendering, that might be related.
Post Reply