MonstrousSoftware / MonstrousSoftware.github.io

Blog about game dev coding
Creative Commons Attribution Share Alike 4.0 International
0 stars 0 forks source link

2023/11/19/Tutorial-3D-step19 #4

Open utterances-bot opened 5 months ago

utterances-bot commented 5 months ago

3D Tutorial - Step 19 - Navigation Mesh | Coding Adventures

by Monstrous Software

https://monstroussoftware.github.io/2023/11/19/Tutorial-3D-step19.html

gatlanit commented 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?

gatlanit commented 5 months ago

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?

MonstrousSoftware commented 5 months ago

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.

MonstrousSoftware commented 5 months ago

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.

gatlanit commented 5 months ago

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?

MonstrousSoftware commented 5 months ago

In the build() method we extract vertices from the Scene mesh. You could multiply these with the modelInstance transform to move the navmesh.

gatlanit commented 5 months ago

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();
        }
    }
MonstrousSoftware commented 5 months ago

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.

gatlanit commented 5 months ago

Thank you so much! That seems to have done it :)

MonstrousSoftware commented 5 months ago

Yay!

MonstrousSoftware commented 5 months ago

I have added a mini guide on how to use an online tool to create the navmesh. See step 21 of this tutorial.

zqll4 commented 2 months ago

Are there any methods to draw grid lines on a model's surface, and also to draw textures on specific grids?