Two controls combination

Good day,
I have Spatial - player and two controls attached to it.

The first is CharacterControl and the second is my custom PlayerControl.
I attach CharacterControl the first and then, when I attach my PlayerControl in it’s overridden method setSpatial I get player’s CharacterControl instance via spatial.getControl(CharacterControl.class) (spatial is default field in every control that refer to spatial to wich it attached - player in my case).

In my PlayerControl I have mappings and listeners set up. According them I do manipulations with CharacterControl, for example moving it. I mean, I manipulate one control from another through one spatial.

I think it isn’t best practice. But how can I create custom control for player if player isn’t Spatial - he is CharacterControl and in fact I control it’s CollisionShape and not common Spatial?
Maybe there is the best way of player creating?

I don’t see any problem with a custom control that manages a CharacterControl. Can you be more specific about your concern?

Perhaps you are feeling uncomfortable because you are using a control where an app state probably makes more sense?

But I want to separate context responsable for control… I think it must be Control here, isn’t that?

I am feeling uncomfortable because of I have Spatial - player but it is nowhere in fact and real player is CapsuleCollisionShape of CharacterControl

My understanding is that a control is for controlling scene objects, which is why it comes with a getSpatial() method.

As Paul said, an appstate is better suited here because you want more than that.

Now I don’t like way of creating player via CharacterControl because of I must work with it’s CollisionShape instead of usual Spatial. What if I want to add 3D model to my player? CollisionShape is only physics object, it isn’t visible in game. And I want to my player could be reflected in the mirror for example. Is there a way to add a 3D model to CharacterControl? It is additional problem to that I have Spatial - player that used only for Controls managing and I don’t use it as real Spatial object(with it’s position, translation, textures etc). I use it only for communication between controls.

Sorry, my question is quite confused. I quickly moved from working with the controls to the player visualization

When you use the physics engine you must move the physics object and not the spatial. That is why you interact with the charactercontrol.

1 Like

But can I add a 3D model to my player if he is CharacterControl?

Spatial easily can became a physics object by adding RigidBodyControl to it. This case I’m adding physical properties to visual object. But in case CharacterControl I want to understand how to add visual properties to physics object

This is a bad idea.

This is a bad idea.

Add the CharacterControl to your 3D model. Now you have your 3D model. Now your model is a Physics Object. To move and rotate it you must do it to the physics control, and not the spatial. This is because the physics engine is controlling the model now. Gravity and collision will move the spatial - it is in control of it. So you must also tell the physics engine to move it - that is why you talk to the physics object and not the spatial anymore.

To add key mappings you should use an AppState - because it’s a state of the game. If you pause the game you disable the appstate and enable another “menu” appstate.

the physics object is not “visual”. The meshes are data for the physics engine to find collisions.

I think this is where we get confused. If you want to add more things to your model, add it to the spatial. For a gun or a hat or something, just add it to the model spatial.


Or add it to the hand of the model, or the head, or wherever.

First, I suspect if you described what your “player control” is doing that it’s actually a singleton and doing the work that an app state would be doing.

Second, there seems to be some misunderstanding about how to setup a physics character?

Is it that you want a model with the physics object or not? Usually, one has a model with the physics object but if the player is ‘invisible’ to himself then you wouldn’t. But it’s not clear to me which one you are trying to achieve.

Ok, thank you all for help. I will re-read your answers once again and most likely will try to do this this via the AppState
But yesterday I have encountered a little problem with controlUpdate method of my control.
In docs I read that control’s update method executed automatically from moment I add control to spatial. But it wasn’t. Did I understand correctly, additional condition is control’s parent spatial must be added to root node?

Yes, controls are only updated when a spatial is updated, easiest way is to attach to a scene node

1 Like