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

**URL:** <https://forum.babylonjs.com/t/character-controller-that-interacts-with-physics-engine-ammo-cannon/17073>\
**Category:** Questions\
**Created:** [January 1, 2021, 7:44pm UTC](https://forum.babylonjs.com/t/character-controller-that-interacts-with-physics-engine-ammo-cannon/17073 "2021-01-01T19:44:56Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![to5ta](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/to5ta/32/50087_2.png) [@to5ta](https://forum.babylonjs.com/u/to5ta)\
**Post date:** [January 1, 2021, 7:44pm UTC](https://forum.babylonjs.com/t/character-controller-that-interacts-with-physics-engine-ammo-cannon/17073/1 "2021-01-01T19:44:56Z")

</div>

Hi there 🙂

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.

---

<div class="post-metadata">

**Author:** ![gbz](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/gbz/32/23780_2.png) [@gbz](https://forum.babylonjs.com/u/gbz)\
**Post date:** [January 1, 2021, 8:12pm UTC](https://forum.babylonjs.com/t/character-controller-that-interacts-with-physics-engine-ammo-cannon/17073/2 "2021-01-01T20:12:20Z")

</div>

Hi @to5ta, I think the Playground in [CannonJS not respecting children physicsImpostors when colliding with other physicsImpostors - Questions - Babylon.js](https://forum.babylonjs.com/t/cannonjs-not-respecting-children-physicsimpostors-when-colliding-with-other-physicsimpostors/12320/6) could help

---

<div class="post-metadata">

**Author:** ![MackeyK24](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mackeyk24/32/3741_2.png) [@MackeyK24](https://forum.babylonjs.com/u/MackeyK24)\
**Post date:** [January 1, 2021, 9:30pm UTC](https://forum.babylonjs.com/t/character-controller-that-interacts-with-physics-engine-ammo-cannon/17073/3 "2021-01-01T21:30:03Z")

</div>

Can show a demo of what you got so far ?

---

<div class="post-metadata">

**Author:** ![MackeyK24](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mackeyk24/32/3741_2.png) [@MackeyK24](https://forum.babylonjs.com/u/MackeyK24)\
**Post date:** [January 1, 2021, 9:30pm UTC](https://forum.babylonjs.com/t/character-controller-that-interacts-with-physics-engine-ammo-cannon/17073/4 "2021-01-01T21:30:32Z")

</div>

Are you using my toolkit ?

---

<div class="post-metadata">

**Author:** ![to5ta](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/to5ta/32/50087_2.png) [@to5ta](https://forum.babylonjs.com/u/to5ta)\
**Post date:** [January 2, 2021, 11:57am UTC](https://forum.babylonjs.com/t/character-controller-that-interacts-with-physics-engine-ammo-cannon/17073/5 "2021-01-02T11:57:58Z")

</div>

thanks for replying 🙂

ive got a version that somehow works on the `physics_controller` branch on my [repo](https://github.com/to5ta/js_zero)

Let me know if i should host a build somehwhere.

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

---

<div class="post-metadata">

**Author:** ![to5ta](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/to5ta/32/50087_2.png) [@to5ta](https://forum.babylonjs.com/u/to5ta)\
**Post date:** [January 2, 2021, 12:10pm UTC](https://forum.babylonjs.com/t/character-controller-that-interacts-with-physics-engine-ammo-cannon/17073/6 "2021-01-02T12:10:22Z")

</div>

I will maybe provide a sandbox if this problem persists longer for me. (currently im a bit short on time 😉 ) 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](https://jblaha.art/sketchbook) project of Jan Blaha (github-[repo](https://github.com/swift502/Sketchbook)), 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](https://github.com/swift502/Sketchbook/blob/master/src/ts/characters/Character.ts) form line 827, where he alters the resulting velocity for the character.

---

<div class="post-metadata">

**Author:** ![to5ta](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/to5ta/32/50087_2.png) [@to5ta](https://forum.babylonjs.com/u/to5ta)\
**Post date:** [January 2, 2021, 12:33pm UTC](https://forum.babylonjs.com/t/character-controller-that-interacts-with-physics-engine-ammo-cannon/17073/7 "2021-01-02T12:33:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![to5ta](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/to5ta/32/50087_2.png) [@to5ta](https://forum.babylonjs.com/u/to5ta)\
**Post date:** [January 2, 2021, 3:22pm UTC](https://forum.babylonjs.com/t/character-controller-that-interacts-with-physics-engine-ammo-cannon/17073/8 "2021-01-02T15:22:50Z")

</div>

@MackeyK24 i guess you have been talking about [this](https://docs.unity3d.com/ScriptReference/CharacterController.Move.html) 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. 🙂

---

<div class="post-metadata">

**Author:** ![gbz](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/gbz/32/23780_2.png) [@gbz](https://forum.babylonjs.com/u/gbz)\
**Post date:** [January 2, 2021, 8:01pm UTC](https://forum.babylonjs.com/t/character-controller-that-interacts-with-physics-engine-ammo-cannon/17073/9 "2021-01-02T20:01:08Z")

</div>

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()`

---

<div class="post-metadata">

**Author:** ![to5ta](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/to5ta/32/50087_2.png) [@to5ta](https://forum.babylonjs.com/u/to5ta)\
**Post date:** [January 5, 2021, 12:13pm UTC](https://forum.babylonjs.com/t/character-controller-that-interacts-with-physics-engine-ammo-cannon/17073/10 "2021-01-05T12:13:07Z")

</div>

[CharacterController Demo (Playground)](https://playground.babylonjs.com/#9VQJPV#24)

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… 😃

Any feedback / help / input appreciated 🙂

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

---

<div class="post-metadata">

**Author:** ![gbz](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/gbz/32/23780_2.png) [@gbz](https://forum.babylonjs.com/u/gbz)\
**Post date:** [January 5, 2021, 10:18pm UTC](https://forum.babylonjs.com/t/character-controller-that-interacts-with-physics-engine-ammo-cannon/17073/11 "2021-01-05T22:18:08Z")

</div>

@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](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

---

<div class="post-metadata">

**Author:** ![to5ta](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/to5ta/32/50087_2.png) [@to5ta](https://forum.babylonjs.com/u/to5ta)\
**Post date:** [January 6, 2021, 12:23am UTC](https://forum.babylonjs.com/t/character-controller-that-interacts-with-physics-engine-ammo-cannon/17073/12 "2021-01-06T00:23:15Z")

</div>

thanks for the input 🙂 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](https://playground.babylonjs.com/#9VQJPV#28)

Gonna work on that next…

---

<div class="post-metadata">

**Author:** ![gbz](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/gbz/32/23780_2.png) [@gbz](https://forum.babylonjs.com/u/gbz)\
**Post date:** [January 6, 2021, 5:43am UTC](https://forum.babylonjs.com/t/character-controller-that-interacts-with-physics-engine-ammo-cannon/17073/13 "2021-01-06T05:43:48Z")

</div>

The function `getNormal()` from `PickingInfo` come in handy when finding the surface normal: [PickingInfo | Babylon.js Documentation](https://doc.babylonjs.com/typedoc/classes/babylon.pickinginfo#getnormal)

---

<div class="post-metadata">

**Author:** ![to5ta](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/to5ta/32/50087_2.png) [@to5ta](https://forum.babylonjs.com/u/to5ta)\
**Post date:** [January 6, 2021, 6:37pm UTC](https://forum.babylonjs.com/t/character-controller-that-interacts-with-physics-engine-ammo-cannon/17073/14 "2021-01-06T18:37:45Z")

</div>

Yes, exactly 🙂 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)

> **[Babylon.js Playground](https://playground.babylonjs.com/#9VQJPV%2354)**

---

<div class="post-metadata">

**Author:** ![gbz](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/gbz/32/23780_2.png) [@gbz](https://forum.babylonjs.com/u/gbz)\
**Post date:** [January 6, 2021, 7:25pm UTC](https://forum.babylonjs.com/t/character-controller-that-interacts-with-physics-engine-ammo-cannon/17073/15 "2021-01-06T19:25:31Z")

</div>

Wow this plays very nicely 🙂

---

<div class="post-metadata">

**Author:** ![to5ta](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/to5ta/32/50087_2.png) [@to5ta](https://forum.babylonjs.com/u/to5ta)\
**Post date:** [January 6, 2021, 8:46pm UTC](https://forum.babylonjs.com/t/character-controller-that-interacts-with-physics-engine-ammo-cannon/17073/16 "2021-01-06T20:46:16Z")

</div>

[https://playground.babylonjs.com/#9VQJPV#62](https://playground.babylonjs.com/#9VQJPV#62)

Platform velocity assigment, first approach…

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

---

<div class="post-metadata">

**Author:** ![to5ta](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/to5ta/32/50087_2.png) [@to5ta](https://forum.babylonjs.com/u/to5ta)\
**Post date:** [January 18, 2021, 11:22pm UTC](https://forum.babylonjs.com/t/character-controller-that-interacts-with-physics-engine-ammo-cannon/17073/17 "2021-01-18T23:22:07Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jelster](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/jelster/32/25881_2.png) [@jelster](https://forum.babylonjs.com/u/jelster)\
**Post date:** [January 23, 2021, 8:05pm UTC](https://forum.babylonjs.com/t/character-controller-that-interacts-with-physics-engine-ammo-cannon/17073/18 "2021-01-23T20:05:35Z")

</div>

Have you looked at [Display Vertex Normals | Babylon.js Documentation](https://doc.babylonjs.com/toolsAndResources/utilities/Normals) in the docs? Might have some hints. [Display World Axes | Babylon.js Documentation](https://doc.babylonjs.com/toolsAndResources/utilities/World_Axes) might also be helpful

HTH

---

<div class="post-metadata">

**Author:** ![Pryme8](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/pryme8/32/7212_2.png) [@Pryme8](https://forum.babylonjs.com/u/Pryme8)\
**Post date:** [January 23, 2021, 8:44pm UTC](https://forum.babylonjs.com/t/character-controller-that-interacts-with-physics-engine-ammo-cannon/17073/19 "2021-01-23T20:44:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Baktrian](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/baktrian/32/29646_2.png) [@Baktrian](https://forum.babylonjs.com/u/Baktrian)\
**Post date:** [February 14, 2024, 1:52pm UTC](https://forum.babylonjs.com/t/character-controller-that-interacts-with-physics-engine-ammo-cannon/17073/20 "2024-02-14T13:52:34Z")

</div>

Were you able to find it?

[Next page](https://forum.babylonjs.com/t/character-controller-that-interacts-with-physics-engine-ammo-cannon/17073.md?page=2)
