Character Controller that interacts with physics engine (ammo/cannon)?

Hi there :slight_smile:

currently im trying to build up a character controller that interacts with an physics engine in a way that the character both can be controlled by player input as well as can be hit by flying imposters / collide with dynamic boxes.

As far as i know the basic idea is to get the current velocity each frame, manipulate it and set it again (tweaking the pyhsics of the character a little).

Did anyone tried this before or found an example? I could only find examples of controllers that did not interact with an pyhsics engine.

Hi @to5ta, I think the Playground in CannonJS not respecting children physicsImpostors when colliding with other physicsImpostors - Questions - Babylon.js could help

1 Like

Can show a demo of what you got so far ?

Are you using my toolkit ?

thanks for replying :slight_smile:

ive got a version that somehow works on the physics_controller branch on my repo

Let me know if i should host a build somehwhere.

Currently not, do you have a reference to your toolkit? @MackeyK24

I will maybe provide a sandbox if this problem persists longer for me. (currently im a bit short on time :wink: ) Also i could not find much information at all on how to combine a controller with a physics engine.

Best reference i have so far is the sketchbook project of Jan Blaha (github-repo), whose character controller works a lot like i would expect things to work. Hes uses cannon btw.

I was wondering if i could reproduce a similar controller only using the babylonjs wrappers.

The interesting part is in the Character class form line 827, where he alters the resulting velocity for the character.

@gbz your demo is also pretty cool, but im wondering if the character could be pushed by moving imposters?

@MackeyK24 i guess you have been talking about this unity function in the other thread?

But isnt this anything more than a “moveWithCollisions” as in babylonjs? Or is the simulated velocity (coming from the physics engine) injected behind the scenes?

I will have a closer look into the CharacterController of sketchbook, and report when i manage to build a similar controller. :slight_smile:

It should be possible, though you’d have to modify the code by removing the sleep() calls when there are no key presses and somehow not erasing the velocity other objects cause to your character in setLinearVelocity()

CharacterController Demo (Playground)

Here is what ive got so far. Currently im using only raycasting and moveWithCollsion, no engine involved.

Next step is to get the normal of the ground on collsion to change to input velocity to walk slopes properly.

When im done, i will try to transfer the behaviour to an approach ammo / cannon. If i finish at all… :smiley:

Any feedback / help / input appreciated :slight_smile:

EDIT: I will update the version of that playground regularly, if anyone is interested in the progress.

1 Like

@to5ta, this is awesome, and thank you for sharing!

I think the benefit of moveWithCollisions is very smooth movement, even up and down slopes. It would be awesome if you figured out how to calculate the normals to change velocity on slopes

Recreating this smooth movement with a physics engine is challenging. In the Playground (https://www.babylonjs-playground.com/#3EDS3A#96) within the Question post linked above, there is a small issue I haven’t solved: There is a small initial “bump” in movement when going down a slope

If you do decide to go with a physics engine, you could leverage Friction, which should give you slope angle-dependent velocity

1 Like

thanks for the input :slight_smile: I will have a closer look at this playground later on again.

With the current version the slope walking problem persists (walking it down causes falling triggers).
https://playground.babylonjs.com/#9VQJPV#28

Gonna work on that next…

1 Like

The function getNormal() from PickingInfo come in handy when finding the surface normal: PickingInfo | Babylon.js Documentation

Yes, exactly :slight_smile: Global surface slopes are calculated now, moving vector transformation still tbd.

This is what i have so far…

At least the platform interaction problem can be observed already… guess collision objects’ classes have to be detected and movement has to be treated accordingly (aka hit platform? move with platform)

2 Likes

Wow this plays very nicely :slight_smile:

1 Like

https://playground.babylonjs.com/#9VQJPV#62

Platform velocity assigment, first approach…

EDIT: Assigning Platform Velocity works now.
EDIT2: fixed broken input handling

2 Likes

Whats the best way to visualize a Vector3? Best i did before is

function InjectVec3toLine(vec3, line) {
    line.updateVerticesData(BABYLON.VertexBuffer.PositionKind, [0,0,0,vec3.x, vec3.z, vec3.z]);

}

but somehow it seems not to work? Are there better approaches?

Have you looked at Display Vertex Normals | Babylon.js Documentation in the docs? Might have some hints. Display World Axes | Babylon.js Documentation might also be helpful

HTH

1 Like

I’ve got a PG of this floating around with this all done. Let me see if I can find it.

Has a fps or third person controller and animations linked to ammo.

1 Like