Skip to content

Commit

Permalink
feat: add example for computing a smooth path
Browse files Browse the repository at this point in the history
  • Loading branch information
isaac-mason committed Jun 23, 2024
1 parent 0576950 commit fea697b
Show file tree
Hide file tree
Showing 8 changed files with 695 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/recast-navigation-core/src/detour.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export class DetourLink {
}

next(): number {
return this.raw.next;
return this.raw.get_next();
}

edge(): number {
Expand Down
17 changes: 15 additions & 2 deletions packages/recast-navigation-core/src/nav-mesh-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,7 @@ export class NavMeshQuery {
findStraightPath(
start: Vector3,
end: Vector3,
path: UnsignedIntArray,
path: number[] | UnsignedIntArray,
options?: {
/**
* The maximum number of points the straight path arrays can hold. [Limit: > 0]
Expand Down Expand Up @@ -925,6 +925,15 @@ export class NavMeshQuery {
const maxStraightPathPoints = options?.maxStraightPathPoints ?? 256;
const straightPathOptions = options?.straightPathOptions ?? 0;

let pathPolys;

if (Array.isArray(path)) {
pathPolys = new UnsignedIntArray();
pathPolys.copy(path);
} else {
pathPolys = path;
}

const straightPath = new FloatArray();
straightPath.resize(maxStraightPathPoints * 3);

Expand All @@ -939,7 +948,7 @@ export class NavMeshQuery {
const status = this.raw.findStraightPath(
vec3.toArray(start),
vec3.toArray(end),
path.raw,
pathPolys.raw,
straightPath.raw,
straightPathFlags.raw,
straightPathRefs.raw,
Expand All @@ -951,6 +960,10 @@ export class NavMeshQuery {
const straightPathCount = straightPathCountRaw.value;
Raw.destroy(straightPathCountRaw);

if (Array.isArray(path)) {
pathPolys.destroy();
}

return {
success: Raw.Detour.statusSucceed(status),
status,
Expand Down
1 change: 1 addition & 0 deletions packages/recast-navigation-core/src/nav-mesh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ export class NavMesh {
Raw.destroy(endRaw);

return {
success: statusSucceed(status),
status,
start,
end,
Expand Down
4 changes: 4 additions & 0 deletions packages/recast-navigation-wasm/recast-navigation.idl
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,10 @@ interface Detour {
attribute octet TILECACHE_WALKABLE_AREA;
attribute unsigned short TILECACHE_NULL_IDX;

attribute unsigned long NULL_LINK;
attribute unsigned short EXT_LINK;
attribute unsigned long OFFMESH_CON_BIDIR;

boolean statusSucceed(unsigned long status);
boolean statusFailed(unsigned long status);
boolean statusInProgress(unsigned long status);
Expand Down
4 changes: 4 additions & 0 deletions packages/recast-navigation-wasm/src/Detour.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class Detour
unsigned char TILECACHE_WALKABLE_AREA = DT_TILECACHE_WALKABLE_AREA;
unsigned short TILECACHE_NULL_IDX = DT_TILECACHE_NULL_IDX;

unsigned int NULL_LINK = DT_NULL_LINK;
unsigned short EXT_LINK = DT_EXT_LINK;
unsigned int OFFMESH_CON_BIDIR = DT_OFFMESH_CON_BIDIR;

bool statusSucceed(dtStatus status)
{
return dtStatusSucceed(status);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { OrbitControls } from '@react-three/drei';
import { ThreeEvent } from '@react-three/fiber';
import { NavMeshQuery } from '@recast-navigation/core';
import { NavMeshQuery, Raw } from '@recast-navigation/core';
import { DebugDrawer, threeToSoloNavMesh } from '@recast-navigation/three';
import React, { useEffect, useState } from 'react';
import * as THREE from 'three';
Expand Down Expand Up @@ -69,10 +69,10 @@ export const FloodFillPruning = () => {
const { poly, tile } = navMesh.getTileAndPolyByRefUnsafe(ref);

// visit linked polys
const DT_NULL_LINK = 0xffffffff;
for (
let i = poly.firstLink();
i !== DT_NULL_LINK && i !== -1;
// https://github.com/emscripten-core/emscripten/issues/22134
i !== Raw.Detour.NULL_LINK && i !== -1;
i = tile.links(i).next()
) {
const neiRef = tile.links(i).ref();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ const agentMaterial = new MeshStandardMaterial({
color: 'red',
});

const _navMeshOnPointerDownVector = new Vector3();

export const CrowdWithSingleAgent = () => {
const agentTargetSpanRef = useRef<HTMLSpanElement>(null!);
const agentNextTargetPathSpanRef = useRef<HTMLSpanElement>(null!);
Expand All @@ -51,16 +53,21 @@ export const CrowdWithSingleAgent = () => {

const agentRadius = 0.1;
const cellSize = 0.05;
const cellHeight = 0.05;

const { success, navMesh } = threeToSoloNavMesh(meshes, {
cs: cellSize,
ch: 0.2,
walkableRadius: Math.ceil(agentRadius / cellSize),
ch: cellHeight,
walkableRadius: Math.ceil(0.3 / cellSize),
borderSize: 5,
// maxEdgeLen: 2,
// walkableHeight: Math.floor(1 / cellHeight),
});

if (!success) return;

const navMeshQuery = new NavMeshQuery(navMesh);
// navMeshQuery.defaultQueryHalfExtents = { x: 0.5, y: 0.1, z: 0.5 }

const crowd = new Crowd(navMesh, { maxAgents: 1, maxAgentRadius: 0.2 });

Expand Down Expand Up @@ -118,7 +125,13 @@ export const CrowdWithSingleAgent = () => {

e.stopPropagation();

const { point: target } = navMeshQuery.findClosestPoint(e.point);
const point = _navMeshOnPointerDownVector.copy(e.point)

navMeshQuery.defaultQueryHalfExtents.x = 0.01;
navMeshQuery.defaultQueryHalfExtents.z = 0.01;
navMeshQuery.defaultQueryHalfExtents.y = 0.01;
const { nearestPoint: target } = navMeshQuery.findNearestPoly(point);


if (e.button === 2) {
agent.teleport(target);
Expand Down
Loading

0 comments on commit fea697b

Please sign in to comment.