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

[Bullet] Severe lag when adding a collider to a physics object with many colliders #22064

Closed
Tracked by #45022
aaronfranke opened this issue Sep 14, 2018 · 16 comments
Closed
Tracked by #45022

Comments

@aaronfranke
Copy link
Member

aaronfranke commented Sep 14, 2018

Godot version: fc50728 (#21808) and later are affected, ec4b8e4 and prior are unaffected.

OS/device including version: Xubuntu 18.04 LTS 64-bit

Issue description: EDIT: This issue has been a wild ride. I don't think concave support is the solution anymore. As mentioned in this comment, the main limitation is that I am unable to get the same level of performance using compound box colliders compared to using a single concave collider in 3.0.

When using many BoxShape CollisionShape nodes, my project gets about 1 FPS when generating, and once generated, I get about 450 FPS. If I comment out add_child then it doesn't lag, so it's not my game's math slowing things down, it's just adding the collider node to the object, and having the object exist with box colliders is apparently slower than Concave.

However, when using a Concave collider in Godot 3.0, my project runs fine and I get about 700 FPS once it's generated. In Godot 3.1+, this spams warnings and produces weird behavior.

Steps to reproduce: See comments for more information.

CC @AndreaCatania

@akien-mga akien-mga added this to the 3.1 milestone Sep 14, 2018
@AndreaCatania AndreaCatania self-assigned this Sep 14, 2018
@AndreaCatania
Copy link
Contributor

Can you update a sample project please?

@aaronfranke
Copy link
Member Author

aaronfranke commented Sep 23, 2018

EDIT: I don't know how accurately this test project shows the problems, my main project uses a custom concave mesh generated from code which would be a lot of work to try to slim down to a minimal reproduction project.

ConcaveRegressionTestProject.zip

I'm getting different and weird errors with the latest master. Concave seems to be able to collide with other objects (most of the time...?). (Note: The concave collision shapes are just test points and don't represent the cube perfectly)

For anything set to "Kinematic":

WARNING: copyAllOwnerShapes: This shape is not supported to be kinematic!
   At: modules/bullet/rigid_body_bullet.cpp:243.
WARNING: copyAllOwnerShapes: This shape is not supported to be kinematic!
   At: modules/bullet/rigid_body_bullet.cpp:243.

W 0:00:00:0891   This shape is not supported to be kinematic!
  <C Source>     modules/bullet/rigid_body_bullet.cpp:243 @ copyAllOwnerShapes()
  <Stack Trace>  :0 @ void Godot.NativeCalls.godot_icall_2_363(IntPtr , IntPtr , IntPtr , Boolean )()
                 Node.cs:309 @ void Godot.Node.AddChild(Godot.Node , Boolean )()
                 TestRigidBody.cs:18 @ void TestRigidBody._Ready()()

For RigidBodies set to Kinematic, they simply don't move. At all. Not by user code or anything. See the example on the far left in the test project.

RigidBodies with Concave colliders are not able to hit other Concave colliders. See the example in the middle-left in the test project. They just phase through each other.

RigidBodies with Cube colliders are able to hit RigidBodies with Concave colliders. See the example in the middle-right in the test project. But sometimes, they both disappear on contact.

I also get inconsistent results when a Concave collider spawns inside of another one. With the example in the middle, sometimes these objects disappear immediately, sometimes they don't.

And of course, Cube colliders can still collide with each other. I added this as an example just to prove that physics does work in this project. See the example on the far right in the test project.

Here's what it looks like after a few seconds on my system:

test2

But sometimes this happens and they disappear (ZERO code has been changed):

test

@AndreaCatania
Copy link
Contributor

AndreaCatania commented Sep 23, 2018 via email

@aaronfranke
Copy link
Member Author

aaronfranke commented Sep 23, 2018

