Mysterious Exception(StaticTriangleGroup)

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
J2T
Posts: 48
Joined: Sat Sep 20, 2008 6:20 pm
Contact:

Mysterious Exception(StaticTriangleGroup)

Post by J2T »

Hi there,

When i like to load a model twice i get a exception in initializeData(). So when i create a game, go back to menu and go back to game the exceptions is thrown. I remove the StaticTriagnleGroup from the physicssystem and the modelreference is correct as well(not null etc). When i create the game the second time the model is added in the contentmanager(through first time) and i get a reference to this!

The mysterious things is also that the exception i mostly thrown second time i go in game!But sometimes(not very often) at third or fourth!
Also very mysterouis is that when i create a game with the one map/model, go out, go in chossing antother map/model, go out, took the first again and so on it works correct!

So i've looked very long time for now and sry i dunno what it can cause!

Here is a screenshot from the exception: http://img137.imageshack.us/my.php?imag ... iontx6.jpg

I hope u can check it out with the help of the screen :) Or better to say i hope u have some suggestions...because u know whats happens in the method...

Thanks for advice!
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Mysterious Exception(StaticTriangleGroup)

Post by Norbo »

I believe this is caused by the device disliking the addMesh's access of the index buffer after it is used during rendering. The slightly intermittent nature is probably caused by the system deciding to dump it and allowing you to access it again, though I'm not an expert on that part. Loading another map forces the device to clear out the old data, making it accessible.

I haven't tested this, but could you try something like GraphicsDevice.Indices.SetData(null) during the quit to menu/reload phase? I'm sure there's something cleaner/more correct/more direct, but it's a start.

