Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (784)
Games in Android Showcase (234)
games submitted by our members
Games in WIP (858)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Pathfinding [solved]  (Read 4152 times)
0 Members and 1 Guest are viewing this topic.
Offline Regenuluz
« Posted 2012-10-28 12:28:48 »

Right, so I've started working on a tower defense game!

I've reached a point where I want to implement pathfinding, so that the mobs can go from their spawner to the players base.

I've set up my map as a 2d grid, and I don't have any trouble doing pathfinding on it, but I don't want the mobs to skip a tile at a time, I want them to move in pixels.

So how to I go on about converting a path in a 2d map of 32x32px tiles to the next step the mob should take in pixels? Smiley


A ss to make it a bit more clear how my mobs move. Smiley

Red square on the left, is the monster spawner. Cyan square on the right is the base. Big black dots are mobs. Tiny red dot is a projectile. And the towers are well, towers.. Tongue Oh, and blue is water or other unwalkable terrain.
Offline Orangy Tang

JGO Kernel

Medals: 57
Projects: 11

Monkey for a head

« Reply #1 - Posted 2012-10-28 13:30:25 »

So how to I go on about converting a path in a 2d map of 32x32px tiles to the next step the mob should take in pixels? Smiley

Hopefully your pathfinding returns a list of tile coords that the mob has to move between. Your mob then needs to hold it's current tile (initially it's spawn tile) and it's offset from that tile in pixels. Your target tile is the next tile in the pathfinding list.

Every frame, calculate the direction the mob needs to move in by subtracting the target tile's position from the current position (current position = current tile * tile size + current offset). Normalize the direction, then scale it by the mob's speed, then add it on to the current tile offset. That's your mob's step this turn.

Then check to see if your mob has reached the target tile. This might be just a distance check from the current position and the target tile. If it's small enough, you decide you've reached the target tile, so pop the tile off the pathfinding list. Then update your current tile to the old target tile and update your tile offset accordingly so you're still at the same physical position. Repeat until your pathfinding list is empty (ie you reached your destination).

Alternatively, don't store a pixel tile offset and instead store a 'lerp' amount, and lerp between current and target position. That's essentially the same idea but arriving at it from a slightly different direction.

[ - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline Regenuluz
« Reply #2 - Posted 2012-10-28 17:15:11 »

Thanks, I'll try and implement that either later tonight or tomorrow, when I have time! Smiley

I'll come running back, if I encounter any problems. Tongue
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Varkas
« Reply #3 - Posted 2012-10-30 14:39:43 »

If oyu move tile by tile, a breadth-first search will do the job:

The "next nodes" are the four (or Cool adjacent tiles in your case. It's usually quite easy to implement.

If you need something more sophisticated, look at A*:*_search

if (error) throw new Brick(); // Blog (german):
Offline Regenuluz
« Reply #4 - Posted 2012-10-30 19:09:32 »

But I don't move from tile to tile. I move from (x, y) px position, over tiles. Smiley

The problem isn't the pathfinding, it was converting from a tile position to an (x, y) position in pixels, so that the movement would be smooth. Smiley
Offline Phased
« Reply #5 - Posted 2012-10-31 05:41:57 »

If you want to find what tile the entity is on you could do

//could use double to get a more accurate position within the tile
double tileX = entity.getX() / Tile.WIDTH;
double tileY = entity.getY() / Tile.HEIGHT;

//getting what the x and y is from tileX tileY
//int double what ever ever you prefer
double posX = tileX * Tile.WIDTH;
double posY = tileY * Tile.HEIGHT;

That should get you what tile you are on, and closer to where about in the tile you are if your using a double.
Double would probably be better as then it wont really round up the the next whole number.
Offline Regenuluz
« Reply #6 - Posted 2012-10-31 08:20:42 »

Yes, I already know that ^^

My *only* problem was that I couldn't seem to figure out how to make my movement *smooth*, e.g. not jumping a tile at a time, but only moving n pixels, depending on the entities speed.

But I've figured that out, thanks to Orangy Tang. - Just fetch the next tile that my entity should move towards, and move towards it. ^^
Pages: [1]
  ignore  |  Print  

hadezbladez (1627 views)
2018-11-16 13:46:03

hadezbladez (642 views)
2018-11-16 13:41:33

hadezbladez (1626 views)
2018-11-16 13:35:35

hadezbladez (332 views)
2018-11-16 13:32:03

EgonOlsen (2678 views)
2018-06-10 19:43:48

EgonOlsen (2943 views)
2018-06-10 19:43:44

EgonOlsen (1641 views)
2018-06-10 19:43:20

DesertCoockie (2351 views)
2018-05-13 18:23:11

nelsongames (2255 views)
2018-04-24 18:15:36

nelsongames (2950 views)
2018-04-24 18:14:32
Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!