Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
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 1553 times)
0 Members and 1 Guest are viewing this topic.
Offline Regenuluz
« Posted 2012-10-28 13: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

Edit:

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: 51
Projects: 11


Monkey for a head


« Reply #1 - Posted 2012-10-28 14: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.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline Regenuluz
« Reply #2 - Posted 2012-10-28 18: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 15:39:43 »

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

http://en.wikipedia.org/wiki/Breadth-first_search

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*:

http://en.wikipedia.org/wiki/A*_search

if (error) throw new Brick(); // Blog (german): http://gedankenweber.wordpress.com
Offline Regenuluz
« Reply #4 - Posted 2012-10-30 20: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
Online Phased
« Reply #5 - Posted 2012-10-31 06:41:57 »

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

1  
2  
3  
4  
5  
6  
7  
8  
//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 09: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  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (53 views)
2014-04-15 18:08:23

BurntPizza (51 views)
2014-04-15 03:46:01

UprightPath (65 views)
2014-04-14 17:39:50

UprightPath (48 views)
2014-04-14 17:35:47

Porlus (65 views)
2014-04-14 15:48:38

tom_mai78101 (89 views)
2014-04-10 04:04:31

BurntPizza (149 views)
2014-04-08 23:06:04

tom_mai78101 (245 views)
2014-04-05 13:34:39

trollwarrior1 (203 views)
2014-04-04 12:06:45

CJLetsGame (210 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org 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‑gaming.org
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!