Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (115)
games submitted by our members
Games in WIP (562)
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  
  Making something follow a particular path?  (Read 1503 times)
0 Members and 1 Guest are viewing this topic.
Online wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Posted 2012-12-27 22:27:10 »

I was thinking of making of a tower defense but I am curious of how the attackers follow a certain path like in Bloons Tower Defense , how do they make the balloons follow the path? Thanks in advance

Offline tyeeeee1
« Reply #1 - Posted 2012-12-27 23:26:26 »

If I were to make something like this I'd probably create an array of (X,Y) coords which would outline the entire path. As the sprites appear on the screen I'd just make the sprite go towards point 1, then 2, then 3, etc... It shouldn't be too difficult if you think about it for a bit.  Cheesy
Offline LunaticEdit

Senior Member


Medals: 8
Projects: 1



« Reply #2 - Posted 2012-12-27 23:29:57 »

A common solution is to implement A* pathfinding. You can find many many examples of this online. Here is one I pulled from the interwebs:
http://old.cokeandcode.com/pathfinding

Here's an example from wikipedia of how it works:
Click to Play


This allows the player more challenge in setting up their towers to cause the enemies to move in a different path than they originally did. Otherwise, you can simply have a 2d array of bools defining the grid, where true is the path they follow. Start them on the edge, check each direction and when you find a true, go that way and store the OPPOSITE of your last directional move (UL, U, UR, L, R, DL, D, DR) and make sure you skip that direction on the next tile check.

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

JGO Kernel


Medals: 254
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #3 - Posted 2012-12-27 23:30:36 »

Just make a chain of coordinates to move to, each one only containing the coordinates of the next, then tell the sprites to move through the coordinates in the order of the chain.

Online wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #4 - Posted 2012-12-28 05:24:18 »

How would I tell the spirits to move through a set of coordinates?

Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 131
Projects: 4
Exp: 3 years



« Reply #5 - Posted 2012-12-28 05:38:21 »

@LunaticEdit I think he's asking for predetermined paths/routes that the enemies run through, not pathfinding.
@wreed12345 You can use a linked list or something to store the coordinates, you can use Bresenham's line algorithm to find the tiles that are in between the points (not sure if bresenham's is necessarily a good idea, only experimented with it very briefly).
Offline HeroesGraveDev

JGO Kernel


Medals: 254
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #6 - Posted 2012-12-28 05:42:52 »

How would I tell the spirits to move through a set of coordinates?


Think about it. Think about it very carefully. If you are still stuck, use Google. If that doesn't help, ask here again.

Offline ctomni231

JGO Wizard


Medals: 99
Projects: 1
Exp: 7 years


Not a glitch. Just have a lil' pixelexia...


« Reply #7 - Posted 2012-12-28 06:35:00 »

It completely depends on how your map is organized, but I'm going to assume you have a map filled with tiles on a double array. In this example, I'll use a play field that has 10x10 tiles in length and width.

 int[][] tiles = new int[10][10]; 


To make this pretty easy, you'll want to make a predetermined path for the units to follow. For this example, I'll make the object travel along a straight line. You want to make a basic enemy object that will correspond to your map.

1  
2  
3  
4  
5  
6  
public class Enemy{

     public int x;//The x-axis position of the enemy
    public int y;//The y-axis position of the enemy

}


In the place where you update the code, you'll want to make sure you take timed steps in between each movement. For this object, I'll have him move from
 (5, 10) 
to
 (5, 0) 
.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
public Enemy enemy;

public void init(){

          enemy = new Enemy();
          enemy.x = 5;
          enemy.y = 10;
}
public void update( ... ){

         //Have a certain amount of time elapse before each movement.
        if(enemy > 0)
                enemy.y -= 1;
}


If you want more advanced movements though, the easiest way would be to put the path into an array. Like...

 int[] xPath; 

 int[] yPath; 


You can then traverse each array like a group to have a perfect predetermined path. Here is the same example using this method.

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  
public Enemy enemy;
public int counter;
public int[] xPath;
public int[] yPath;

