notgiven688/jitterphysics2

Demo got stuck when emitting amount of shapes in tiny size at one

labbbirder opened this issue · 2 comments

The system seems to be unstable when deal with very tiny shapes. Tested on the latest master branch.

Steps to Reproduce

  1. change the Shoot method to produce tiny shapes:
    Random rand = new(123);
    public void ShootPrimitive()
    {
        const float primitiveVelocity = 20.0f;

        var pos = Camera.Position;
        var dir = Camera.Direction;

        var sb = World.CreateRigidBody();
        sb.Position = Conversion.ToJitterVector(pos);
        sb.Velocity = Conversion.ToJitterVector(dir * primitiveVelocity);

        //var ss = new BoxShape(0.3f);
        var ss = new BoxShape(rand.Next(1, 4) / 1000f);
        sb.AddShape(ss);
    }
  1. change the Control method to shoot a alot at once:
    for (int i = 0; i < 10; i++)
    {
        ShootPrimitive();
    }
  1. Run the demo and press space.

Additionally, there is another issue, may be a same problem as described below:

I continuously spawning tiny size shapes, meanwhile, I remove the rigidbody in world randomly. Soon got a exception says Shapes passed to default narrow phase collision handler are not associated with a rigid body

System.InvalidOperationException
  HResult=0x80131509
  Message=Shapes passed to default narrow phase collision handler are not associated with a rigid body.
  Source=Jitter2
  StackTrace:
   at Jitter2.World.Detect(Shape sA, Shape sB) in E:\UnityProjects\jitter2\src\Jitter2\World.Detect.cs:line 289
   at Jitter2.World.DetectCollisionsCallback(Batch batch) in E:\UnityProjects\jitter2\src\Jitter2\World.Step.cs:line 451
   at Jitter2.World.DetectCollisions(Boolean multiThread) in E:\UnityProjects\jitter2\src\Jitter2\World.Step.cs:line 764
   at Jitter2.World.Step(Single dt, Boolean multiThread) in E:\UnityProjects\jitter2\src\Jitter2\World.Step.cs:line 144
   at JitterDemo.Playground.Draw() in E:\UnityProjects\jitter2\src\JitterDemo\Playground.cs:line 127
   at JitterDemo.Renderer.OpenGL.GLFWWindow.Sync() in E:\UnityProjects\jitter2\src\JitterDemo\Renderer\OpenGL\GLFWWindow.cs:line 291
   at JitterDemo.Renderer.OpenGL.GLFWWindow.Open(CreationSettings settings) in E:\UnityProjects\jitter2\src\JitterDemo\Renderer\OpenGL\GLFWWindow.cs:line 179
   at JitterDemo.Program.Main() in E:\UnityProjects\jitter2\src\JitterDemo\Program.cs:line 25

Maybe it'a problem caused by DynamicTree.

I want to known why the tiny shapes so different, and how to solve the problems.

Here is the additional modification:

...
        if (Keyboard.KeyPressBegin(Keyboard.Key.M))
        {
            multiThread = !multiThread;
        }

-        if (!GuiRenderer.WantsCaptureKeyboard && Keyboard.KeyPressBegin(Keyboard.Key.Space))
+        if (!GuiRenderer.WantsCaptureKeyboard && Keyboard.IsKeyDown(Keyboard.Key.Space))
        {
-            ShootPrimitive();
+            for (int i = 0; i < 2; i++)
+            {
+                ShootPrimitive();
+            }
        }
+        if (rand.Next(2) == 1)
+        {
+            var idx = rand.Next(world.RigidBodies.Count);
+            if (idx>4) world.Remove(world.RigidBodies[idx]);
+        }

         base.Draw();
...

@labbbirder : Thanks for the report! Just to keep things sorted: Can you open a separate issue for the "Shapes passed to default narrow phase collision handler are not associated with a rigid body"-problem? Another user also reported this and I was not able to reproduce it, yet.