Ammo.js with Three.js

Ok, I see. But at least you can read the API.

Please, it is better if you made a complete live example. It is very hard to tell what is wrong without it.

I found this on github. Maybe it helps.

You missed the call to physicsWorld.addCollisionObject( ghostObject, 32, -1 ); ?

Yes, lo.th has made a lot of libraries for characters, physics and other stuff with Ammo.js. It is worth a look.

just added, not working

I get “TypeError: scope.domElement is undefined” at OrbitControls.js

(Sorry I have to go out for half an hour)

it is okey, thank you for your help ! i will check the example from that guy,maybe will work

I will try to implement the character controller into my framework as well, will share it once I got it to work.

In the meanwhile here is all the ammo related code I have implemented so far.

And here some examples I made so far.

Hope it helps :slight_smile:

would be awesome :smile:

I quickly tried to implement it. It works, but the character only collides with dynamic objects.

In the video, the first (from the left) cube is kinematic, the second static and the third dynamic.
If I add gravity to the character, he falls through the ground, since it is static.

@extremety1989 & @yombo some ideas?

This is what the codes looks like.

public addCharacter() {
  const shape = new Ammo.btCapsuleShape(0.5, 0.5)

  const ghostObject = new Ammo.btPairCachingGhostObject()
  const transform = new Ammo.btTransform()
  transform.setIdentity()
  transform.setOrigin(new Ammo.btVector3(-5, 1, 0))
  transform.setRotation(new Ammo.btQuaternion(0, 0, 0, 1))
  ghostObject.setWorldTransform(transform)
  ghostObject.setCollisionShape(shape)
  ghostObject.setCollisionFlags(ghostObject.getCollisionFlags() | 16) //CHARACTER_OBJECT

  const controller = new Ammo.btKinematicCharacterController(ghostObject, shape, 0.35, 1)
  controller.setUseGhostSweepTest(true)

  controller.setGravity(0)
  // it falls through the ground if I apply gravity
  // controller.setGravity(-this.physicsWorld.getGravity().y())

  // move slowly to the right
  controller.setWalkDirection(new Ammo.btVector3(0.05, 0, 0))

  // addCollisionObject(collisionObject: Ammo.btCollisionObject, collisionFilterGroup?: number | undefined, collisionFilterMask?: number | undefined): void
  this.physicsWorld.addCollisionObject(ghostObject)
  this.physicsWorld.addAction(controller)
}

I solved it. I just added

this.physicsWorld
      .getBroadphase()
      .getOverlappingPairCache()
      .setInternalGhostPairCallback(new Ammo.btGhostPairCallback())

and changed the ground to be kinematic instead of static with a mass of zero.

nice work, i also made it

I did not notice any issues.

But I noticed that the player slides down the slopes when it should stand still. Do you know how to prevent this?

no, i am working first time with physics etc, i am web developer :smiley:
i am searching for that problem

here we go
but here is the problem with reseting gravity back, it does not apply, i am looking forward to fix this

if ( ghostControls.onGround() ) {

ghostControls.setGravity(0)

}else{

ghostControls.setGravity(-physicsWorld.getGravity().y())

}

just to need to find out how to reset gravity, when character is on ground set gravity to 0 if on air add gravity

this code works perfectly
if ( ghostControls.onGround() ) {

console.log("on ground");

}else{

console.log("on air");

}

This should be solved by using these flags (the last two parameters) when adding objects to the world:
physicsWorld.addRigidBody( rb, 1, -1 );

And also by setting these flags in the ghost object:
ghostObject.setCollisionFlags( ghostObject.getCollisionFlags() | 16 );

one day we will achieve this Kinematic Character Controller by Phil_SA :wink:

1 Like

i just replaced
// const shape = new Ammo.btCapsuleShape(0.5, 0.05);

//with
const shape = new Ammo.btSphereShape( 0.5 );

works smooth but i think we need to combine character capsule (to make collision characters height and width) + Sphere for floor collision

This did not work until I added the snipped below: