Physics character controller

Yo @Cedric I think we might me missing maxStepHeight to traversing slightly larger steps

I’ll take a look asap

Yo @Cedric … After some playing around, it seems keep is helping in getting that much close ground contact:

characterController.keepContactTolerance = 0.001;
characterController.keepDistance = 0.01;

Yields much better ground contact and more reliable feet position on the ground surface.

What do you think, or should i not tweak those params for better ground contact ?

On another note, how would you support SLIDING state in getNextState:

        public getNextState(supportInfo: BABYLON.CharacterSurfaceInfo): TOOLKIT.CharacterState {
            if (this.state == TOOLKIT.CharacterState.IN_AIR) {
                if (supportInfo.supportedState == BABYLON.CharacterSupportedState.SUPPORTED) {
                    return TOOLKIT.CharacterState.ON_GROUND
                }
                return TOOLKIT.CharacterState.IN_AIR
            } else if (this.state == TOOLKIT.CharacterState.ON_GROUND) {
                if (supportInfo.supportedState != BABYLON.CharacterSupportedState.SUPPORTED) {
                    return TOOLKIT.CharacterState.IN_AIR
                }
                if (this.wantJump === true) {
                    this.wantJump = false; // Note: Reset jump flag
                    return TOOLKIT.CharacterState.START_JUMP
                }
                return TOOLKIT.CharacterState.ON_GROUND
            } else if (this.state == TOOLKIT.CharacterState.START_JUMP) {
                return TOOLKIT.CharacterState.IN_AIR
            }
            return TOOLKIT.CharacterState.ON_GROUND
        }

1 Like

keepContactTolerance and keepDistance are public. you are free (and encouraged to tweak them)

maxStepHeight you can tweak maxSlopeCosine to set the max incline.

I guess Sliding a different ON_GROUND. Do you have criteria to make the difference? Maybe if expected velocity (from user inputs) is different from computed velocity by Character Controller, while being on_ground?

Maybe if i am grounded and current vertical velocity.y < aslidingthreashold… I must be sliding down… What do you think ?

In Ammo.js and Unity the character controller has a separate property you use to set the max step height for climbing steps… all the step from the playground are small enough, but these steps are slightly bigger.

If I set max slope cosign to a value for 90 degrees it will climb these larger steps… but is too much for a good player slope angle limit… which I might want to limit to something like 45 degrees

Using my Unity Degrees to Slope Angle Function

public static UnitySlopeAngleToCosine(unitySlopeAngleDegrees:number):number {
    return Math.cos(unitySlopeAngleDegrees * Math.PI / 180);
}

I was just wondering if we could control those two things separately ?

BTW… Is the BABYLON.PlayerCharacterController a class from havok physics engine or is it entirely made from babylon.js code using the standard havok physics shapes and rigidbodies ?

Feel free to do a PR to add a conversion function to the character controller, I’ll be glad to approve it :slight_smile:

1 Like

Hey @Cedric … I am having an issue that is hard to tell when using just a capsule. But when actually using a mesh with animations for JUMP and FALLING. It seems when traversing uneven ground that make. you go up and down or running down stairs… The character controller looses contact with the ground and therefore the state != ON_GROUND for a frame or two. So that lose of ground contact may cause my falling animation to trigger and i am really still walking on an un even ground or running down the stairs.

How would you handle something like, is there a way to extend. the shapecast length so it maintain ground contact thru small uneven surfaces or running down stairs ?

Did you try to increase character gravity?

Yeah, Im using like 3G (-29.43)

I’d try to lower Babylon.js docs so character doesn’t ‘fly’.

I just a much lower Jump Force value of 9.81 for the start jump so he doesnt fly… But I like that 3G drop gravity force if i walk off a ledge or the downside of a jump

            } else if (this.state == TOOLKIT.CharacterState.START_JUMP) {
                this.jumpDirection.copyFrom(this.inputDirection)
                // DEPRECATED: let u = Math.sqrt(2 * this.characterGravity.length() * this.jumpHeight)
                let u = Math.sqrt(this.jumpForce * this.jumpHeight)
                let curRelVel = currentVelocity.dot(upWorld)
                return currentVelocity.add(upWorld.scale(u - curRelVel))
            }

But if you think LOWER characterGravity vector … I will try that as well, What do you suggest for characterGravity for my Unity Like Player Character ?

What about applying a bit of gravity when GROUNDED, I dont think we have ANY gravity appliated if the state is grounded, is that correct ?

Gravity is applied, independently of user facing state machine.
Looking at the code, maybe maxCastIterations can help here to get the last bit of distance.
Also, increasing keepContactTolerance should help character keep being ground.