[libGDX] Converting a path into waypoints using raycasting - Fixed

Hey everybody!

I’ve been working on a little project for a bit over a month now and I’ve been working at making my enemy units pathfind in a reasonable way. I’ve borrowed an A* implementation that seems to work great, but my current issue is trying to make the units behave more like they can actually ‘see’ where they’re headed. I’ve been trying to turn the path that A* gives out and using raycasting to check if a given point is visible. currently, this only works for the first point (the goal, as the path is given in x,y pairs from the goal to the start), so the units completely ignore everything if they can’t see the goal point. I’ve been trying to fix this on my own for a few days but this has become a big roadblock so I figured I’d come to you guys for help. without further ado, here’s the important code block:

	public void trackPlayer(Player player, World world) {
	    if (enemyBody == null)
	        return;
	    if (player.playerBody == null) {
	        enemyBody.setLinearVelocity(0, 0);
	        return;
	    }
	    this.position = enemyBody.getPosition();
	   
	    //create a callback for raycasting. this ray checks if any Box2D objects in the way are part of the level geometry, i.e. have "Room" UserData.
	    RayCastCallback callback = new RayCastCallback() {

	        @Override
	        public float reportRayFixture(Fixture fixture, Vector2 point, Vector2 normal, float fraction) {
	            if (fixture.getUserData() != "Room") {
	                canSeePoint = true;
	                return -1.0f;
	            } else {
	                canSeePoint = false;
	                return 0.0f;
	            }
	        }
	    };
	    int enemyCheckSize = 4;

	        //generate new path based on the grid positions of the player and enemy, returns an intarray with in x,y order.
	        path = astar.getPath((int) (enemyBody.getPosition().x * PPM / 16),
	        		             (int) (enemyBody.getPosition().y * PPM / 16),
	                             (int) (player.playerBody.getPosition().x * PPM / 16),
	                             (int) (player.playerBody.getPosition().y * PPM / 16));
	        for (int i = 0; i<path.size; i+=2) {
	                //convert grid position to real world positions.
	                Vector2 pathCoords = new Vector2((path.get(i)) * 16 / PPM + 8 / PPM,
	                                                 (path.get(i+1)) * 16/PPM + 8f / PPM);
	                rotation = (float) (Math.atan2(position.y - pathCoords.y, position.x - pathCoords.x));
	               
	                //create two separate rays to check if a point is visible to both sides of the entity.
	                translatePosition = new Vector2((float) (position.x - Math.sin(rotation) * enemyCheckSize/PPM),
	                                                (float) (position.y - Math.cos(rotation) * enemyCheckSize/PPM));
	                Vector2 pathCoords2 = new Vector2((float) (pathCoords.x -Math.sin(rotation)*enemyCheckSize/PPM),
	                                                  (float) (pathCoords.y -Math.cos(rotation)*enemyCheckSize/PPM));
	                world.rayCast(callback, translatePosition, pathCoords2);
	                if (canSeePoint) {
	                        translatePosition = new Vector2((float) (position.x + Math.sin(rotation) * enemyCheckSize/PPM),
	                                                        (float) (position.y + Math.cos(rotation) * enemyCheckSize/PPM));
	                        pathCoords2 = new Vector2((float) (pathCoords.x + Math.sin(rotation) * enemyCheckSize/PPM),
	                                                  (float) (pathCoords.y + Math.cos(rotation)*enemyCheckSize/PPM));
	                        world.rayCast(callback, translatePosition, pathCoords2);
	                        if (canSeePoint) {
	                            nextPosition = pathCoords;
	                            break;
	                        }
	                }
	        }
	    //update the enemy's trajectory to the next position.
	    if (nextPosition != null) {
	        Vector2 differencePos = new Vector2(position.x - nextPosition.x, position.y - nextPosition.y);
	        this.radians = (float) Math.atan2(differencePos.y, differencePos.x) - pi;
	        this.direction = new Vector2((float) Math.cos(radians), (float) Math.sin(radians));
	        enemyBody.setTransform(this.position, radians);
	        enemyBody.setLinearVelocity(this.direction.x * speed, this.direction.y * speed);
	    }
	}

if you guys need more of my code or other information, please ask ;D I really want to get this done within the week if possible.

Edit: Turns out I forgot to account for rays that don’t touch a fixture at all, which was my problem.