Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
0x5143 opened this issue Apr 4, 2023 · 7 comments

Comments

@0x5143
Copy link
Contributor

0x5143 commented Apr 4, 2023

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
Copy link
Contributor Author

0x5143 commented Apr 4, 2023

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
Copy link
Member

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

@0x5143
Copy link
Contributor Author

0x5143 commented Apr 5, 2023

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
Copy link
Member

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
Copy link
Contributor Author

0x5143 commented Apr 6, 2023

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
Copy link
Member

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 22dac46.

@0x5143
Copy link
Contributor Author

0x5143 commented Apr 6, 2023

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

@0x5143 0x5143 closed this as completed Apr 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants