Potential Bug with Terrain Friction?

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
Abuzer
Posts: 20
Joined: Thu Jan 19, 2012 4:12 pm

Potential Bug with Terrain Friction?

Post by Abuzer »

Hi,

I was trying to modify the terrain (or my custom collidable that is based directly off of the terrain collidable), to use kinetic and static friction values that are something different than the default 0.6 and 0.8. I realized that no matter what i changed the values to, it didn't seem to affect what I saw in the game. So I dug a bit deeper into the BEPU code, and while I didn't spot the bug, I did see something that might help tracking down if there really is a bug or not in the terrain friction and material usage.

Simply put, in ContactFrictionConstraint.cs in the setup function, when I look at the MaterialInteraction values for contactManifordConstraint, they seem to keep coming thru as their default values (0.6 and 0.8). If i hard code the values I want in that function, then I get the expected visual results in the app. I've also checked to make sure that calls to UpdateMaterialProperties() comes back with the expected values, so the issue seems to be somewhere when it's going from the blended material values to the point they're fed into the contactManifordConstraint, and I didn't dig any deeper at the moment particularly the intertwined calls between contact manifolds, and pair handlers seem to get get a bit hairy to track without knowing the exact architecture and naming convention in BEPU.

It'd be awesome if I could get a hand at trying to nail this issue down.

Thanks a bunch ;)
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Potential Bug with Terrain Friction?

Post by Norbo »

I am unable to reproduce this; based on the cited default material settings, it sounds like it's happening in a pre-v1.2.0 version. If that's the case, does updating to v1.2.0 help? (Note that the default blending has changed to multiplicative in v1.2.0, but it can be set to anything, including the old averaging, by changing the MaterialManager.MaterialBlender delegate.)
Abuzer
Posts: 20
Joined: Thu Jan 19, 2012 4:12 pm

Re: Potential Bug with Terrain Friction?

Post by Abuzer »

Ok, so I upgraded to 1.2 now. Had to move some of my BEPU changes over into the new version as well. Luckily it was quick. :)

Unfortunately I'm still seeing the same issue though. Of course now the kinetic and static coefficients are coming thru as 0.8 and 1.0; and again when I hardcode the values in:

Code: Select all

public void Setup(ContactManifoldConstraint contactManifoldConstraint, Contact contact)
to values like 2.0 and 3.0, I get the expected visual results, but otherwise the material values I'm setting on the terrain-like object do not seem to come thru.

I also checked and the material is being set on the ContactConstraint via a call to UpdateMaterialProperties in StandardPairHandler when this section of code triggers:

Code: Select all

        public BroadPhaseOverlap BroadPhaseOverlap
        {
            get
            {
                return broadPhaseOverlap;
            }
            set
            {
                broadPhaseOverlap = value;
                Initialize(value.entryA, value.entryB);
            }
        }
I doubt it's a bug in BEPU, but I don't know what I might be doing wrong either. :?

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

Re: Potential Bug with Terrain Friction?

Post by Norbo »

Unfortunately, I don't have any good theories. Could you reproduce it in a BEPUphysicsDemos demo for me to look at?
Abuzer
Posts: 20
Joined: Thu Jan 19, 2012 4:12 pm

Re: Potential Bug with Terrain Friction?

Post by Abuzer »

Yeah tried it with the terrain demo and it works as expected. I'm wondering if some how my custom shape has the bug in it where it pulls/generates the contact constraints with the wrong material settings. My gut reaction is that I might not be overriding a particular virtual function and it just ends up using a base class implementation. Anyway. I'll keep looking.
Abuzer
Posts: 20
Joined: Thu Jan 19, 2012 4:12 pm

Re: Potential Bug with Terrain Friction?

Post by Abuzer »

Ok. I think I might've found the issue, though I'm not 100% about it.

So it looks like the issue shows itself when you have a compound object colliding the terrain, instead of a simple single convex shape colliding with it. I've also found what seems to be the fix, but like I said, I'm not 100%.

Here's how I changed the TerrainDemo to get it going:

