Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (497)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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  
  My unit teleports instead of moves, A*PathFinder Slick2d  (Read 485 times)
0 Members and 1 Guest are viewing this topic.
Offline sylverfyst

Junior Newbie





« Posted 2013-12-09 16:12:55 »

Here's the important bits of the unit class I use to extend Entity, the init, findPath and the method I use to move. The update method is empty and the render method just draws the sprite at it's position.    For whatever reason it just teleports the unit to the goal, it doesn't even draw the sprite at the start of the game, just at it's final location. 
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
public void init(float x, float y) throws SlickException{
      Image [] mup = {new Image("data/cv_up_1.png"),new Image("data/cv_up_2.png"),new Image("data/cv_up_3.png"),new Image("data/cv_up_4.png")};
      Image [] mdown = {new Image("data/cv_dwn_1.png"),new Image("data/cv_dwn_2.png"),new Image("data/cv_dwn_3.png"),new Image("data/cv_dwn_4.png")};
      Image [] mleft = {new Image("data/cv_lft_1.png"),new Image("data/cv_lft_2.png"),new Image("data/cv_lft_3.png")};
      Image [] mright = {new Image("data/cv_rt_1.png"),new Image("data/cv_rt_2.png"),new Image("data/cv_rt_3.png")};
      Image [] midle = {new Image("data/cv_idle_1.png"),new Image("data/cv_idle_2.png"),new Image("data/cv_idle_3.png")};
      initxpos = x;
      initypos = y;
      xposition = x;
      yposition = y;
      Tile_Map = new IA_EMBLEM_MAP();
      PathFinder = new AStarPathFinder(Tile_Map,100,false, new ManhattanHeuristic(1));
     
      up = new Animation(mup, duration1, true);
      down = new Animation(mdown, duration1, true);
      left = new Animation(mleft, duration2, true);
      right = new Animation(mright, duration2, true);
      idle = new Animation(midle, duration2, true);
      sprite = idle;
   }
public Path findPath(int targetx,int targety){
      return PathFinder.findPath(this, (int)initxpos, (int)initypos, targetx, targety);
   }
public void move(boolean flag, Path p){
      for(int j=0; j<p.getLength();j++){
         Log.debug("Step" + j+ ": " + "x: " + p.getStep(j).getX() + " y: " + p.getStep(j).getY());
      }
      for(int i=1; i<p.getLength();i++){
         if(p.getStep(i).getX() > p.getStep(i-1).getX()){
            sprite = right;
               xposition += 16f;
               if(xposition >= xposition +16){
                  xposition = xposition +16;
               }
               if(xposition >= 496){
                       xposition = 496;
               }
         }
         else if(p.getStep(i).getX() < p.getStep(i-1).getX()){
            sprite = left;
               xposition -= 16f;
               if(xposition <= initxpos - 16){
                  xposition = initxpos - 16;
               }
               if(xposition <= 0){
                       xposition = 0;
               }
         }
         else if(p.getStep(i).getY() < p.getStep(i-1).getY()){
            sprite = up;
               yposition -= 16f;
               if(yposition <= 0){
                       yposition = 0;
               }
         }
         else if(p.getStep(i).getY() > p.getStep(i-1).getY()){
            sprite = down;
               yposition +=  16f;
               if(yposition >= 496){
                       yposition = 496;
               }
         }
         else
         {
            sprite = idle;
         }
      }
   }


And here's the play state's update method:
1  
2  
3  
4  
5  
6  
7  
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException 
   {
      if (played == false){
         played = true;
            unit1.move(played, p);
      }
   }

Any Idea why it could be doing this?
Offline StrideColossus
« Reply #1 - Posted 2013-12-09 16:41:25 »

1. What is the output of the debug log at line #26?  i.e. is the A* routine actually generating a path?  If so then...

