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 physics engine implementation #10013

Closed
wants to merge 39 commits into from

Conversation

AndreaCatania
Copy link
Contributor

It's long time that we talk about this. and finally today I'm submitting the PR.

Note

Before present my job I want highlight some things.
This implementation is not intended to completely overwrite the current godot implementation, infact by going to the the project settings is possible choose which Physics engine to use so the developer has 100% freedom about which engine to use (the default is the current Godot physics engine).
Godot - physics engine select

The other thing is that by using bullet physics engine we are not changing nothing about the old usage of Godot. All the old known ways used to handle the physics in the games remains untouched at all.
I've made some videos where I compare the two physics engine and the only thing I did to swap the engines was select GodotDefault or Bullet in the settings.

There are two things to implement vehicle and kinematic character, I'll implement these if this PR will be accepted. The SCsub file must be improved to allow more customization of Bullet.

Presentation

Thanks to the good concept of Godot implementation I was able to implement Bullet in a natural way.

In this video https://www.youtube.com/watch?v=NYLQSaMXHmg I show the areas and the trimeshes that works perfectly, I want to point out that the areas can be used as trigger or space modificator, like is done by default Godot. In this video you can see all the balls that falls down under a limit enter inside a trigger area that destroy it.
Another thing is that as you can see Bullet manage better the trimesh collisions and the balls movement.

In this video https://www.youtube.com/watch?v=4X4GRbmfSnc I want to show you the improvements that Bullet port and for this reason I show three tests.
The first test show the joints implementation, So I've made a chain with dynamics number of elements. Using godot physics engine we are limited to 88 elements, but with Bullet we can go further to 300 with 30FPS.
The other test I test a slider, hinge, 6DOF + motor joints and with Godot physics it doesn't work at all.
The last test I made is usefull to demostrate that Bullet support perfectly the method of godot to create multi shaped bodies

Conclusion

As you can see by implementing Bullet physics engine we have the access to a very stable physics engine, that is a lot know and well documented, without losing any performance.
We also have the possibility to focus more on the implementation of new featurs like Cloth / soft bodies, or improve performance of trimesh by using vertex reduction utility. And improve the vehicle stability. I've also implemented the heightmap shape so when we'll implement the possibility to create the height field inside the engine the collision shape is ready to work.

I think this is a good feature and improvement that speed up the implementation of the engine and port even more interest about the third version of this engine, that with the new rendering engine and withy a stable physics engine make this game engine even more competitive.
I don't want provide it as a GDNative because I want implement the things that I've pointed above (clothing) that are impossible to support using GDNative since I need to make some changes to engine code.

@reduz
Copy link
Member

reduz commented Aug 1, 2017 via email

@AndreaCatania
Copy link
Contributor Author

AndreaCatania commented Aug 1, 2017

We just need to implement vehicle and kinematic character. but all other stuff are working. I'm very happy to hear this.

@kubecz3k
Copy link
Contributor

kubecz3k commented Aug 1, 2017

Is character mode supported at current time in this pr?

@AndreaCatania
Copy link
Contributor Author

@kubecz3k As I've written before, the character is not yet supported, but It'll be supported soon together with vehicle

@kubecz3k
Copy link
Contributor

kubecz3k commented Aug 1, 2017

Well you was writing about 'kinematic character', thought that meant kinematic mode, not kinematic and character :)

@karroffel
Copy link
Contributor

A few non-problems I noticed:

  • module and thirdparty directory name are capitalized, everything else is lowercase
  • many many methods that are not exposed to ClassDB use camelCase, the rest of the engine uses snake_case. It's not a big deal but that makes all Bullet related stuff look alien in the engine.
  • you're using #pragma once in the headers. This is quasi-standard for compilers, but again, the rest of the engine uses #ifndef, #define, #endif
  • filenames are PascalCase, again, not a huge deal but everything else is snake_case or at least lowercase.

I don't know much about physics so I can't really make a proper code review, this is just surface level stuff I noticed. But really impressive, nice work!

@AndreaCatania
Copy link
Contributor Author

@karroffel Nice hint, the capitalization could be a problem and I can update it. This is not a problem.
However which methods do you mean that are not exposed? The BulletPhysicsServer implement the physics_server class that is used as a bridge between Godot and physics engine, so the godot don't need to know about other things

@reduz
Copy link
Member

reduz commented Aug 1, 2017

