Problems with graphic matching complex objects

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
needshelprendering
Posts: 25
Joined: Mon Sep 17, 2012 1:17 am

Problems with graphic matching complex objects

Post by needshelprendering »

Hi Norbo, I recently came back to my hobby of making a game in XNA. I had a few problems with the graphics not matching the collision, I implemented the code you have in your graphics matching demo. It works for semi-complex models such as guns, sorta. But more complex models such as vehicles or buildings (actually those are fairly simple) don't match up at all. I am using the Character controller, loading the model as a convex hull, offsetting the graphics transform, then drawing it.

Here is a photo to let you know exactly what is happening, the character floats slightly above the truck bed:
Image

I can also walk through the cab:
Image



Here is the code where I construct the object:

Code: Select all

BepuEntity creatBarrel(Vector3 position, float mass)
        {
        BepuEntity bar = new BepuEntity();
        bar.modelName = "Levels\\Test\\untitled"; //The truck model
        bar.diffName = "Levels\\Test\\TruckR"; //Diffuse texture
        bar.normName = "Levels\\Test\\TruckRNrml"; //Normal texture
        bar.specName = "Levels\\Test\\TruckRNrml"; //Specular
        bar.LoadContent(); //Loads the model and textues
        TriangleMesh.GetVerticesAndIndicesFromModel(bar.model, out vertices, out indices);
        //for (int i = 0; i < vertices.Length; i++)
        //    vertices[i] = Vector3.Transform(vertices[i], Matrix.CreateScale(1 - 0.01f)); //I tried the graphics matching solution I found on the forums, didn't work.
        bar.body = new ConvexHull(vertices, 10);
        bar.localGraphicsTransform = Matrix.CreateTranslation(-bar.body.Position);//.body.Position
        bar.body.Position = position;
        bar.Position = position;// position;
        bar.configureEvents();
        space.Add(bar.body);
        bar.body.BecomeKinematic();
        children.Add(bar);
        return bar;
        }
Here is where the localGraphicsTransform comes into play:

Code: Select all

public override void Update(GameTime gameTime) //This is called for all BEPU objects in the game.
        {
            //Console.WriteLine(body.WorldTransform.Translation.ToString());
            worldTransform = localGraphicsTransform * body.WorldTransform;
        }
Anything helps, thanks.
mohammadAdnan
Posts: 48
Joined: Sat Jan 26, 2013 5:54 pm

Re: Problems with graphic matching complex objects

Post by mohammadAdnan »

Ok Norbo answer this hundred of times :) , i faced your problem before and it was then very tricky , but here the complete solution :
mohammadAdnan wrote:Hello Norbo :) , my team and I will never forgot all your help

I follow your steps for convex hull and now it works fine :) , I just send the hull entity created with each static mesh :) to the draw method like this :

Code: Select all

        public ModelGraphics(Model model,ConvexHull hul , Matrix transform)
        {
            this.model = model;
            this.Transform = transform;
            this.hull = hul;

            //Collect any bone transformations in the model itself.
            //The default cube model doesn't have any, but this allows the StaticModel to work with more complicated shapes.
            boneTransforms = new Matrix[model.Bones.Count];
            foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
                }
            }
        }

        public void Draw(Camera camera)
        {
            model.CopyAbsoluteBoneTransformsTo(boneTransforms);
            foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.World = boneTransforms[mesh.ParentBone.Index] * hull.WorldTransform;
                    effect.View = camera.ViewMatrix;
                    effect.Projection = camera.projection;
                }
                mesh.Draw();
            }
        }
that's it as you said I have to update the convex hull entity for each model :)

Code: Select all

   
             // Level 1
            Vector3[] vertices6;
            int[] indices6;
            TriangleMesh.GetVerticesAndIndicesFromModel(Content.Load<Model>("Models/Level1/stairs"), out vertices6, out indices6);
            //Give the mesh information to a new StaticMesh.  
            //Give it a transformation which scoots it down below the kinematic box entity we created earlier.
            var mesh7 = new StaticMesh(vertices6, indices6, new AffineTransform(Matrix3X3.CreateScale(2), new Vector3(0, 200, 0)));
            // Add hull
            ConvexHull hull6 = new ConvexHull(vertices6, 10);
            //Add it to the space!
            space.Add(hull6);
            // Make it visible to
            models.Add(new ModelGraphics(Content.Load<Model>("Models/Level1/stairs"), hull6, hull6.WorldTransform));
