Skip to content

NavMesh handles are not stable #48

@indietyp

Description

@indietyp

I am currently using this crate for navmesh generation in a 2d top-down gam where I want to create a navmesh for every room, and associate some data with it.

After some debugging, I found out that it doesn't seem that the Handle to the navmesh doesn't seem to be persistent when updates occur, I found this by using the following example, when I do this with multiple navmeshes, the navmesh jumps around, even tho the entity is always the same, this means that you cannot attach any data, or component to it, as the handle might suddenly change between frames.

fn display_navmesh(
    live_navmeshes: Query<(Entity, &Handle<NavMesh>)>,
    mut gizmos: Gizmos<DebugGismos>,
    navmeshes: Res<Assets<NavMesh>>,
    controls: Option<Res<NavMeshesDebug>>,
	selected: Local<Option<Entity>>
) {
    let color = controls.as_ref().map_or(Color::Srgba(RED), |c| c.0);
	let mesh = if let Some(entity) = selected {
		live_navmeshes.get(entity).expect("selected navmesh should exist").1
	} else {
		let (entity, handle) = live_navmeshes.iter().next().expect("should have at least one live navmesh");
		*selected = Some(entity);
		handle
	};

    let Some(navmesh) = navmeshes.get(mesh) else {
        continue;
    };

    let transform = navmesh.transform();
    let navmesh = navmesh.get();

    let compute_matrix = transform.compute_matrix();

    for polygon in &navmesh.polygons {
        if polygon.vertices.is_empty() {
            continue;
        }

        let vertices = polygon
            .vertices
            .iter()
            .map(|i| &navmesh.vertices[*i as usize].coords)
            .map(|v| compute_matrix * Vec4::from((*v, 0.0, 1.0)))
            .map(Vec4Swizzles::xy);

        let first = polygon.vertices[0];
        let first = &navmesh.vertices[first as usize];

        let vertices = vertices.chain(iter::once(
            (compute_matrix * Vec4::from((first.coords, 0.0, 1.0))).xy(),
        ));

        gizmos.linestrip_2d(vertices, color);
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions