Attaching player to vehicle

Hello all,
I don’t know if this is a dumb question, but how do I make my player object stay inside the vehicle?
The player contains lots of stuff, but important here is the node and the bettercharactercontrol.
The Vehicle is basically just a class which contains everything described in the Vehicle tutorial plus the controls and switching from player to vehicle controlling and back.
Both know if they are “riding” or “being ridden”. How would I go about having the player as long as he’s “riding” be inside the vehicle at a specific coordinate relative to the model?
And would this interfere with physics?

Robbi Blechdose

Maybe I’m thinking to easy, but you could just remove the player-model and switch the camera to the car. If you leave the car, you could do it the other way round. In other words: Only the “used” node is visible at the moment, the other is only providing your data.

Thanks for the idea,
but I think it’s not possible with my camera system,
and I really want the player to be inside the vehicle.

Robbi Blechdose

I would have the vehicleControl act on a node.
When the player enters the vehicle:

  • disable the player movement controls (listener)
  • enable the vehicle controls (listener)
  • attach the player to the node the vehicleControl acts on
  • remove the player from the physical space

That is if the player won’t be moving anymore… if you want it to be able to move inside the vehicle while the vehicle can move, then maybe better have a physics space for the vehicle interior might want to also change what group the player collides with.

I have tried using a node instead of a spatial, and then setting it to (Node) assetManager.loadModel("Car.j3o").
I also tried the attach/detach approach, but that didn’t work either…
And I don’t need the player to be moving inside the car.

EDIT: I don’t know anymore if I used those two together… but I think it’s not a good idea to cast a model to a node anyway, right?
Also, it didn’t work when I created a node inside the model and tried to attach the player there. (I got a nullpointer when I tried to find it from the rootNode by name (name was correct)).

Robbi Blechdose

Well, if your j3o already contains a top node with the geometry in it, then it’s fine casting it to a Node… otherwise, just create a node in code and use that one (and attach the stuff to it).

If I remember right, some getChild or whatever the name method from jme scene only check right beneath the parameter node… maybe try with a recursive method to try and find your player thingy.

Thank you.
I tried it out, but it only works half:
The player gets teleported to somewhere around 0, 0, 0 when I mount the vehicle, and gets teleported back when I dismount.
I do the attaching and detaching via a reference to the rootNode in the vehicle.
And I can steer it when it is mounted.

Robbi Blechdose

It seems to be the line where I attach the player to the vehicle (vehicle.attachChild(player.getNode()), where vehicle is a node which got set to assetManager.loadModel("car")).
If I comment that one out, the player just disappears like it should, but doesn’t get to some strange position.

Robbi Blechdose

To be sure… you want the player to visibly be inside the vehicle or simply disappear?

If it’s still visible, what I said should work… you just have to keep in mind that the player was in world space and you put him in a node (of the vehicle) and so need to adjust the location and rotation.

If not visible, then just enable disable the player or vehicle control listeners and attach/detach the player model from the scene.

It should be still visible,
so I removed the player controls from the physics space,
detached the player node from the root node and attached it to the vehicle.
Now the player is stuck at somewhere around 0, 0, 0 until i leave the vehicle. (I can still steer it though.)

Robbi Blechdose