Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (521)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (589)
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  
  Moving entity to waypoint  (Read 229 times)
0 Members and 1 Guest are viewing this topic.
Offline Gibbo3771
« Posted 2014-03-16 14:51:57 »

I have implemented A* into my game, it shows the correct path on several different layouts and works perfectly.

However I am having a little trouble moving my test entity, each node is a small square inside the center of the tile, basically when the center point of the entity lands in this square, we move to the next mode.

The way I am doing this is

  • Get the next node position and the entity position, use atan2 to return the angle between them
  • Move the player to that node using sin/cos and the angle
  • Once reached, select next node in list, repeat

Problem I seem to have is that my entity can move along the path flawlessly if going negative in the y axis or position in the x axis, when I am moving in the other directions the entity seems to skip several nodes on the path, this results in it trying to cut across a wall or something.

I have not done anything special with my code, it is all hardwired for now so that could be the problem, lack of OO design.

However here is my update method in the entity:

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  
public void update() {

      if (isMoving) {
         if (nextNode < pathFinder.getShortestPath().waypoints.size) {
            Node node = pathFinder.getShortestPath().waypoints
                  .get(nextNode);
            angle = MathUtils.atan2(
                  node.getPosition().y - body.getPosition().y,
                  node.getPosition().x - body.getPosition().x);
            if ((body.getPosition().x < node.getPosition().x - node.SIZE / 5)
                  || (body.getPosition().x > node.getPosition().x
                        + node.SIZE / 5)
                  && (body.getPosition().y < node.getPosition().y
                        - node.SIZE / 5)
                  || (body.getPosition().y > node.getPosition().y
                        + node.SIZE / 5)) {
               body.applyLinearImpulse(speed * MathUtils.cos(angle), speed * MathUtils.sin(angle), 0, 0, true);
               body.setTransform(body.getPosition(), angle);
            } else {
                nextNode++;
            }
         }
      }
     

   }


When it starts to move left or down, it seems to get stuck in the else block for 3-4 nodes, ultimately skipping a few and creating a weird path.

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Offline Gibbo3771
« Reply #1 - Posted 2014-03-16 16:18:30 »

Well I fixed it, seriously been sitting staring at this screen for about 10 hours trying to figure it out.

Anyway, rather than checking the x and y seperate, it seemed like a better idea to use THE BUILT IN FUNCTIONS /slap.

Here is what I do now to calculate the position, compare it to the node and decide if we have reached it yet.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
/**
    * Move this enemy to a destination given by the pathfinder
    */

   public boolean moveToDest() {
      node = path.getWaypoint(nextNode);
      angle = MathUtils.atan2(node.getPosition().y - body.getPosition().y,
            node.getPosition().x - body.getPosition().x);
      if(node.getPosition().dst(body.getPosition()) <= 0.1f){
         nextNode++;
         
      }
      if (body.getPosition() != path.getLastWaypoint().getPosition()) {
         return true;
      }
      return false;
   }


Basically in my update method, if the entity has a path, run this method until it returns false.

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
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.

xFryIx (58 views)
2014-11-13 12:34:49

digdugdiggy (37 views)
2014-11-12 21:11:50

digdugdiggy (30 views)
2014-11-12 21:10:15

digdugdiggy (26 views)
2014-11-12 21:09:33

kovacsa (48 views)
2014-11-07 19:57:14

TehJavaDev (51 views)
2014-11-03 22:04:50

BurntPizza (51 views)
2014-11-03 18:54:52

moogie (66 views)
2014-11-03 06:22:04

CopyableCougar4 (65 views)
2014-11-01 23:36:41

DarkCart (150 views)
2014-11-01 14:51:03
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!