My main issues with implementing Bullet as physics engine, despite all the advantages, so (if you have any fedback on this it's welcome). Discussing how they can be solved will be interesting:

  • Bullet uses GJKEPA for narrow phase. This is fantastic because of simplicity, but it requires shapes to have a collision margin (where, basically, it switches from GJK to EPA). I can imagiene this needs to be exposed in Shape for it to be tweakeable and not sure how user friendly it is.
  • Bullet uses SAP (sweep and prune) as broadphase. This is super fast for pairing collision objects, but slow for raycasts and shape testing. I know Bullet has alternative broadphases, such as tree, but not sure how well it adapts to Godot. Maybe it should be made configurable somewhere?
  • Godot uses type masking for objects in a way it's quite complex (layer mask and collision mask) is implementing this in bullet even possible?
  • test_motion needs to be implemented somehow, I think this can be done wit Bullet API (you pretty much only need GJK for this), but did not look for it enough.

@reduz
Copy link
Member

reduz commented Aug 1, 2017

Some more cocerns:

-Godot has a RayCast shape, which basically just pushes objects apart. This very useful for dynamic character controllers, not sure how it's done in Bullet
-Simulating RigidBody Character mode in bullet requires infinite MOI mass. I'm sure it can be done, but it needs to be tested..
-KinematicBody detects when it was moved and computes a fake linear/angular velocity so it can drag stuff over it or push it properly when interacting with physics engine. This needs to be implemented too..

@reduz
Copy link
Member

reduz commented Aug 1, 2017

And even more concerns:

  • Area uses only axis testing in Godot, not full collision testing (it does not need to retrieve contact points). In Bullet this would mean using only GJK but not EPA. For this, margins are kind of useless too. Is Area behing handled in a similar way here?

@HummusSamurai
Copy link
Contributor

HummusSamurai commented Aug 1, 2017

Oh wow this is huge, and I hope it works out.

I adore godot but if bullet can help stabilise the physics then I'm all for it.

@LeonardMeagher2
Copy link
Contributor

I would use this pr because joints actually work, where I've never had luck with joints in godot's current physics engine.

@reduz
Copy link
Member

reduz commented Aug 3, 2017

@LeonardMeagher2 So far @AndreaCatania is doing a great work, we are still ironing out all the inconsistencies between the APIs to make adapting it as smooth as possible.

@Zylann
Copy link
Contributor

Zylann commented Aug 3, 2017

HeightMap shape implemented? Ooooh that means the heightmap module I'm making in 3.0 can get proper physics :D I wonder if Bullet can handle huge sizes / dynamic stitching with these

@suptoasty
Copy link
Contributor

suptoasty commented Aug 3, 2017

Bullet 2.85 added htc vive and occulus rift support, so the guys that worked/are working on Godot ARVR might be able to make use of bullet to further that aspect of the engine. Bullet could also allow for things that need a lot of speed like destructible and deformable environments. If this wrapper can be added and allow for interchangeability between Godot's physics engine and Bullet without the need to rewrite existing scripts, this would be a huge milestone and would be worth throwing time towards in the future in a newer version like 3.1 or what ever version comes after that. For now the concerns Reduz brought up should be addressed.

@eon-s
Copy link
Contributor

eon-s commented Aug 3, 2017

This is advancing faster than I was expecting :D

Once this is done, a post about the experience of implementing an existing physics engine on Godot may be interesting for game developers.

@HummusSamurai
Copy link
Contributor

HummusSamurai commented Aug 4, 2017 via email

@groud
Copy link
Member

groud commented Aug 5, 2017

Will this also work in 2D ? Is it even possible to use Bullet in 2D ?

@Zylann
Copy link
Contributor

Zylann commented Aug 5, 2017

Personally I wouldn't expect Bullet to be used for the 2D engine, it would sound like the same mistake Unity did a few years ago^^" But it could be usable if the user really wants to, not forged into the design of the engine.
Alternatively, it would also be interesting to be able to use the 2D physics engine in 3D if the game is top-down / side with 3D being used only for visuals, but it's probably not the right place to discuss this.

@reduz
Copy link
Member

reduz commented Aug 5, 2017 via email

@eon-s
Copy link
Contributor

eon-s commented Aug 5, 2017

And what about Nape?
Forget it, for a moment I have thought it had a c++ implementation.

@AndreaCatania
Copy link
Contributor Author

Thanks to the help of Juan I have implemented the kinematic character. Soon I'll reply to all doubt that Juan raised up, and I'll commit all changes. I let you know!

@alugarius
Copy link

Good luck @AndreaCatania

@Zireael07
Copy link
Contributor

Are Bullet vehicle physics done? When working on the roll influence patch, I referred to the Bullet documentation since the vehicle physics in Godot are very very close... And hopefully having the Bullet option would let me work around the issues I'm having in 3.0...

@AndreaCatania
Copy link
Contributor Author

I've do some changes to fix some inconsistencies with current, pointed by @karroffel and @reduz.

I've updated the code to agree to Godot writing policy.

Reduz concerns

  • Bullet uses GJKEPA for narrow phase. This is fantastic because of simplicity, but it requires shapes to have a collision margin (where, basically, it switches from GJK to EPA). I can imagiene this needs to be exposed in Shape for it to be tweakeable and not sure how user friendly it is.

Yes this can be exposed in the shape module, it's the best place since this is something related to shape


  • Bullet uses SAP (sweep and prune) as broadphase. This is super fast for pairing collision objects, but slow for raycasts and shape testing. I know Bullet has alternative broadphases, such as tree, but not sure how well it adapts to Godot. Maybe it should be made configurable somewhere?

Yes we can make it configurable, it's not a problem. To do it I need to insert in the function that start the engine some lines of codes that read the engine settings


  • Godot uses type masking for objects in a way it's quite complex (layer mask and collision mask) is implementing this in bullet even possible?

Like Godot, The Bullet engine use Layer and Mask to manage the collisions, the only difference is that in order to generate a collision both sides must collide.

For example a ball that collides with a wall and not with other balls

BULLET:

Ball layer = 1000 mask 0100
Wall layer = 0100 mask 1000

IN GODOT:

We can just have collision in one side

Ball layer = 1000 mask 0100
Wall layer = 0100 mask 0000

Another thing is that Godot has collision exceptions, These are managed, nothing in particular to say about.


  • test_motion needs to be implemented somehow, I think this can be done wit Bullet API (you pretty much only need GJK for this), but did not look for it enough.

Thanks to the help of reduz I've implemented these function, so the kinematic character is now working

  • Godot has a RayCast shape, which basically just pushes objects apart. This very useful for dynamic character controllers, not sure how it's done in Bullet

Implemented

  • Simulating RigidBody Character mode in bullet requires infinite MOI mass. I'm sure it can be done, but it needs to be tested..

I've managed this using the function setAngularFactor


  • KinematicBody detects when it was moved and computes a fake linear/angular velocity so it can drag stuff over it or push it properly when interacting with physics engine. This needs to be implemented too..

This is not supported by current godot 3.0, so I've skipped it for now. However I've checked how to accomplish it and I done a test. It can be done by generating and managing a Manifold when an hit occurs.


  • Area uses only axis testing in Godot, not full collision testing (it does not need to retrieve contact points). In Bullet this would mean using only GJK but not EPA. For this, margins are kind of useless too. Is Area behing handled in a similar way here?

I've checked Bullet's code, and as done by Godot, it uses only Broad Phase to handle that.

@AndreaCatania
Copy link
Contributor Author

@Zireael07 Yes I'll implement the vehicle as next step

@SirPigeonz
Copy link
Contributor

SirPigeonz commented Aug 14, 2017

@AndreaCatania

Bullet uses SAP (sweep and prune) as broadphase. This is super fast for pairing collision objects, but slow for raycasts and shape testing. I know Bullet has alternative broadphases, such as tree, but not sure how well it adapts to Godot. Maybe it should be made configurable somewhere?

Yes we can make it configurable, it's not a problem. To do it I need to insert in the function that start the engine some lines of codes that read the engine settings

This will have to be well documented and defaults should be thought out. Physics in games is used much differently than in VFX where Bullet shines, we do hell a lot raycasts and not much heavy physics simulations. ;)

