# Yuka Game AI Library with Babylon.js

**URL:** <https://forum.babylonjs.com/t/yuka-game-ai-library-with-babylon-js/26902>\
**Category:** Demos and projects\
**Created:** [January 19, 2022, 5:35am UTC](https://forum.babylonjs.com/t/yuka-game-ai-library-with-babylon-js/26902 "2022-01-19T05:35:02Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![labris](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/labris/32/12576_2.png) [@labris](https://forum.babylonjs.com/u/labris)\
**Post date:** [January 19, 2022, 5:35am UTC](https://forum.babylonjs.com/t/yuka-game-ai-library-with-babylon-js/26902/1 "2022-01-19T05:35:02Z")

</div>

[Yuka](https://mugen87.github.io/yuka/) is a JavaScript library for developing Game AI. Yuka is a standalone library and independent of a particular 3D engine. The idea is to implement the actual game logic with Yuka and the visual representation with your preferred JavaScript 3D library.

Until now all Yuka examples were made with Three.js. I started from steering behaviours - have a look at [Yuka | Examples with Babylon.js](https://yuka.babylonpress.org/examples/)  
If you would like to participate in this project, let me know 🙂

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/9/b/9b274c3d8da22bcce532c3044e17f3ca57920848.png)

---

<div class="post-metadata">

**Author:** ![roland](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/roland/32/38940_2.png) [@roland](https://forum.babylonjs.com/u/roland)\
**Post date:** [January 19, 2022, 6:00am UTC](https://forum.babylonjs.com/t/yuka-game-ai-library-with-babylon-js/26902/2 "2022-01-19T06:00:21Z")

</div>

@labris, this is awesome! Put me on the list of participants please! 🖖

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [January 19, 2022, 3:38pm UTC](https://forum.babylonjs.com/t/yuka-game-ai-library-with-babylon-js/26902/3 "2022-01-19T15:38:16Z")

</div>

cc @PirateJC would be so cool to have a showcase link at the top of their site too 😉

---

<div class="post-metadata">

**Author:** ![PirateJC](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/piratejc/32/1003_2.png) [@PirateJC](https://forum.babylonjs.com/u/PirateJC)\
**Post date:** [January 19, 2022, 4:46pm UTC](https://forum.babylonjs.com/t/yuka-game-ai-library-with-babylon-js/26902/4 "2022-01-19T16:46:14Z")

</div>

Very cool @labris! Please keep us posted on this project. It looks very exciting!

---

<div class="post-metadata">

**Author:** ![jeremy-coleman](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/jeremy-coleman/32/15493_2.png) [@jeremy-coleman](https://forum.babylonjs.com/u/jeremy-coleman)\
**Post date:** [January 31, 2022, 7:46pm UTC](https://forum.babylonjs.com/t/yuka-game-ai-library-with-babylon-js/26902/5 "2022-01-31T19:46:12Z")

</div>

I was doing some research on native game engines and concluded yuka drew a lot of “inspiration” from opensteer (developed by sony a long time ago). It also seems to commonly be used in conjunction with recast, which is great because bjs already uses recast. Hopefully, this background knowledge can help widen the search on ideas.

you can see here opensteer + recast combo  
.[Google Code Archive - Long-term storage for Google Code Project Hosting.](https://code.google.com/archive/p/videogames-ai-using-opensteer/source/default/source)

babylon has a recastjs plugin  
.[RecastJSPlugin | Babylon.js Documentation](https://doc.babylonjs.com/typedoc/classes/babylon.recastjsplugin)

and recastjs is included by default in babylon native  
.[BabylonNative/Apps/Dependencies at master · BabylonJS/BabylonNative · GitHub](https://github.com/BabylonJS/BabylonNative/tree/master/Apps/Dependencies)

opensteer resources:  
.[http://opensteer.sourceforge.net/](http://opensteer.sourceforge.net/)  
.[Search · opensteer · GitHub](https://github.com/search?o=desc&q=opensteer&s=updated&type=Repositories)  
.[GitHub - meshula/OpenSteer: OpenSteer is a C++ library to help build steering behaviors for autonomous characters in games and animation.](https://github.com/meshula/OpenSteer) (this one seems a little bit modernized, using glfw and cmake)

alternative / supplemental libs:  
godot 4 ai based pathfinding is based on this lib:  
.[GitHub - snape/RVO2: Optimal Reciprocal Collision Avoidance (C++)](https://github.com/snape/RVO2)  
.[Godot 3.5 Beta - YouTube](https://www.youtube.com/watch?v=9xfqhU1eZSc) first part of this video covers it

RVO2 is using a kd tree which there is an excellent explanation here by the creator of regl:  
.[GitHub - mikolalysenko/static-kdtree: A static kdtree data structure](https://github.com/mikolalysenko/static-kdtree)  
(see also rb tree or red-black tree)

also another great js library  
.[GitHub - anvaka/ngraph.path: Path finding in a graph](https://github.com/anvaka/ngraph.path)  
check this demo out, its very impressive.  
.[GitHub - anvaka/ngraph.path.demo: This is a demo project for ngraph.path](https://github.com/anvaka/ngraph.path.demo)

---

<div class="post-metadata">

**Author:** ![slin](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/slin/32/36084_2.png) [@slin](https://forum.babylonjs.com/u/slin)\
**Post date:** [February 10, 2022, 11:08pm UTC](https://forum.babylonjs.com/t/yuka-game-ai-library-with-babylon-js/26902/6 "2022-02-10T23:08:14Z")

</div>

I want to evaluate YUKA and compare with recast.js. I am too lazy to init a new project, so I tried to get YUKA into a babylon.js PG. It seems to be working fine. Here is a blank PG with YUKA imported.

> **[Babylon.js Playground](https://playground.babylonjs.com/#Q4TM0Z%231)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

**Update:** Somehow after reloading browser, YUKA is not loaded first time and give error. Need to press Run button then it starts working.

---

<div class="post-metadata">

**Author:** ![labris](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/labris/32/12576_2.png) [@labris](https://forum.babylonjs.com/u/labris)\
**Post date:** [February 10, 2022, 11:52pm UTC](https://forum.babylonjs.com/t/yuka-game-ai-library-with-babylon-js/26902/7 "2022-02-10T23:52:45Z")

</div>

Here is one of the simplest Yuka examples - Orientation: [Babylon.js Playground](https://playground.babylonjs.com/#Y44P0J)

You may have a look at more ready Babylon.js examples (before “official” release which will be very soon - [UPD: here it is](https://yuka.babylonpress.org/examples/)) here - [GitHub - eldinor/yuka-babylonjs-examples: Yuka Game AI + 3D rendering with Babylon.js](https://github.com/eldinor/yuka-babylonjs-examples/)

[Navmesh is incredible](https://yuka.babylonpress.org/examples/js/navigation/navmeshPerformance/) 🙂

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/f/b/fbcd70eacdd4fce878ead254042567ec89886b1f.jpeg)

---

<div class="post-metadata">

**Author:** ![slin](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/slin/32/36084_2.png) [@slin](https://forum.babylonjs.com/u/slin)\
**Post date:** [February 11, 2022, 12:53am UTC](https://forum.babylonjs.com/t/yuka-game-ai-library-with-babylon-js/26902/8 "2022-02-11T00:53:19Z")

</div>

Hi @labris

It seems you don’t need your own `animate` function to step babylon scene rendering and yuka world. You can subscribe to `scene.registerBeforeRender`. The babylon way! 😄

> **[Babylon.js Playground](https://playground.babylonjs.com/#Q4TM0Z%2334)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

---

<div class="post-metadata">

**Author:** ![slin](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/slin/32/36084_2.png) [@slin](https://forum.babylonjs.com/u/slin)\
**Post date:** [February 11, 2022, 1:11am UTC](https://forum.babylonjs.com/t/yuka-game-ai-library-with-babylon-js/26902/9 "2022-02-11T01:11:06Z")

</div>

Argh. I was stuck on converting this example to PG for a long time. Then I switched babylon.js version of the PG from 5.0 to 4.2.1. It starts to work!

> **[Babylon.js Playground](https://playground.babylonjs.com/#Q4TM0Z%2322)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

**Update:** I added a hack so YUKA is not loaded error won’t throw when refresh the browser page or launching PG for the first time. 😄

> **[Babylon.js Playground](https://playground.babylonjs.com/#Q4TM0Z%2333)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

I think the problem is from the following code from @labris YUKA example, which is not working in babylon.js 5.0.

```auto
function sync( entity, renderComponent ) {
    entity.worldMatrix.toArray( entityMatrix.m );
    const matrix = renderComponent.getWorldMatrix();
    matrix.copyFrom( entityMatrix );
}

```

`renderComponent` is the cone mesh created in babylon.js. entity is the cone modeled in YUKA environment. It seems this api to set transformation matrix to the cone doesn’t work in babylon.js 5.0 any more, but works in 4.2.

---

<div class="post-metadata">

**Author:** ![labris](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/labris/32/12576_2.png) [@labris](https://forum.babylonjs.com/u/labris)\
**Post date:** [February 11, 2022, 1:15am UTC](https://forum.babylonjs.com/t/yuka-game-ai-library-with-babylon-js/26902/10 "2022-02-11T01:15:36Z")

</div>

For 5.0 one needs to add `entityMatrix.markAsUpdated()`  
This function now looks like

```auto
    function sync(entity, renderComponent) {
        entity.worldMatrix.toArray(entityMatrix.m)
        entityMatrix.markAsUpdated()

        const matrix = renderComponent.getWorldMatrix()
        matrix.copyFrom(entityMatrix)
    }

```

Now it will work with 5.0 - [https://playground.babylonjs.com/#Q4TM0Z#25](https://playground.babylonjs.com/#Q4TM0Z#25)  
All Github examples are already prepared for 5.0.

---

<div class="post-metadata">

**Author:** ![labris](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/labris/32/12576_2.png) [@labris](https://forum.babylonjs.com/u/labris)\
**Post date:** [February 11, 2022, 1:32am UTC](https://forum.babylonjs.com/t/yuka-game-ai-library-with-babylon-js/26902/11 "2022-02-11T01:32:10Z")

</div>

> [@slin](#):
>
> It seems you don’t need your own `animate` function to step babylon scene rendering and yuka world. You can subscribe to `scene.registerBeforeRender` .

Sure, for PG it will work. For standalone examples it may depend.  
Also, Yuka lets to separate the game logic from the rendering engine; it is up to you to decide which one to use.

---

<div class="post-metadata">

**Author:** ![brianzinn](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/brianzinn/32/28013_2.png) [@brianzinn](https://forum.babylonjs.com/u/brianzinn)\
**Post date:** [February 11, 2022, 1:33am UTC](https://forum.babylonjs.com/t/yuka-game-ai-library-with-babylon-js/26902/12 "2022-02-11T01:33:15Z")

</div>

> [@slin](#):
>
> I added a hack so YUKA is not loaded error won’t throw when refresh the browser page or launching PG for the first time.

try this (or you can await instead of .then())

```auto
import('https://mugen87.github.io/yuka/build/yuka.module.js').then(yuka => {
     console.log('imported...', yuka);
     window.YUKA = yuka;
});

```

---

<div class="post-metadata">

**Author:** ![Blake](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/blake/32/21509_2.png) [@Blake](https://forum.babylonjs.com/u/Blake)\
**Post date:** [February 11, 2022, 1:55am UTC](https://forum.babylonjs.com/t/yuka-game-ai-library-with-babylon-js/26902/13 "2022-02-11T01:55:13Z")

</div>

Ooh that was the missing piece, import.then(). 🙂 Here’s a version that awaits the promise without the while loop.

> **[Babylon.js Playground](https://playground.babylonjs.com/#I1EZNX%231)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

---

<div class="post-metadata">

**Author:** ![brianzinn](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/brianzinn/32/28013_2.png) [@brianzinn](https://forum.babylonjs.com/u/brianzinn)\
**Post date:** [February 11, 2022, 3:01am UTC](https://forum.babylonjs.com/t/yuka-game-ai-library-with-babylon-js/26902/14 "2022-02-11T03:01:28Z")

</div>

I just had it in the `createScene` method directly, but that’s another way!! 😄  
ie:

```auto
const YUKA = await import('https://mugen87.github.io/yuka/build/yuka.module.js');

```

---

<div class="post-metadata">

**Author:** ![Blake](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/blake/32/21509_2.png) [@Blake](https://forum.babylonjs.com/u/Blake)\
**Post date:** [February 11, 2022, 3:05am UTC](https://forum.babylonjs.com/t/yuka-game-ai-library-with-babylon-js/26902/15 "2022-02-11T03:05:31Z")

</div>

LOL that’s much simpler, I could have sworn importing modules didn’t work in playground when I tried it a few weeks ago for Rapier. Thanks for info and fix! 🙂

> **[Babylon.js Playground](https://playground.babylonjs.com/#Y44P0J)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

---

<div class="post-metadata">

**Author:** ![roland](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/roland/32/38940_2.png) [@roland](https://forum.babylonjs.com/u/roland)\
**Post date:** [February 11, 2022, 1:26pm UTC](https://forum.babylonjs.com/t/yuka-game-ai-library-with-babylon-js/26902/16 "2022-02-11T13:26:05Z")

</div>

Hey guys!

The BabylonJS examples we are hardly working on with @labris are not intended to demonstrate BabylonJS at it’s best in the first round but mainly to show YUKA’s capabilities.

We are using the THREE examples for the base code and the existing helper classes created for THREE and are rewriting them. There are lot of things we would do absolutely differently but starting from scratch would take ages to finish the examples and we wanted to deliver ASAP.

After we release the first version feel free to contribute and rewrite the examples in a better way. So basically the examples are not currently about BabylonJS best practices.

However we have plans to rewrite the examples in TS from scratch implemented with best approaches known to us.

@slin the same applies to the `animate` method.

🖖

---

<div class="post-metadata">

**Author:** ![ecoin](https://avatars.discourse-cdn.com/v4/letter/e/8e7dd6/32.png) [@ecoin](https://forum.babylonjs.com/u/ecoin)\
**Post date:** [February 12, 2022, 2:35pm UTC](https://forum.babylonjs.com/t/yuka-game-ai-library-with-babylon-js/26902/17 "2022-02-12T14:35:19Z")

</div>

> [@labris](#):
>
> [https://playground.babylonjs.com/#Q4TM0Z#3](https://playground.babylonjs.com/#Q4TM0Z#3)

This PG does not work in 5.0 anymore. Same with this:

> [@labris](#):
>
> [https://playground.babylonjs.com/#Q4TM0Z#25](https://playground.babylonjs.com/#Q4TM0Z#25)

---

<div class="post-metadata">

**Author:** ![labris](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/labris/32/12576_2.png) [@labris](https://forum.babylonjs.com/u/labris)\
**Post date:** [February 12, 2022, 2:37pm UTC](https://forum.babylonjs.com/t/yuka-game-ai-library-with-babylon-js/26902/18 "2022-02-12T14:37:20Z")

</div>

It works if you hit the Run button again.  
Version with the correct script loading is here - [Babylon.js Playground](https://playground.babylonjs.com/#Y44P0J)

---

<div class="post-metadata">

**Author:** ![labris](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/labris/32/12576_2.png) [@labris](https://forum.babylonjs.com/u/labris)\
**Post date:** [February 21, 2022, 5:33pm UTC](https://forum.babylonjs.com/t/yuka-game-ai-library-with-babylon-js/26902/19 "2022-02-21T17:33:17Z")

</div>

And here is the link to the 1st release thread - [Yuka Game AI + Babylon.js Examples - the 1st Release](https://forum.babylonjs.com/t/yuka-game-ai-babylon-js-examples-the-1st-release/27690)
