Open utterances-bot opened 5 months ago
I tried making a NavMesh like yours in my map but my npc keeps getting stuck in the corners. I tried to make the gaps bigger and smaller but it still happens, is there a specific way I should be making the corners? Unlike your map, mine is full of straight hallways instead of an open map; almost maze-like, could that be an issue?
It seems that the path of the enemy is getting too close to my map's NavMesh corners. Is there a way to prevent this?
You need to leave some gap between the sides of the navmesh and the walls or obstacles. The enemy will go right to the navmesh corners by design because that is the shortest path. So the navmesh edges and corners needs to have a distance from the walls of a a bit more than the enemy’s radius.
You could also use the colouring of navmesh triangles with the step number to check that the Dijkstra algorithm is resolving a path from enemy to player in various positions and that it doesn’t get stuck or anything. It is a helpful trick to check your navmesh is valid.
I remade the NAVMESH with your suggestions but now my NAVMESH is in the incorrect spot, moving it manually as such: new Vector3(x, y, z); doesn't move the NAVMESH at all, any idea what that could be?
In the build() method we extract vertices from the Scene mesh. You could multiply these with the modelInstance transform to move the navmesh.
Apologies for having so much trouble, but is it possible to make the enemy not get stuck when traveling towards a specific vector coordinate? I'm trying to make a roam feature where the enemy just travels towards a selected coordinate, once it reaches goes onto the next one. I don't have this issue as much when the player is moving, only when it is a static coordinate such as player standing still or a specific coordinate as its target. Any ideas as to why this could happen or how to fix? This is my code at the moment for cook behavior:
@Override
public void update(World world, float deltaTime ) {
if (isChasing) {
playerVector.set(world.getPlayer().getPosition()).sub(go.getPosition()); // vector to player in a straight line
float distance = playerVector.len();
if (navActor == null) { // lazy init because we need world.navMesh
navActor = new NavActor(world.navMesh);
}
Vector3 wayPoint = navActor.getWayPoint(go.getPosition(), world.getPlayer().getPosition()); // next point to aim for on the route to target
float climbFactor = 1f;
if (navActor.getSlope() > 0.1f) { // if we need to climb up, disable the gravity
go.body.geom.getBody().setGravityMode(false);
climbFactor = 2f; // and apply some extra force
} else
go.body.geom.getBody().setGravityMode(true);
// move towards waypoint
targetDirection.set(wayPoint).sub(go.getPosition()); // vector towards way point
if (targetDirection.len() > 1f) { // if we're at the way point, stop turning to avoid nervous jittering
targetDirection.y = 0; // consider only vector in horizontal plane
targetDirection.nor(); // make unit vector
direction.slerp(targetDirection, 0.02f); // smooth rotation towards target direction
go.body.applyForce(targetDirection.scl(20f * climbFactor));
}
}
else // roam
{
randVector.set(randPositions.get(index).sub(go.getPosition())); // vector to player in a straight line
float distance = randVector.len();
fixValues();
if (navActor == null) { // lazy init because we need world.navMesh
navActor = new NavActor(world.navMesh);
}
Vector3 wayPoint = navActor.getWayPoint(go.getPosition(), (randPositions.get(index))); // next point to aim for on the route to target
float climbFactor = 1f;
if (navActor.getSlope() > 0.1f) { // if we need to climb up, disable the gravity
go.body.geom.getBody().setGravityMode(false);
climbFactor = 2f; // and apply some extra force
} else
go.body.geom.getBody().setGravityMode(true);
// move towards waypoint
targetDirection.set(wayPoint).sub(go.getPosition()); // vector towards way point
if (targetDirection.len() > 1f) { // if we're at the way point, stop turning to avoid nervous jittering
targetDirection.y = 0; // consider only vector in horizontal plane
targetDirection.nor(); // make unit vector
direction.slerp(targetDirection, 0.02f); // smooth rotation towards target direction
go.body.applyForce(targetDirection.scl(20f * climbFactor));
}
if (distance < 1f)
nextPos();
}
}
NavActor is supposed to move towards the closest waypoint and once it is reached (distance < CLOSE) switch to the next waypoint. It sounds like NavActor is not switching to the next waypoint but then leaves the cook stuck at an interim waypoint. Perhaps increasing the CLOSE constant could help or set a breakpoint to see what is happening exactly.
Thank you so much! That seems to have done it :)
Yay!
I have added a mini guide on how to use an online tool to create the navmesh. See step 21 of this tutorial.
Are there any methods to draw grid lines on a model's surface, and also to draw textures on specific grids?
3D Tutorial - Step 19 - Navigation Mesh | Coding Adventures
by Monstrous Software
https://monstroussoftware.github.io/2023/11/19/Tutorial-3D-step19.html