Of course it varies from game to game, but looking at the market. I don't see games that use complicated simulations but many that need to use a lot of raycasting).

Bullet uses GJKEPA for narrow phase. This is fantastic because of simplicity, but it requires shapes to have a collision margin (where, basically, it switches from GJK to EPA). I can imagiene this needs to be exposed in Shape for it to be tweakeable and not sure how user friendly it is.

Yes this can be exposed in the shape module, it's the best place since this is something related to shape

I think main concern here was not, where place it, but "how user friendly it is". Careful design and good documentation would be appreciated a lot. :)

@akien-mga
Copy link
Member

Closing, we don't want this feature in Godot.

...

Ok just kidding, this PR is awesome :)

But @AndreaCatania is going to do a new PR with the same code rebased to something with a cleaner git history - we close this PR to keep its work-in-progress commit history for future reference if need be (especially the lengthy discussions).

@akien-mga akien-mga closed this Nov 4, 2017
@Zireael07
Copy link
Contributor

@akien-mga you nearly gave me a heart attack with the closing thing :P

@neikeq
Copy link
Contributor

neikeq commented Nov 4, 2017

@akien-mga You...

@slapin

This comment was marked as off-topic.

@AndreaCatania
Copy link
Contributor Author

AndreaCatania commented Nov 5, 2017

This is the best awakening ever

untitled

NOTE:

Now Bullet is merged and it is the default physics engine, you don't need to change the settings in order to use it.

@SirPigeonz
Copy link
Contributor

Congratz @AndreaCatania !

Good job :3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.