Search code examples
c#3dxnageometrybounding

BoundingSphere Update


Hallo I'm writting little RPG about Ants in XNA 4.0. I have made LoadModel class to fbx model load, and to create bounding spheres. I have one general bounding sphere at the model created by merging model mesh. Now i created model with additional sphere which represent my bounding sphere in game. I just checking if the Mesh name is "BoundingSphere" and when it is I'm adding mesh.BoundingSphere to my array of bs. Now i dont now how to update those bs... My code attempts:

 private void buildBoundingSphere()
    {

        BoundingSphere sphere = new BoundingSphere(Vector3.Zero, 0);
        List<BoundingSphere> spheres = new List<BoundingSphere>();
        foreach (ModelMesh mesh in Model.Meshes)
        {
            if (mesh.Name.Contains("BoundingSphere") )
            {
                BoundingSphere transformed = mesh.BoundingSphere.Transform(modelTransforms[mesh.ParentBone.Index])
            spheres.Add(transformed);
            sphere = BoundingSphere.CreateMerged(sphere, transformed);

            }
        }


        this.boundingSphere = sphere;
        this.spheres = spheres.ToArray();   
    }

Now the BoundingSphere array get/set:

public BoundingSphere[] spheres
    {

        get
        {
            // No need for rotation, as this is a sphere
            List<BoundingSphere> spheres = new List<BoundingSphere>();
            foreach (ModelMesh mesh in Model.Meshes)
            {
                Matrix worldTransform = Matrix.CreateScale(Scale)* Matrix.CreateTranslation(Position);

                if (mesh.Name.Contains("BoundingSphere")) {

                BoundingSphere transformed = mesh.BoundingSphere.Transform(worldTransform);
                spheres.Add(transformed);
                }
            }

            return spheres.ToArray();
        }
        set{}
    }

I have 2 spheres in model, and both of them are in the same place. Thank you for any hint.


Solution

  • For one thing your setter is empty. If you're going for an auto-generated setter, use set; instead of set{}. So it seems everything you do in the buildBoundingSphere() is lost. Verify that first.

    Another minor issue is that your bounding sphere accumulator starts as a point at the origin. This assumes the origin will be inside the final sphere, which is not true in general. Really you should somehow only generate the starting sphere once you know the first sphere.

    I can't tell anything more from this snippet. If you're going to transform all the spheres in the getter, is it by the object's current transform? Scale and Position don't seem dependent on the mesh it's transforming the sphere of, so that construction of the worldTransform matrix could be moved outside the loop.