2. What is purpose of the loop at #28?  Seems to me that the move() method runs the sprite through the entire path in one go with the result that it teleports to the end of the path - exactly what you're seeing?  Unless this is running in a separate thread shouldn't move() be iteratively updating the position of the sprite on each call to move() then returning to the rendering loop? 

i.e. you would have a class member that is the current index into the path, it would start at one, and move() would increment it to the next position and update the x/y positions, until it reaches the end of the path.

Never used Slick so this might be complete cobblers ofc Wink

- stride
Offline sylverfyst

Junior Newbie





« Reply #2 - Posted 2013-12-09 16:44:43 »

the output of the debug log looks like this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step0: x: 3 y: 4
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step1: x: 3 y: 5
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step2: x: 3 y: 6
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step3: x: 3 y: 7
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step4: x: 3 y: 8
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step5: x: 3 y: 9
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step6: x: 3 y: 10
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step7: x: 3 y: 11
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step8: x: 3 y: 12
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step9: x: 3 y: 13
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step10: x: 3 y: 14
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step11: x: 3 y: 15
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step12: x: 3 y: 16
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step13: x: 3 y: 17
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step14: x: 3 y: 18
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step15: x: 3 y: 19
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step16: x: 3 y: 20
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step17: x: 3 y: 21
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step18: x: 3 y: 22
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step19: x: 3 y: 23
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step20: x: 3 y: 24
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step21: x: 4 y: 24
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step22: x: 5 y: 24
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step23: x: 6 y: 24
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step24: x: 7 y: 24
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step25: x: 8 y: 24
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step26: x: 9 y: 24
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step27: x: 10 y: 24
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step28: x: 11 y: 24
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step29: x: 12 y: 24
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step30: x: 13 y: 24
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step31: x: 14 y: 24
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step32: x: 15 y: 24
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step33: x: 16 y: 24
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step34: x: 17 y: 24
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step35: x: 18 y: 24
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step36: x: 19 y: 24
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step37: x: 20 y: 24
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step38: x: 21 y: 24
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step39: x: 22 y: 24
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step40: x: 23 y: 24
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step41: x: 24 y: 24
Mon Dec 09 11:39:24 EST 2013 DEBUG:Step42: x: 25 y: 24


The loop was stepping through the path, does it not need to do that? It seemed to me to be the only way to get individual steps to the method.  I don't know if there is a current step method.  I know that there's not a next step method. 
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline sylverfyst

Junior Newbie





« Reply #3 - Posted 2013-12-09 16:52:29 »

If I'm understanding correctly then, in update it would be something like:
 
update{
move()
}

in move(){
    getCurrentStep()
    increment unit()
    getNextStep()
}
?
Offline sylverfyst

Junior Newbie





« Reply #4 - Posted 2013-12-09 17:48:44 »