Also, using a content importer to gather up the triangles separately is a sure-fire way to avoid it (there's a sample on the xna website, 'picking with triangle accuracy,' that has a directly usable importer in it). This also avoids some other more obscure robustness issues with the convenience methods.
J2T
Posts: 48
Joined: Sat Sep 20, 2008 6:20 pm
Contact:

Re: Mysterious Exception(StaticTriangleGroup)

Post by J2T »

Okay to SetData(null) is not allowed. I've also tested Indices.Dispose() but then the exception 'can access a disposed object' is thrown when addMesh() like to get the Data from IndexBuffer!

So i've taken a look to the sample!I've referenced the TrianglePickingPipeline to my Content References and switch both map's(fbx) to this content processor!I've looked to the sample and i think thats it. Nothing more to do. I run and the same error occurs :-/ (give also initializeData() just the model)
I've looked at the sample again but i see the use the model normal and haven't methods where the get the indices/vertices right?Maybe i missunderstood something because i thought now i give initializeData() the vertices!

edith: ohhh i see there are now ther vertices in the Tag Propertie :D I haven't time now but will look again in few hours. I think this is the solution...
J2T
Posts: 48
Joined: Sat Sep 20, 2008 6:20 pm
Contact:

Re: Mysterious Exception(StaticTriangleGroup)

Post by J2T »

Thanks for your help...everything is okay now :D
J2T
Posts: 48
Joined: Sat Sep 20, 2008 6:20 pm
Contact:

Re: Mysterious Exception(StaticTriangleGroup)

Post by J2T »

Hi Norbo it me again with another problem :roll:

Okay since i switched the method there is a siginificant increase in loading time!before it was around 2seconds now around 10sec with the vertices list!Sure no big problem for a release but this was on our old map with 8k tris!
But i've testet a map which comes near our final trianglecount with around 60k!I've tested a map with 80k and after 2minutes waiting i cancelt :( So i dunno if there would ever happens something but i think it hangs!If not we can't accept a loading time in ranges like minutes!

This is the method i use to exctract the vertices and give BEPU!I think normal and near the picking sample:

Code: Select all

Dictionary<string, object>
tagData = (Dictionary<string, object>)BaseModel.Tag;
if (tagData == null)
{
                    throw new InvalidOperationException(
                    "Model.Tag is not set correctly. Make sure your model " +
                    "was built using the custom TrianglePickingProcessor.");
 }

Vector3[] vertices = (Vector3[])tagData["Vertices"];

verticesList = new List<StaticTriangleGroupVertex>(vertices.Length);

foreach (Vector3 vector in vertices)
       verticesList.Add(new StaticTriangleGroupVertex(vector));

group.initializeData(verticesList);
Do u have a Idea to solve this issue?
Thanks very much :)
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Mysterious Exception(StaticTriangleGroup)

Post by Norbo »

The initializeData method which takes only vertices compiles a new vertex and index buffer which is pretty time consuming. If you change the content importer to output a vertex list and a index list, the load time should drop considerably.

I'd also like to put in a save/load system for static triangle group sometime soon which would totally bypass the initializeData methods. I might be able to get it into v0.7.0; if not, I'll put it into v0.8.0.
J2T
Posts: 48
Joined: Sat Sep 20, 2008 6:20 pm
Contact:

Re: Mysterious Exception(StaticTriangleGroup)

Post by J2T »

Hi,
okay now the method tooks minutes or even hangs with just the small map!

Now i do:

Code: Select all

indicesList = new List<int>((List<int>)tagData["Indices"]);                        
group.initializeData(verticesList, indicesList); 
The rest before(verticesList) is the same!

The calculations of the indices in the processor:

Code: Select all

void FindVertices(NodeContent node)
        {
            // Is this node a mesh?
            MeshContent mesh = node as MeshContent;

            if (mesh != null)
            {
                // Look up the absolute transform of the mesh.
                Matrix absoluteTransform = mesh.AbsoluteTransform;

                // Loop over all the pieces of geometry in the mesh.
                foreach (GeometryContent geometry in mesh.Geometry)
                {
                    // Loop over all the indices in this piece of geometry.
                    // Every group of three indices represents one triangle.
                    foreach (int index in geometry.Indices)
                    {
                        // Look up the position of this vertex.
                        Vector3 vertex = geometry.Vertices.Positions[index];

                        // Transform from local into world space.
                        vertex = Vector3.Transform(vertex, absoluteTransform);

                        // Store this vertex.
                        vertices.Add(vertex);
                        indices.Add(index);
                    }
                }
            }

            // Recursively scan over the children of this node.
            foreach (NodeContent child in node.Children)
            {
                FindVertices(child);
           }
}
So i just at the index in the inner foreach to the indicesList!I dunno if this correct but when not i can't image that this would causes the long loading time. I guess when the indices wrong there just a incorrect result in physic!

So furthermore i tested this without the initializeData() method:

Code: Select all

group.vertexBuffer = verticesList;
group.indexBuffer = indicesList;
But then a exception is thrown when i set the worldMatrix of the group. And if i don't set the worldMatrix there is just another exception!

Any suggestions?
Thanks for advice :)
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Mysterious Exception(StaticTriangleGroup)

Post by Norbo »

Try this for the FindVertices method:

Code: Select all

        /// <summary>
        /// Helper for extracting a list of all the vertex positions in a model.
        /// </summary>
        void FindVertices(NodeContent node)
        {
            // Is this node a mesh?
            MeshContent mesh = node as MeshContent;

            if (mesh != null)
            {
                // Look up the absolute transform of the mesh.
                Matrix absoluteTransform = mesh.AbsoluteTransform;

                // Loop over all the pieces of geometry in the mesh.
                foreach (GeometryContent geometry in mesh.Geometry)
                {
                    int baseElement = vertices.Count;
                    foreach (Vector3 v in geometry.Vertices.Positions)
                    {
                        vertices.Add(Vector3.Transform(v, absoluteTransform));
                    }
                    foreach (int i in geometry.Indices)
                    {
                        indices.Add(baseElement + i);
                    }
            
                }
            }

            // Recursively scan over the children of this node.
            foreach (NodeContent child in node.Children)
            {
                FindVertices(child);
            }
        }
This version won't add in a large amount of redundant vertices (though I don't think its optimal) and should keep the indices correct through multiple meshes.
J2T
Posts: 48
Joined: Sat Sep 20, 2008 6:20 pm
Contact:

Re: Mysterious Exception(StaticTriangleGroup)

Post by J2T »

It works wonderfull thanks very much :wink:
Post Reply