[SOLVED] BulletAppState concept

Good day,
Now, I am trying to understand how to use BulletAppState physics in my game.
I have a SimpleApplication instance and some AppStates inside, that helps me switch between scenes. When I say scene I mean new class extends BaseAppState I created. I want to add physics to some of this scenes. For example, I want to have physics space in game scenes and don’t want to have it in menu scenes. I am using BulletAppState for it.

The first I don’t understand, is why do they use AppState for physics adding. As I understand, AppStates using for work with scenes.

The main question is how I can add physics to some of my scenes(for every scene I create a new class extends BaseAppState) and work with them. I don’t understand how I must work with BulletAppState. I must create one BulletAppState instance in main class and add objects of all scenes to it’s physics space or I must create a new field(instance of BulletAppState) inside each of my classes extends of BaseAppState?

The first way I think won’t work because all objects from all scenes will be in one physics space. As I understand, there will be collisions between objects that not really in one scene.

The second way seems more good, but I don’t understand how to work with different BulletAppState's instances attached to the one stateManager. How I can get BulletAppState of concrete scene I need, if I will have many BulletAppStates attached to the one stateManager?

For example, now I want to write method in main class that will enable physics debug mode on all bullet app states I have. I want go through all bullet app states and call setDebugEnabled method. But I can’t get exactly BulletAppStates attached to stateManager because of it’s method getStates returns all AppStates and some of them is BulletAppStates describing physics but some of them is BaseAppStates describing game scenes . If I calls getState method it returns the first state that is instance of the specified class but how about others and how do I know bulletAppState of what scene will be the first?

Please, advice me the best way working with BulletAppState

1 Like

Appstates have many valid uses. Instantiating an AppState to manage each scene is a reasonable strategy, but it’s not the only way to go. For instance, I typically create an AppState to manage each input mode, so activating a pulldown menu activates the AppState in charge of pulldown menu input.

You’re not required obtain your PhysicsSpace from BulletAppState. You can instantiate PhysicsSpace directly, if you wish.

Each BulletAppState manages a single PhysicsSpace. If you can get by with re-using a single PhysicsSpace for all scenes—and I suspect you can—then you can get by with a single BulletAppState.

You can get around that by adding objects to the physics space in the scene’s initialize() method and removing them in its cleanup() method.

Nope. AppStates are for extending the functionality of Application (SimpleApplication in this case) in the same way that Controls are for extending the functionality of Spatial.

Composition over inheritance. Better, makes things more reusable, etc…

The fact that you can use this feature for scenes is just a nice bonus.

1 Like

Thanks @pspeed and @sgold users for AppState usage explanation.
Thanks @sgold for advice about direct PhysicsSpace usage.
Thanks @sgold for explanation about reusing single BulletAppState

How can I mark @sgold answer(as most full) as solution?)

We don’t rate solutions. Once your issue is solved, it’s customary to edit the title of the topic and add “[SOLVED]”.