Ok so I've gotten it to move to the target and then display that it's reached it's goal, but it's moving like super fast, is there a way to slow him down? (The Logs are there for me to make sure he's following the right path.  diagonal movement is disabled).  If I change the position assignments it just changes the destination and doesn't get to the target. 

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
public Step getCurrentStep(Path p, int index){
      Step s = null;
      if(index == 0){
         s = this.getFirstStep();
      }
      if(index > 0 && index <= p.getLength()){
       s = this.findPath(25, 24).getStep(index);
      }
      else{
         System.out.println("We're hitting the end");
      }
      return s;
     
   }
   public Step getFirstStep(){
      Step s = this.findPath(25, 24).getStep(0);
      return s;
   }
   public Step getNextStep(Path p, int index){
      Step s = null;
      if(index < p.getLength())
         s = p.getStep(index + 1);
      else{
         System.out.println("We're at the end");
      }
      return s;
   }
   public Step getPreviousStep(Path p, int index){
      Step s = null;
      if(index > 0)
         s = p.getStep(index - 1);
      else{
         System.out.println("We're at the end");
      }
      return s;
   }
   public int incrementCurrentStep(){
      if(!(getIndex() >= this.findPath(25, 24).getLength()))
         setIndex(getIndex() + 1);
      else{
         System.out.println("I'm sorry Dave I can't do that");
         setIndex(-1);
      }
      return getIndex();
   }
   public int move(Path p,int delta){
      int state = 1;
      Step s1 = this.getCurrentStep(p, getIndex());
      Step s2 = this.getNextStep(p, getIndex());
      Log.debug("current step: " +"(" +s1.getX() + " , " + s1.getY() +")" + " next step: " + "(" +s2.getX() + " , " + s2.getY() +")");
      if(s1.getX() < s2.getX()){
         sprite = right;
            xposition += 16f;
            if(xposition > xposition  + 16){
               xposition = xposition + 16;
            }
            if(xposition >= 496){
                    xposition = 496;
            }
      }
      else if(s1.getX() > s2.getX()){
         sprite = left;
            xposition -= 16f;
            if(xposition < xposition - 16){
               xposition = xposition - 16;
            }
            if(xposition <= 0){
                    xposition = 0;
            }
      }
      else if(s1.getY() > s2.getY()){
         sprite = up;
            yposition -= 16f;
            if(yposition < yposition - 16){
               yposition = yposition - 16;
            }
            if(yposition <= 0){
                    yposition = 0;
            }
      }
      else if(s1.getY() < s2.getY()){
         sprite = down;
            yposition += 16f;
            if(yposition > yposition + 16){
               yposition = yposition + 16;
            }
            if(yposition >= 496){
                    yposition = 496;
            }
      }
      this.incrementCurrentStep();
      if(getIndex() == p.getLength()-1){
         if(this.getCurrentStep(p, getIndex()).getX() == 25 && this.getCurrentStep(p, getIndex()).getY() == 24)
            state = 2;
      }
      if(getIndex() == -1){
         setIndex(state);
      }
      return state;
   }


The update method now looks like this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException 
   {
      int state = 1;
     
            state = unit1.move(p, delta);
            Log.debug("Index: " +unit1.getIndex());
            Log.debug("Step" + unit1.getIndex()+ ": " + "x: " + p.getStep(unit1.getIndex()).getX() + " y: " + p.getStep(unit1.getIndex()).getY());
     
      if(state == 2){
         sbg.enterState(state);
      }
      if(state == -1){
         System.out.println("There's been an error maaan");
      }
   }
Offline bilznatch

Senior Member


Medals: 8
Projects: 2
Exp: 1 year


I'm bad, I'm bad, I'm really... really bad T_T


« Reply #5 - Posted 2013-12-10 02:45:23 »

There are two ways to slow the movement, either 1) Make your movement increments smaller or 2) Make a timer of some sort to handle the event. I recommend just using a byte/short here and just moving once every few frames. This means you'll need to do something like this:
1  
2  
3  
4  
5  
6  
7  
short moveFrame;
if(moveFrame < 255){
moveFrame++;
}else{
move();
moveFrame = 0;
}


Those are the ways I would do it based on glancing at your code momentarily.
Offline lcass
« Reply #6 - Posted 2013-12-10 16:37:08 »

To slow down the movement I would personally use a timer to tickby.
Offline Axeman

Senior Member


Medals: 7



« Reply #7 - Posted 2013-12-10 17:14:59 »

If I´m guessing you´re moving one tile every update. That makes your movement "fps tiles per second" so a timer to cap your movement speed would be a good thing.
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.

BurntPizza (15 views)
2014-09-19 03:14:18

Dwinin (33 views)
2014-09-12 09:08:26

Norakomi (58 views)
2014-09-10 13:57:51

TehJavaDev (81 views)
2014-09-10 06:39:09

Tekkerue (40 views)
2014-09-09 02:24:56

mitcheeb (63 views)
2014-09-08 06:06:29

BurntPizza (46 views)
2014-09-07 01:13:42

Longarmx (33 views)
2014-09-07 01:12:14

Longarmx (38 views)
2014-09-07 01:11:22

Longarmx (36 views)
2014-09-07 01:10:19
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!