Slick Forums

Discuss the Slick 2D Library
It is currently Sun May 19, 2013 2:43 pm

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Tue Dec 16, 2008 10:28 pm 
Offline
Regular

Joined: Sat May 31, 2008 12:26 am
Posts: 207
I am not sure about this, because when I give it tile coordinates, and a valid TileBasedMap with a well-defined way of detecting blockages, it doesn't work when I assume tile coordinates are being used.

The API doesn't provide any documentation on this, so I assumed it was referring to tile coordinates.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 16, 2008 10:36 pm 
Offline
Site Admin
User avatar

Joined: Thu Jan 01, 1970 12:00 am
Posts: 3143
Tile coordinates, yep. The tile based map path finder isn't given any details of the size of your tiles so it can't be pixel coordinates.

Kev


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 16, 2008 10:39 pm 
Offline
Regular

Joined: Sat May 31, 2008 12:26 am
Posts: 207
Hmm, I wonder what I'm doing wrong then.

Here is my entire TileBasedMap class:
Code:
import org.newdawn.slick.tiled.TiledMap;
import org.newdawn.slick.util.pathfinding.PathFindingContext;
import org.newdawn.slick.util.pathfinding.TileBasedMap;


public class RTSMap implements TileBasedMap
    {    TiledMap map;
   boolean[][] collisionmap;
       float[][] costMap;
       
       
       public RTSMap(TiledMap m)
       { map=m;
      collisionmap=new boolean[m.getWidth()][m.getHeight()];
      
      for(int x=0; x<m.getWidth(); x++)
      {
          for(int y=0; y<m.getHeight(); y++)
          {
              int property=Integer.parseInt(m.getTileProperty(m.getTileId(x, y, 1), "Passable", "1"));
              if(property==1)
                  collisionmap[x][y]=true;
          }
         
         
      }
       }

   @Override
   public boolean blocked(PathFindingContext pathfinder, int x, int y)
   {
      System.out.println(!(collisionmap[x][y]));
       return !(collisionmap[x][y]);
   }

   @Override
   public float getCost(PathFindingContext pathfinder, int x, int y)
   {
   
       return 1;
   }

   @Override
   public int getHeightInTiles()
   {
   
       return map.getHeight();
   }

   @Override
   public int getWidthInTiles()
   {
   
       return map.getWidth();
   }

   @Override
   public void pathFinderVisited(int x, int y)
   {
   
   }

    }


And I know for a fact using debug data that the collision map parsing works just fine.

The System.out.println in blocked() reveals that the AstarPathfinder looks at one tile, finds it to not be blocked and then returns null.

Here is some code for my Mover:
Code:


//Find a path between you and the pixel coordinate target "target"
public void setTarget(Vector2f target)
       {
           this.target = target;
           fitTargettoGrid();
           findPath();
           currentStep=0;
       }

//Convert pixel coordinates of "target" to grid coordinates.
public void fitTargettoGrid()
       {
      int currentTileX=(int) (target.x/Game.currentMap.getTileWidth());
        int currentTileY=(int) (target.y/Game.currentMap.getTileHeight());
        target=new Vector2f(currentTileX,currentTileY);
       }

//Fit your own coordinates to grid coordinates.
  public void findGridPosition()
       {
             int currentTileX=(int) (parent.getMoveSystem().pos.x/Game.currentMap.getTileWidth());
        int currentTileY=(int) (parent.getMoveSystem().pos.y/Game.currentMap.getTileHeight());
        gridPosition=new Vector2f(currentTileX,currentTileY);
       }

//Find a path from you to the target.
public void findPath()
       {  findGridPosition();
      pathtoTarget=pathfinder.findPath((Mover)this, (int)gridPosition.getX(), (int)gridPosition.getY(), (int)target.getX(), (int)target.getY());
      if(pathtoTarget==null)
      {
          System.out.println("ITS NULL");
      }
       }



Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 16, 2008 11:52 pm 
Offline
Regular

Joined: Sat May 31, 2008 12:26 am
Posts: 207
Further debug testing reveals that "pathFinderVisited(" is never called.

Looking in the source code, the only way this would be possible is if the target node were blocked, but putting in a debug statement to check for this reveals that the target node is not blocked, so this is a contradiction.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 17, 2008 1:19 am 
Offline
Regular

Joined: Sat May 31, 2008 12:26 am
Posts: 207
I found out my error!

I was passing it "0" as the max search distance by accident.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group