It used to work just fine though (not everything in the test project, just one specific thing), therefore it is a regression. In my main project I have a single movable concave object which collides with my character in Godot 3.0 but disappears with the latest master. (When I say 3.0 I'm actually testing ec4b8e4 but I assume it applies to 3.0 as well since it has worked for a long time for me)

I can identify two specific regressions in my main project:

  • When my character comes into contact with a non-kinematic concave RigidBody, everything disappears (video demo). Note how the speed indicator changes to "nan.00 m/s" and the debugger spams errors. This problem does not exist in 3.0 (my character collides with the object as you'd expect)

  • Concave RigidBodies inside of others causes them to disappear in master, in 3.0 they are just fine with existing and will collide with me but not each other (which is fine, I know concave-concave collision is an expensive operation, I don't expect that anywhere, I only showed it in the demo because I was trying many things to figure out what exactly is wrong).

Also, sometimes the above problems will crash Godot entirely (not just the running project) (segfaults).

@AndreaCatania
Copy link
Contributor

Can you please try to change RigidBody with StaticBody for all bodies that use concave shape?

@aaronfranke
Copy link
Member Author

I need my concave objects to be able to move around. StaticBody does not do what I need.

@AndreaCatania
Copy link
Contributor

Check this please: #22064 (comment)

@AndreaCatania AndreaCatania removed the bug label Sep 24, 2018
@AndreaCatania AndreaCatania removed this from the 3.1 milestone Sep 24, 2018
@AndreaCatania
Copy link
Contributor

Note: Depending on the type of shape that you need you can recreate it using primitive shapes

@aaronfranke
Copy link
Member Author

aaronfranke commented Sep 24, 2018

I've used concave shapes for kinematic bodies in Godot 3.0 and in Unity without issues.

@aaronfranke
Copy link
Member Author

aaronfranke commented Nov 5, 2018

I haven't experienced any crashing or errors as before and Concave colliders seem to work so far but I still get warned with Concave being unsupported with KinematicBody (as you said it was).

I tried replacing the Concave collider with individual BoxShape Colliders for my generated voxel object. I generate on a per-chunk basis then check how much time has elapsed to decide whether or not to generate another one. Also, I'm using C#, and I've tested both Alpha 1 and the master branch.

  • When using a Concave collider, my project runs fine and I get about 700 FPS once it's generated.

  • When using many BoxShape CollisionShape nodes, my project gets about 1 FPS when generating, and once generated, I get about 450 FPS. If I comment out the AddChild(c); line then it doesn't lag, so it's not my game's math slowing things down, it's just adding the collider node to the object, and having the object exist with box colliders is apparently slower than Concave.

It's probably a bug in its own right, but this wouldn't be a problem if KinematicBodies officially supported Concave shapes without throwing warnings.

@aaronfranke aaronfranke changed the title Regression: Concave collision shapes no longer work after #21808 Add support for Concave collision shapes with Kinematic bodies Nov 12, 2018
@AndreaCatania
Copy link
Contributor

Mostly because move a concave shape is not an optimal operation we have this restriction. However, there are physics engine that supports this feature, but unfortunately bullet doesn't.

However, add a new shape (especially a box shape, should not impact performance) so this is something to address.

@aaronfranke
Copy link
Member Author

aaronfranke commented Feb 28, 2020

@AndreaCatania Yeah, ignore my original request, concave isn't the solution (even if it used to kinda work in 3.0), I just need the ability to build compound colliders without severe lag.

For compound colliders, see godotengine/godot-proposals#535

For the severe lag, I'll rename the title of this issue and edit the OP to just reflect that.

But also, the weird behavior and random crashes mentioned in this post likely still need fixing - it should be at least consistent.

@aaronfranke aaronfranke changed the title Add support for Concave collision shapes with Kinematic bodies Severe lag when adding a collider to a physics object with many colliders Feb 28, 2020
@pouleyKetchoupp
Copy link
Contributor

@aaronfranke Could you please provide a MRP for this issue? I suspect this issue is specific to Bullet and similar to the scenario from #45360 but I can't confirm without an actual test case.

@aaronfranke
Copy link
Member Author

aaronfranke commented Apr 14, 2021

@pouleyKetchoupp Here is a minimal reproduction project (for 3.x), it slows significantly after 1000, and it's crawling at 2000. The framerate at any given shape count is about 1/5th of what it is at half that shape count. My expectation would be that it's a linear relationship (or better). 22064-MRP.zip

Also, check out the Voxel demo from the Godot demo projects repo. Adding the colliders as children is very slow there too. It's not as significant as the MRP because each chunk is its own StaticBody so each StaticBody deals with less shapes. To push the Voxel demo to its limit, disable VSync, then try the following (not at the same time):

  • If you comment out line 186 of chunk.gd (and line 77 of player.gd so that you don't fall), then the chunks generate much faster and the framerate is higher.
  • If you increase the number on line 19 of terrain_generator.gd, it slows down. A value of 0.1 brings the demo to its knees. A value of 0.5 turns the demo into a slideshow.

@pouleyKetchoupp
Copy link
Contributor

@aaronfranke Thanks for the repro cases! I can confirm that in both cases, switching to Godot Physics solves the problem.

The bottleneck in Bullet is this callstack:

 godot.windows.tools.64.exe!insertleaf(btDbvt * pdbvt, btDbvtNode * root, btDbvtNode * leaf) Line 153	C++
 godot.windows.tools.64.exe!btDbvt::insert(const btDbvtAabbMm & volume, void * data) Line 539	C++
 godot.windows.tools.64.exe!btCompoundShape::addChildShape(const btTransform & localTransform, btCollisionShape * shape) Line 80	C++
 godot.windows.tools.64.exe!RigidCollisionObjectBullet::reload_shapes() Line 391	C++
 godot.windows.tools.64.exe!RigidBodyBullet::reload_shapes() Line 813	C++
 godot.windows.tools.64.exe!RigidCollisionObjectBullet::add_shape(ShapeBullet * p_shape, const Transform & p_transform, bool p_disabled) Line 250	C++
 godot.windows.tools.64.exe!BulletPhysicsServer::body_add_shape(RID p_body, RID p_shape, const Transform & p_transform, bool p_disabled) Line 510	C++
 godot.windows.tools.64.exe!CollisionObject::shape_owner_add_shape(unsigned int p_owner, const Ref<Shape> & p_shape) Line 336	C++

@pouleyKetchoupp pouleyKetchoupp changed the title Severe lag when adding a collider to a physics object with many colliders [Bullet] Severe lag when adding a collider to a physics object with many colliders Apr 16, 2021
@aaronfranke
Copy link
Member Author

Closing as resolved due to Godot 4.x no longer having Bullet. Also, fixing this in 3.x is not very important.

@aaronfranke aaronfranke closed this as not planned Won't fix, can't repro, duplicate, stale Dec 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants