Babylon.PhysicsCharacterController Usage

Hey @Cedric I am still having issues with getting my chracter controller working like you have the BABYLON.PhysicsCharaterController.

I cant use (and frankly dont fully understand) your implmentation for movement, which is beautifully smooth, by the way, especalliy the stairs and slopes… Just beautiful :slight_smile:

From this snippet here:

const support = characterController.checkSupport(dt, down);
const desiredLinearVelocity = getDesiredVelocity(dt, support, characterCurrentOrientation, characterCurrentVelocity);
characterController.setVelocity(desiredLinearVelocity);
characterController.integrate(dt, support, characterGravity);
const newPosition = characterController.getPosition();

and what you have at https://playground.babylonjs.com/?inspectorv2=true#WO0H1U#13

I cant really tell what is the full rhyme or reason for movement code.

What is ACTUAL required code to move (with collisions) and what is just the sample code ?

Especalliy what in getDesiredVelocity

Is all that the Required way of doing movement or just the way you are handling things for this demo, which brings me back to what is required for movement.

I am so used to Unity and Ammo for character based movement both of which seem to move with collision by its move function with a moveVelocity. (Kinda how the basic Babylon mesh.moveWithCollisions works).

I raycast download to detect IsGrounded … etc…

I would luv to switch my BACKEND RIGIDBODY base character controller I created with just Pure Ridigbody physics with your offical BABYLON.PhysicsCharacterController

Again… I am trying to fully understand what makes it tick so I can refactor my Unity Exported Character Controllers

Any detail information on how this really works:

const support = characterController.checkSupport(dt, down);
const desiredLinearVelocity = getDesiredVelocity(dt, support, characterCurrentOrientation, characterCurrentVelocity);
characterController.setVelocity(desiredLinearVelocity);
characterController.integrate(dt, support, characterGravity);
const newPosition = characterController.getPosition();

What is actually required to get that moveWithCollisions type movement we had with built-in babylon movement or btKinematicCharacterController from Ammo.js

Please feel free to DM me if needed.

checkSupportand getDesiredVelocity are both on user side. A speed is computed depending on game/experience and states. If character is walking in mud, speed will be reduced. getDesiredVelocity needs to know the support (and inputs) to compute a velocity.

Next, with that desired speed, integratewill compute the new position using the physics.

moveWithCollision computes a new position based on a desired position delta.

integrateis almost the same except the position delta is decomposed as velocity ( desiredLinearVelocity ) and dt.

deltaPosition = desiredLinearVelocity * dt;

That decomposition of time+speed instead of delta distance allows more possibilities.

So, to replace moveWithCollision by character controller integrate, you need to compute a velocity. As you have the delta position (current mesh position + displacement), you can compute the speed as displacement / deltaTime.

To replace do the opposite, displacement is speed * deltaTime.

I think the checkSupport method should now have been on the characterController side and put in a different conceptual piece of code. It’s misleading to check support on character controller, then tweak speed on user side and then integrate it. Separation could be better.

EDIT: thinking more about it. If you provide default CharacterSurfaceInfo parameter for integratecall, with supportedState:CharacterSupportedState.UNSUPPORTED then you should have complete control on desiredVelocity and you are free to add or not gravity for example.

I guess it’s possible to add a new moveWithCollision method that would do the conversion. It would take a same parameters as that method and would use a ‘dummy’ CharacterSupportedState. Would it help?

OMG… that would be a great help.

I have all my code already calculated to FIXED FRAMERATE COMPENSATED movement velocity. I was using this value to move my rigidbody character controller with setLinearVelocity.

If you made a moveWithCollision function, i should be able to feed that same exact movement velocity to your new moveWithCollision function

Cool. I’ll give it a try beginning of next week. I’ll ping you for feedback when I have a draft PR opened.

1 Like

Thank you so much :slight_smile:

Sorry for the delay! No progress but it’s still on my radar, I do not forget you :slight_smile:

1 Like