and while doing this I face problem , as with each Model there is the same model drawn beside it and I found that the problem was from :

Code: Select all

            foreach (Entity e in space.Entities){
                ModelDrawer.Add(e);
            }
as I did not know that Hull is an entity !!!

Norbo , my first question is do you advice my to delete the ModelDrawer calss from my project and just used my own drawer class ? or it's not problem to use both at the same time like this :

Code: Select all

        public void Draw(Matrix View , Matrix projection) {

            graphicsDevice.RasterizerState = RasterizerState.CullClockwise;

            foreach(ModelGraphics model in this.models)
            {
                model.Draw(camera);
            }

            ModelDrawer.Draw(camera.ViewMatrix, camera.projection);
        }
    }
2 : if I delete the ModelDrawer.Draw(camera.ViewMatrix, camera.projection); , then the textures of models dose not appear(even they are drawn with another drawer class separated from ModelDrawer of BEPU) , but the model still colliode with my charachter !!

3 : I feel that while I am walking my charachter dose not walking normally ( wide steps ) and very large with respect the level models ?
needshelprendering
Posts: 25
Joined: Mon Sep 17, 2012 1:17 am

Re: Problems with graphic matching complex objects

Post by needshelprendering »

mohammadAdnan wrote:Ok Norbo answer this hundred of times :) , i faced your problem before and it was then very tricky , but here the complete solution :
mohammadAdnan wrote:Hello Norbo :) , my team and I will never forgot all your help

I follow your steps for convex hull and now it works fine :) , I just send the hull entity created with each static mesh :) to the draw method like this :

Code: Select all

        public ModelGraphics(Model model,ConvexHull hul , Matrix transform)
        {
            this.model = model;
            this.Transform = transform;
            this.hull = hul;

            //Collect any bone transformations in the model itself.
            //The default cube model doesn't have any, but this allows the StaticModel to work with more complicated shapes.
            boneTransforms = new Matrix[model.Bones.Count];
            foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
                }
            }
        }

        public void Draw(Camera camera)
        {
            model.CopyAbsoluteBoneTransformsTo(boneTransforms);
            foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.World = boneTransforms[mesh.ParentBone.Index] * hull.WorldTransform;
                    effect.View = camera.ViewMatrix;
                    effect.Projection = camera.projection;
                }
                mesh.Draw();
            }
        }
that's it as you said I have to update the convex hull entity for each model :)

Code: Select all

   
             // Level 1
            Vector3[] vertices6;
            int[] indices6;
            TriangleMesh.GetVerticesAndIndicesFromModel(Content.Load<Model>("Models/Level1/stairs"), out vertices6, out indices6);
            //Give the mesh information to a new StaticMesh.  
            //Give it a transformation which scoots it down below the kinematic box entity we created earlier.
            var mesh7 = new StaticMesh(vertices6, indices6, new AffineTransform(Matrix3X3.CreateScale(2), new Vector3(0, 200, 0)));
            // Add hull
            ConvexHull hull6 = new ConvexHull(vertices6, 10);
            //Add it to the space!
            space.Add(hull6);
            // Make it visible to
            models.Add(new ModelGraphics(Content.Load<Model>("Models/Level1/stairs"), hull6, hull6.WorldTransform));
and while doing this I face problem , as with each Model there is the same model drawn beside it and I found that the problem was from :

Code: Select all

            foreach (Entity e in space.Entities){
                ModelDrawer.Add(e);
            }
as I did not know that Hull is an entity !!!

Norbo , my first question is do you advice my to delete the ModelDrawer calss from my project and just used my own drawer class ? or it's not problem to use both at the same time like this :

Code: Select all

        public void Draw(Matrix View , Matrix projection) {

            graphicsDevice.RasterizerState = RasterizerState.CullClockwise;

            foreach(ModelGraphics model in this.models)
            {
                model.Draw(camera);
            }

            ModelDrawer.Draw(camera.ViewMatrix, camera.projection);
        }
    }
2 : if I delete the ModelDrawer.Draw(camera.ViewMatrix, camera.projection); , then the textures of models dose not appear(even they are drawn with another drawer class separated from ModelDrawer of BEPU) , but the model still colliode with my charachter !!

3 : I feel that while I am walking my charachter dose not walking normally ( wide steps ) and very large with respect the level models ?
Thank you, I will try this out in a bit.
Post Reply