bepu / bepuphysics2

Pure C# 3D real time physics simulation library, now with a higher version number.
Apache License 2.0
2.25k stars 261 forks source link

The change in tree cost is an invalid value, strongly implying the tree bounds have been corrupted by infinites or NaNs. #262

Closed 0x5143 closed 1 year ago

0x5143 commented 1 year ago

I create 1000 SponsorCharacterAI in CharacterDemo, after a little minutes it will cause exception below:

System.InvalidOperationException:“The change in tree cost is an invalid value, strongly implying the tree bounds have been corrupted by infinites or NaNs. If this happened in the broad phase's use of the tree, it's likely that there are invalid poses or velocities in the simulation, possibly as a result of bugged input state or constraint configuration. Try running the library with debug asserts enabled to narrow down where the NaNsplosion started.”

>   BepuPhysics.dll!BepuPhysics.Trees.Tree.GetRefineTuning(int frameIndex, int refinementCandidatesCount, float refineAggressivenessScale, float costChange, out int targetRefinementCount, out int refinementPeriod, out int refinementOffset) 行 165   C#
    BepuPhysics.dll!BepuPhysics.Trees.Tree.RefitAndRefineMultithreadedContext.CreateRefinementJobs(BepuUtilities.Memory.BufferPool pool, int frameIndex, float refineAggressivenessScale) 行 95  C#
    BepuPhysics.dll!BepuPhysics.CollisionDetection.BroadPhase.Update(BepuUtilities.IThreadDispatcher threadDispatcher) 行 204    C#
    BepuPhysics.dll!BepuPhysics.Simulation.CollisionDetection(float dt, BepuUtilities.IThreadDispatcher threadDispatcher) 行 225 C#
    BepuPhysics.dll!BepuPhysics.DefaultTimestepper.Timestep(BepuPhysics.Simulation simulation, float dt, BepuUtilities.IThreadDispatcher threadDispatcher) 行 41 C#
    Demos.dll!Demos.Demos.Characters.CharacterDemo.Update(DemoUtilities.Window window, DemoRenderer.Camera camera, DemoUtilities.Input input, float dt) 未知
    Demos.dll!Demos.DemoHarness.Update(float dt)    未知
    Demos.dll!Demos.GameLoop.Update(float dt)   未知
    DemoUtilities.dll!DemoUtilities.Window.Run(System.Action<float> updateHandler, System.Action<BepuUtilities.Int2> onResize)  未知
    Demos.dll!Demos.Program.Main()  未知
0x5143 commented 1 year ago

Final I found is postmetric is NaN, because of NaN of child (Max and Min are all NaN) in below code:

struct Tree
{
    unsafe float RefitAndMeasure(ref NodeChild child)
}
RossNordby commented 1 year ago

In the CharacterDemo? Could you provide the modified version that reproduces the problem?

0x5143 commented 1 year ago

new 1000 SponsorCharacterAI in Demos.Demos.Characters.CharacterDemo, like below

            const int characterCount = 1000;
            characterAIs = new QuickList<SponsorCharacterAI>(characterCount, BufferPool);
            var characterCollidable = Simulation.Shapes.Add(new Capsule(0.5f, 1f));
            for (int i = 0; i < characterCount; ++i)
            {
                var position2D = newtArenaMin + (newtArenaMax - newtArenaMin) * new Vector2(random.NextSingle(), random.NextSingle());
                var targetPosition = 0.5f * (newtArenaMin + (newtArenaMax - newtArenaMin) * new Vector2(random.NextSingle(), random.NextSingle()));
                characterAIs.AllocateUnsafely() = new SponsorCharacterAI(characterControllers, characterCollidable, new Vector3(position2D.X, 5, position2D.Y), targetPosition);
            }
RossNordby commented 1 year ago

Just adding a bunch of characters like that doesn't reproduce a crash after about an hour of idling. Attempting to update them does if there are no newts, because SponsorCharacterAI expects a nonzero number of newts, but that should be a pretty instantaneous crash.

What other changes were made to the CharacterDemo, if any? And is this using the latest master version?

0x5143 commented 1 year ago

Comment code associated with newt like below, and remove spooked condition let it jump sometimes:

            //for (int i = 0; i < newts.Count; ++i)
            //{
            //    ref var newtPosition = ref simulation.Bodies[newts[i].BodyHandle].Pose.Position;
            //    var offset = newtPosition - body.Pose.Position;
            //    var distance = offset.Length();
            //    if (distance > 1e-10f)
            //    {
            //        var influenceMagnitude = 1f / (distance * 0.1f + .1f);
            //        influenceSum -= new Vector2(offset.X, offset.Z) * influenceMagnitude / distance;
            //    }
            //    if (distance < 20)
            //    {
            //        spooked = true;
            //    }
            //}
            ////We want target position relative influence to be consistent regardless of newt count.
            //influenceSum /= newts.Count;

then CharacterAI will move and jump.

I has tested in the master code and v2.4.0, have the same exception.

RossNordby commented 1 year ago

The sponsors AI also relied on the newts being around to provide nonzero movement influence; if they reach the final target, there will be a divide by zero. Went ahead and guarded against it in 22dac469c92e5ffbeb3764feedb9eb90326361ac.

0x5143 commented 1 year ago

Thanks reply. I have tested for a long time and not exception, it look work well.