Code: Select all

        public TerrainDemo(DemosGame game)
            : base(game)
        {
            //x and y, in terms of heightmaps, refer to their local x and y coordinates.  In world space, they correspond to x and z.
            //Setup the heights of the terrain.
            //[The size here is limited by the Reach profile the demos use- the drawer draws the terrain as a big block and runs into primitive drawing limits.
            //The physics can support far larger terrains!]
            int xLength = 180;
            int zLength = 180;

            float xSpacing = 8f;
            float zSpacing = 8f;
            var heights = new float[xLength, zLength];
            for (int i = 0; i < xLength; i++)
            {
                for (int j = 0; j < zLength; j++)
                {
                    float x = i - xLength / 2;
                    float z = j - zLength / 2;
                    //heights[i,j] = (float)(x * y / 1000f);
                    heights[i, j] = (float)(10 * (Math.Sin(x / 8) + Math.Sin(z / 8)));
                    //heights[i,j] = 3 * (float)Math.Sin(x * y / 100f);
                    //heights[i,j] = (x * x * x * y - y * y * y * x) / 1000f;
                }
            }
            //Create the terrain.
            var terrain = new Terrain(heights, new AffineTransform(
                    new Vector3(xSpacing, 1, zSpacing),
                    Quaternion.Identity,
                    new Vector3(-xLength * xSpacing / 2, 0, -zLength * zSpacing / 2)));

            terrain.Material.KineticFriction = 0f;
            terrain.Material.StaticFriction = 0f;

            //terrain.Thickness = 5; //Uncomment this and shoot some things at the bottom of the terrain! They'll be sucked up through the ground.

            Space.Add(terrain);
            //for (int i = 0; i < 3; i++)
            //{
            //    for (int j = 0; j < 3; j++)
            //    {
            //        for (int k = 0; k < 5; k++)
            //        {
            //            Space.Add(new Box(
            //                new Vector3(0 + i * 4, 100 - j * 10, 0 + k * 4),
            //                2 + i * j * k,
            //                2 + i * j * k,
            //                2 + i * j * k,
            //                4 + 20 * i * j * k));
            //        }
            //    }
            //}

            //Build the first body
            var bodies = new List<CompoundShapeEntry>() 
            {
                new CompoundShapeEntry(new BoxShape(1,1,1), new Vector3(0, 50, 0), 1),
                new CompoundShapeEntry(new BoxShape(1,1,1), new Vector3(1, 50.5f, 0), 1),
            };
            var cb2 = new CompoundBody(bodies, 4);
            Space.Add(cb2);

            game.ModelDrawer.Add(terrain);

            game.Camera.Position = new Vector3(0, 30, 20);

        }
Basically, I removed the single boxes, and added a compound object to the simulation. I also tried to kill the friction and in theory the boxes should slide around but they don't (or didn't when I tried it), whereas the single boxes do slide around.

As far as tracking down what I think the bug is, it looks like it might be this function in CompoundTerrainPairHandler.cs:

Code: Select all

        protected override void UpdateContainedPairs()
        {           
            //Could go other way; get triangles in mesh that overlap the compound.
            //Could be faster sometimes depending on the way it's set up.
            var overlappedElements = Resources.GetCompoundChildList();
            compoundInfo.hierarchy.Tree.GetOverlaps(terrain.boundingBox, overlappedElements);
            for (int i = 0; i < overlappedElements.count; i++)
            {
                TryToAdd(overlappedElements.Elements[i].CollisionInformation, terrain, overlappedElements.Elements[i].Material);

            }

            Resources.GiveBack(overlappedElements);
        }
The TryToAdd() function doesn't send in the terrain material. It only sends in the other collidable's material. If I change that line to be:

Code: Select all

                TryToAdd(overlappedElements.Elements[i].CollisionInformation, terrain, overlappedElements.Elements[i].Material, terrain.Material);
Then it looks like it works as intended.

Let me know what you make of this.

Cheers
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Potential Bug with Terrain Friction?

Post by Norbo »

That is indeed a bug and is now fixed in the development version. A similar problem existed for the other meshes and StaticGroup, too.

Thanks for the report!
Post Reply