public void init(){

          enemy = new Enemy();
          enemy.x = 5;
          enemy.y = 10;

          counter = 0;
          xPath = new int[]{ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5};
          yPath = new int[]{10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
}

...

public void update( ... ){

         //Have a certain amount of time elapse before each movement.
        if(counter < xPath.length()){
                 enemy.x = xPath[counter];
                 enemy.y = yPath[counter];
                 counter++;
         }
         
}


These are two valid ways of achieving exact movement along a path. There are more ways to do it of course, and these are very basic. Pac-Man source is pretty helpful when dealing with paths if you needed some deeper knowledge.

Offline matheus23

JGO Kernel


Medals: 107
Projects: 3


You think about my Avatar right now!


« Reply #8 - Posted 2012-12-28 14:31:46 »

So many of you seem to never have played Bloons TD! Sad I liked that game so much when I way younger... yes even younger.

ctomni231 and LunaticEdit, the examples you gave better fit to a tower defense game like Warzone TD. (I personally find this much more cool but yeah...)

Anyways, here is how you'd do it:

You define a set of points. I use a class called
Vec2
, which is basically a class with
public float x, y;
fields.

I then use this to create a path:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
public ArrayList<Vec2> createPath() {
   ArrayList<Vec2> path = new ArrayList<>();
   path.add(new Vec2(0, 0));
   path.add(new Vec2(10.5f, 20.3f));
   path.add(new Vec2(2.4f, 50.2f));
   // ...
  // ...
  // and even more ...
  // and finally:
  path.trimToSize(); // <- optional
  return path; // <- definitely not optional ;D
}

Then you have your entity (I have no idea how your "balloon" class looks like, so I just use a class called
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  
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  
public class Entity() {

   private ArrayList<Vec2> path;
   private Vec2 position;

   // I know the names a pretty long, but I try
  // to make them descriptive. In the end you
  // might want to make them a bit more
  // compact.
 
   // the distance you already traveled
  // (only between the current and next
  // point. This variable is reset to 0, when
  // you reach the next point)
  private float distanceTraveled;
   // distance between the current and next
  // point.
  private float distanceBetweenCurrentPoints;
   
   // the index of the current point.
  private int currentPointIndex = 0;

   public Entity(ArrayList<Vec2> pathToFollow) {
      this.path = pathToFollow; // <- Ah. Here it is again :)
     this.position = new Vec2(path.get(currentPointIndex));
      distanceTraveled = 0f;
      distanceBetweenCurrentPoints = distance(path.get(currentPointIndex), path.get(currentPointIndex+1));
   }

   public void tick() {
      Vec2 lastPoint = path.get(currentPointIndex);
      Vec2 nextPoint = path.get(currentPointIndex+1);
      distanceTraveled = distance(position, lastPoint);
      // So we now got the last Point we wanted to pass
     // and the next point we need to go to.
     // We also have the distance we traveled from the
     // last point in the direction of the next point.
     // We also have the distance between the points
     // we are about to travel along.
     // We now move "forward" by simply adding
     // a little number of pixels to the "distanceTraveled"
     // and then compute a weight from them to update
     // our current position.
     distanceTraveled += /* Speed in pixels */ 1f;
      // But what if we've reached the next point now :O
     if (distanceTraveled > distanceBetweenCurrentPoints) {
         // !!! Could cause ArrayIndexOutOfBoundsException (AIOOBE),
        // if we are at the end of the path ...
        currentPointIndex++;
         // In case we moved 50 pixels, but the distance was
        // only 49 pixels, we already move 1 pixel forward.
        // That looks better in the end.
        distanceTraveled -= distanceBetweenCurrentPoints;
      } else {
         float weight = distanceTraveled / distanceBetweenCurrentPoints;
         // Phew. we've got this.
        position = interpolate(lastPoint, nextPoint, weight, position);
      }
   }

   // This has to be optimized, but that is better
  // done after one has done writing the algorithm.
  // Hint: Use squared distances.
  public float distance(Vec2 point0, Vec2 point1) {
      float dx = point1.x-point0.x;
      float dy = point1.y-point0.y;
      // Hello pythagoras :)
     return Math.sqrt(dx * dx + dy * dy);
   }
   
   // The heart of all methods in this code example!
  // This takes to points and gives you a point on
  // the line from point0 to point1. To define where
  // the point should be on the line, we give the method
  // a weight. This weight should have values between
  // 0 and 1. 0.5 gives the point exact between point0 and point1.
  // positionDst is there, so we don't have to create a new Vec2();
  // everytime we want to get out position. This would increase
  // the memory usage drastically.
  public Vec2 interpolate(Vec2 point0, Vec2 point1, float weight, Vec2 positionDst) {
      // This is just a somehow slightly changed version of getting
     // the average from two numbers. We include weights here.
     positionDst.x = (point0.x * weight) + (point1.x * (1f - weight));
      positionDst.y = (point0.y * weight) + (poitn1.y * (1f - weight));
      return positionDst;
   }
}

This is of course missing a lot of optimization. Especially in the distance part.
This would also cause a AIOOBE when the Entity reaches the end of the path, as described in the code.

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
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 (25 views)
2014-09-21 02:42:18

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

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

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

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

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

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

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

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

mitcheeb (71 views)
2014-09-08 06:06:29
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!