Page 1 of 1

Character standing on a moving vehicle

Posted: Tue Apr 27, 2021 2:44 am
by kander
Hey all,

I have a player controlled character (rigid body) which is moved by setting its velocity before each physics update. I would like the player to be able to stand and walk around on top of a moving vehicle.

I have tried increasing the friction between the vehicle and the player, and also setting the player's velocity to match that of the vehicle, but when the vehicle is under a lot of acceleration (start, stop, turning) the player slides around too much.

I have another idea, and that is to constrain a dummy object to the top of the vehicle at the players last position, and then for each impulse that is applied to this dummy object during simulation step, I apply the same to the player object. However, I haven't figured out how to retrieve this information yet.

Please let me know if you have any other ideas.

Re: Character standing on a moving vehicle

Posted: Tue Apr 27, 2021 11:10 am
by kander
I have found the results are really good if I turn up friction to the max. Howver, this means my character is stuck to to anything it touches and cannot move. It looks like I can use the contactAddedCallback to customize the friction between the player and the vehicle. I will try and see If I can apply the friction selectively.

Re: Character standing on a moving vehicle

Posted: Tue Apr 27, 2021 2:09 pm
by drleviathan
This is really hard to do. By tweaking the friction and damping and adding special "am I standing on a vehicle?" logic to your character you might get satisfactory results with only one "world" if your vehicle moves slowly, however such a system will eventually fail as vehicle velocities increase. BTW, you probably want to try setting character damping to zero when standing on the vehicle if you haven't already. Damping is a non-physical feature of Bullet but is useful for introducing global energy loss to help things settle down.

I've never actually tried it but: I believe better results can be obtained by using a second "world" simulation. When the character walks onto the vehicle you would spin up a second simulation, remove the character from first and add it to the second. In the second (vehicle-local) simulation the vehicle's collision shape is a static "ground". The character can walk around and you use the acceleration of the vehicle in the first simulation to dynamically set the local gravity of the second simulation. In physics terms: the character is standing in an non-inertia reference frame... when the vehicle accelerates. Of course, if you need the character to interact with any other first-world objects besides the vehicle then you'd have to add collision proxies in the second simulation and things start to get really complicated if you need those objects to be dynamically moved by the character.

Re: Character standing on a moving vehicle

Posted: Tue Apr 27, 2021 10:16 pm
by kander
drleviathan wrote: Tue Apr 27, 2021 2:09 pm I've never actually tried it but: I believe better results can be obtained by using a second "world" simulation. When the character walks onto the vehicle you would spin up a second simulation, remove the character from first and add it to the second. In the second (vehicle-local) simulation the vehicle's collision shape is a static "ground". The character can walk around and you use the acceleration of the vehicle in the first simulation to dynamically set the local gravity of the second simulation. In physics terms: the character is standing in an non-inertia reference frame... when the vehicle accelerates. Of course, if you need the character to interact with any other first-world objects besides the vehicle then you'd have to add collision proxies in the second simulation and things start to get really complicated if you need those objects to be dynamically moved by the character.
Thanks drleviathan. I have considered the two world approach, but have not tried it yet. You highlight the difficulty with this approach; objects interacting between worlds. One idea I had was to revert back to world 1 player rigid body when a large enough collision occurs, but keeping them in sync looks tricky. Maybe if I always update world 2 first, I can then pass on the total impulse to the world 1 bodies each time step.

I'm also wondering now if a weak velocity constraint (one iteration of solving) would work. Unfortunately I don't think bullet provides a velocity constraint (constrain relative velocity of 2 bodies). I wonder how difficult it would be to implement 🤔

Re: Character standing on a moving vehicle

Posted: Wed Apr 28, 2021 12:52 am
by kander
The slider constraint looks like it can be used as a velocity constraint. I will play around with it first.

Re: Character standing on a moving vehicle

Posted: Thu Apr 29, 2021 5:00 pm
by kander
Another update.

It looks like setting velocity of player to vehicle + player movement acctually works waaay better than I first thought. My current solution involves setting player position and adding a positional constraint only when the relative movement between the vehicle and the player is very small.

Re: Character standing on a moving vehicle

Posted: Tue May 04, 2021 12:38 pm
by chineseoldghost
maybe try first,calc the correct force to rigidbody,then calc vehicle local space move force to rigidbody ,