Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (568)
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  
  Astar algorithm w/ moving object?  (Read 1305 times)
0 Members and 1 Guest are viewing this topic.
Offline wessles

JGO Wizard


Medals: 66
Projects: 4
Exp: 3 years


Profile picture isn't relevant.


« Posted 2013-06-03 21:13:08 »

I have a player moving around, and an enemy trying to track him through blocks. I am using slick2d pathfinding, but this just won't work. The enemy just glitches around, and downright does not follow the path set out for him. This is on a game loop. Please help; this has been stumping me for a month!

Offline UprightPath
« Reply #1 - Posted 2013-06-03 21:16:00 »

Okay, a few questions!
1) Is the enemy finding a path to the player every update step?
2) Is there more than one enemy moving around, and does your implementation taking into account 'occupied' spaces?

Offline wessles

JGO Wizard


Medals: 66
Projects: 4
Exp: 3 years


Profile picture isn't relevant.


« Reply #2 - Posted 2013-06-03 21:24:29 »

Yes, the enemy is updating its path every update method I have in all objects. It updates every Game Container Update.

One player.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline wessles

JGO Wizard


Medals: 66
Projects: 4
Exp: 3 years


Profile picture isn't relevant.


« Reply #3 - Posted 2013-06-03 21:27:44 »

Whoops, read that wrong, and no. There is only 1 enemy and 1 player and a tilemap.

Offline wessles

JGO Wizard


Medals: 66
Projects: 4
Exp: 3 years


Profile picture isn't relevant.


« Reply #4 - Posted 2013-06-03 21:33:45 »

And yes, there is a boolean in each tile called blocked, otherwise, occupied.

Offline UprightPath
« Reply #5 - Posted 2013-06-03 21:43:47 »

Well, it sounds like you're dealing with something called path thrashing. Basically, every time step he finds a route from himself to the player. If they're close together, this'll be a short part, if they're further apart and there are obstacles, then every update he might find a different path.

Are you making sure that you only set tiles as occupied when the player or enemy on it? Because otherwise this might cause other issues.


As a side thought are you sure that the enemy is actually finding a valid path to the player? Do you have a mechanism to handle (Even if it's just a console print out that tells you it couldn't find a path) to handle that situation? If not, I have an idea of what might be going wrong (But I'm not sure since I haven't looked at your code).

Offline wessles

JGO Wizard


Medals: 66
Projects: 4
Exp: 3 years


Profile picture isn't relevant.


« Reply #6 - Posted 2013-06-03 21:48:30 »

I have in the render method of the enemy a for loop that draws on each tile corresponding to the path. The path is straight and true to the player. When he spazzes around, the path starts to flash into a more costly path for a split second.

OHHHH!!! So you mean that the tile should be blocked if there is an entity on it? I only had it blocked when it was dirt, not air. Ill try that and give an update.

Offline wessles

JGO Wizard


Medals: 66
Projects: 4
Exp: 3 years


Profile picture isn't relevant.


« Reply #7 - Posted 2013-06-03 22:23:38 »

Nothing! You wanted the code. My troubleshooting has been at enemy.java.

http://www.mediafire.com/download/a0pky61dk33jrzv/src.rar

Offline UprightPath
« Reply #8 - Posted 2013-06-03 23:08:39 »

Okay, first off the following code chunk is very, very, very bad practice. You need to know that these are happening, and at the very least what they mean to your game. I don't know whether you've considered the implications of having them pop up or not, but I sort of get the feeling that you got hit with errors here and you just tacked on exception handlers until it passed (Basically, it looks like you're handling the no-path issue, which would be the result of all of those failures, by ignoring it, which might be one of the actual issues here).
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
   try {
       if (x < path.getX(1) * 8)
      velx = speed;
       if (x > path.getX(1) * 8)
      velx = -speed;
       if (y < path.getY(1) * 8)
      vely = speed;
       if (y > path.getY(1) * 8)
      vely = -speed;
   } catch (ArrayIndexOutOfBoundsException e) {
   } catch (IndexOutOfBoundsException e) {
   } catch (NullPointerException e) {
   }


Second, I think that this chunk of code might be part of the problem, but I'm unsure. Basically, you're checking if the full step of velx will be good, but then you only do a partial step (Which can lead to having juttering, especially if this is actual logic code).
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
    public void checkAndMove(float delta) {
   float nx = x + velx, ny = y + vely;

   if (validLocation(nx, y))
       x += velx * delta;
   else
       timetoredirect.done = true;
   if (validLocation(x, ny))
       y += vely * delta;
   else
       timetoredirect.done = true;
    }


Copy your project and instead of doing movement by delta, do it by a single, round time step, like a whole tile each time and see if it moves correctly. Another error can be that instead of x /8, you should do Math.round(x / 8.0), since otherwise you're always biasing your stuff to the Math.floor(value) side of where they are, IE: They might be 7 pixels into a tile, almost onto the next, and then your pathfinder will make them treat the tile they're almost off as the tile they're currently on for pathfinding purposes. :3

Offline wessles

JGO Wizard


Medals: 66
Projects: 4
Exp: 3 years


Profile picture isn't relevant.


« Reply #9 - Posted 2013-06-03 23:49:38 »

Are you suggesting to dump the whole subject of timing!? That would mean that faster computers make the entities move faster!

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline wessles

JGO Wizard


Medals: 66
Projects: 4
Exp: 3 years


Profile picture isn't relevant.


« Reply #10 - Posted 2013-06-03 23:55:59 »

What about in tilemap? I think that tilemap has an issue at blocked when I tried to block anything with an entity on it: Updated version:
http://www.mediafire.com/download/t0543rpbp7rtk27/src.zip

Offline UprightPath
« Reply #11 - Posted 2013-06-04 00:50:34 »

No (Well, yes). I'm saying test it/get it working where there's no delta to worry about, then update/refactor it so it works with the delta.

Basically, a game is a Discrete Event Simulation. Nothing happens all at once, though in games we try to make it happen. Typically, in a Discrete Event Simulation, you can ignore the actual time between events and just work on it as 'This even happened what's next', and you can do that for your game. Once you're sure that, if your character is given a delta = 1 (Which is basically what you're doing when you remove it) that it works, you can start attempting to do add in the delta < 1.

I think that's what's going on here, actually. Your character never hits an integer value position. Instead, it ends up vibrating back and forth across the line. Like, it never actually lands on x = 0, instead, it gets x = 1.25, and then has a delta of like .3, and with deltaX being (I think 8?) that's something like 2.5, so it just to -1.25. Then the next time step, it gets a delta of .125, and ends up at .25. Then the next time, it gets a .25 and hits 1.75. And that just keeps happening again and again.

Also, don't do the thing about the player/enemy blocking, I misunderstood what you were doing from the description.

Offline wessles

JGO Wizard


Medals: 66
Projects: 4
Exp: 3 years


Profile picture isn't relevant.


« Reply #12 - Posted 2013-06-04 21:57:46 »

IT WORKED! Thank you! Ill update this discussion if I run into any other issues. Thanks again!

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.

Pippogeek (39 views)
2014-09-24 16:13:29

Pippogeek (30 views)
2014-09-24 16:12:22

Pippogeek (20 views)
2014-09-24 16:12:06

Grunnt (45 views)
2014-09-23 14:38:19

radar3301 (28 views)
2014-09-21 23:33:17

BurntPizza (64 views)
2014-09-21 02:42:18

BurntPizza (34 views)
2014-09-21 01:30:30

moogie (42 views)
2014-09-21 00:26:15

UprightPath (50 views)
2014-09-20 20:14:06

BurntPizza (54 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!