Skip to content

eldinor/yuka-babylonjs-examples

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

yuka-babylonjs-examples

Yuka Game AI + 3D rendering with Babylon.js.

Yuka library source: https://github.com/Mugen87/yuka

Babylon.js 3D engine source: https://github.com/BabylonJS/Babylon.js

Installation

VS Code extensions to install

https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode

https://marketplace.visualstudio.com/items?itemName=ritwickdey.LiveServer

How to run the examples

Open the index.html file located in examples/js/example_dir with LiveServer in VS Code.

Best practices

  1. try to avoid parented TransformNodes with YUKA. YUKA will place your object in world space.Use YUKA's parenting instead.
  2. you must scale, rotate and position your mesh before registering it as a YUKA renderComponent and bake the transformations into the vertices and freeze the world matrix of your mesh before doing so.
  3. you must register your Mesh/TransformNode/Camera on the YUKA entity by setting it as a renderComponent and pass the syncFunction which will take care of syncing your BabylonJS object's position/rotation/scaling into with the YUKA world's position.
const entity = new YUKA.GameEntity()
entity.setRenderComponent(mesh, syncFunction)
  1. syncFunctions: For syncing a TransformNode with the YUKA entity use this method:
private _sync(entity, renderComponent) {
   Matrix.FromValues(...entity.worldMatrix.elements).decomposeToTransformNode(renderComponent)
}

If it doesn't work for you try this one:

renderComponent.getWorldMatrix().copyFrom(BABYLON.Matrix.FromValues(...entity.worldMatrix.elements))

For the camera use this:

private _syncCamera(entity, camera) {
    camera.getViewMatrix().copyFrom(Matrix.FromValues(...entity.worldMatrix.elements).invert())
}
  1. you must register your YUKA entity in the YUKA.EntityManager with it's add function
  2. you must update the YUKA EntityManager's time (make steps in YUKA world) to make things moving like this:
private _time = new YUKA.Time()
this._scene.onBeforeRenderObservable.add(() => {
    const delta = this._time.update().getDelta()
    this._entityManager.update(delta) // YUKA world step
})

License

All these examples are open source, MIT License.

Babylon.js: Apache-2.0 License

Yuka.js: MIT License

3D Models: CC Attribution License (see readme.md in relevant folders)