Mugen87 / yuka

JavaScript library for developing Game AI.
https://mugen87.github.io/yuka/
MIT License
1.13k stars 91 forks source link

YUKA findPath is not detecting any collisions in Babylon.js #77

Closed orion3dgames closed 1 year ago

orion3dgames commented 1 year ago

EDIT : I"VE RESTARTED THE DEMO SERVER AS IT WAS VERY LAGGY.


Hi there,

I've been trying for over a month to get YUKA integrated into my game with no success, but I refused to give up because it is exactly what I need and would be a tremendous amount of work to do myself.

PUBLIC REPO HERE: GITHUB REPO

DEMO HERE SHOWING ISSUE

CLICK FOR DEMO LINK HERE : As you can see, it looks like the navmesh & graph is loaded correctly but findPath is never false so I cannot detect any collisions.

WHAT IS WRONG AND WHAT AM I EXPECTING

I'm expecting foundPath constant below to be false when I move off the navmesh

SNAPSHOT OF MAIN CODE

// IMPORTS
import { NavMesh, Vector3 as Vector3Y } from "yuka";
import loadNavMeshFromString from "../../shared/Utils/loadNavMeshFromString";
import { createConvexRegionHelper, createGraphHelper } from "../../shared/Utils/navMeshHelper";

////////////////////// load navmesh
// https://github.com/oriongunning/t5c/blob/main/src/shared/Utils/loadNavMeshFromString.ts
this._navMesh = await loadNavMeshFromString(global.T5C.currentLocation.key); 

////////////////////// display navmesh
// https://github.com/oriongunning/t5c/blob/main/src/shared/Utils/navMeshHelper.ts
let navMeshGroup = createConvexRegionHelper(this._navMesh, this._scene)
console.log('NAVMESH LOADED', this._navMesh);

////////////////////// display navmesh graph
// https://github.com/oriongunning/t5c/blob/main/src/shared/Utils/navMeshHelper.ts
let graphHelper = createGraphHelper(this._scene, this._navMesh.graph, 0.2)

////////////////////// PLAYER MOVEMENT
// https://github.com/oriongunning/t5c/blob/main/src/shared/Entities/Player/PlayerMove.ts
const foundPath: any = this._navMesh.findPath(new Vector3Y( oldX, oldY, oldZ), new Vector3Y(newX, newY, newZ));
if (foundPath){
    this.nextPosition.x = newX;
    this.nextPosition.y = newY;
    this.nextPosition.z = newZ;
    this.nextRotation.y = this.nextRotation.y + (newRot - this.nextRotation.y);
    console.log('VALID position for', new Vector3Y( oldX, oldY, oldZ), new Vector3Y(newX, newY, newZ), foundPath);
}else{
    console.error('INVALID position for', new Vector3Y( oldX, oldY, oldZ), new Vector3Y(newX, newY, newZ, foundPath));
}

NAVMESH CAN BE FOUND HERE:

all navemesh can be found here: https://github.com/oriongunning/t5c/tree/main/public/models/navmesh

Many thanks, Orion

orion3dgames commented 1 year ago

Ok, after digging into yuka internal working, if I do the following change to yuka.js, everything works as I want....

I was assuming yuka returned an empty array if no valid path was found.... Does this mean, I'm not using using Yuka the way it was intended? Any suggestions?

image

Mugen87 commented 1 year ago

I was assuming yuka returned an empty array if no valid path was found

That is unfortunately not correct. NavMesh.findPath() always returns a path. If the start and end points lie outside of the nav mesh, the implementation tries to find the closest region so a path can be returned.

Navigation is a complex topic in games. And doing it automatically right for all use cases is tricky for an AI engine. Depending on your application logic, you often know that the start and end position of a game entity lie on your nav mesh. Sometimes you don't and automatic region assignment is unwanted since you don't want the AI to move. In this case, use NavMesh.getRegionForPoint() to find out whether a point (e.g. the game entities destination point) lies insides the level/navmesh. If not, don't trigger a path search via NavMesh.findPath().

orion3dgames commented 1 year ago

@Mugen87 Yes, I realize that now and it make sense 100%.

I cannot wait to dig in and see what I can manage to do. I think we can close this now.

Many thanks for taking the time to explain.

ps: i feel a little silly now spending over a month trying to figure it out, looking in the wrong area, and making assumptions.