Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (575) Games in Android Showcase (154) games submitted by our members Games in WIP (624) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Make cannon ball follow target in tower defense game  (Read 2821 times) 0 Members and 1 Guest are viewing this topic.
Slyth2727
 « Posted 2013-01-17 00:14:14 »

I am developing a simple tower defense game and have been trying to think of a solution to this for a while: I have a cannon tower, of which I can get the X and Y in pixels and the games coordinates, and I also have it's target who's position moves every frame for it is running down the track. How would I make the cannon that the tower shoots "follow" the target. Picture it as a heat seeking missile, trailing behind it. Help would be much appreciated! Also, if anyone knows a simple way to maybe use slope to return which angle the tower should be pointing at? I have all the pictures on a sprite sheet, loaded up and ready. Thanks!
cMp

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
wreed12345

JGO Knight

Medals: 25
Projects: 2
Exp: 2 years

http://linebylinecoding.blogspot.com/

 « Reply #1 - Posted 2013-01-17 00:15:50 »

Agro
 « Reply #2 - Posted 2013-01-17 00:16:40 »

One way would be to use atan2 to find the angle between the target and the cannonball. However, as the target changes, the angles starts to curve from the original start point. To make it more like a cannonball, you just would want to use atan2 when you initiate the cannonball and it will just go in that direction regardless. You probably would want to predict it before.

Usage:
double angle = Math.atan2(target.y - this.y, target.x - this.x)

Or if you don't want to use all this trig, you can just get a vector direction and normalize it and multiply it by the speed of the cannon ball, and every update, add the direction vector to the cannonball's position.

Junior Devvie

 « Reply #3 - Posted 2013-01-17 00:22:33 »

Easy. Learn vector math. Get the difference between two points, normalize it, multiply by the missile speed. More advanced movement is slightly more complicated, but that is not saying much assuming you know how to use vectors.

Make learning vector math your top priority before you continue.
Agro
 « Reply #4 - Posted 2013-01-17 00:24:01 »

Yeah, practically *everything* can be represented with vectors.

Slyth2727
 « Reply #5 - Posted 2013-01-17 00:27:36 »

This is just what I was looking for! Thanks guys, this is a great community.

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
wreed12345

JGO Knight

Medals: 25
Projects: 2
Exp: 2 years

http://linebylinecoding.blogspot.com/

 « Reply #6 - Posted 2013-01-17 00:29:29 »

Do you guys have any good sources for learning about vectors? that is something very lacking in my knowledge and i feel it is time to learn them!

Slyth2727
 « Reply #7 - Posted 2013-01-17 00:38:25 »

Right, actually to use this value, where would I put it? I use g.drawImage() to draw my image, and I am not quite sure where to insert this variable...

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Agro
 « Reply #8 - Posted 2013-01-17 00:45:52 »

I'm assuming you have a class to represent the Cannon. If you want to make it a linear line for the Cannon do this:

Pass in the position of the cannon and the position of the target to the Cannon class. Have a vector field that will represent the direction of the cannon. Define the direction as follows:

 1  2  3  4 `direction = new Vector2D(target.x - pos.x, target.y - pos.y);direction.normalize();direction.multiply(2);`

Normalizing a vector is getting its length, and dividing the x and y component by the length. To get the length of your vector use this:

 1 `double length = Math.sqrt(x^2 + y^2);`

In the update method(where you handle cannon logic):

 1 `pos.add(direction);`

Agro
 « Reply #9 - Posted 2013-01-17 00:48:25 »

Actually, here is my definition of a vector, you'll see how they work:

https://gist.github.com/4552530

Slyth2727
 « Reply #10 - Posted 2013-01-17 01:03:17 »

Alright, I have all that up and running, but I cant exactly add a vector to my x and y position for those are integers... do I add the length?

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Agro
 « Reply #11 - Posted 2013-01-17 01:10:03 »

The vector values are in doubles, so you can just typecast it to an int.

 1  2 `x += (int) direction.x;y += (int) direction.y;`

Also, could you show me the code where you do your stuff? It'll be easier that way.

Slyth2727
 « Reply #12 - Posted 2013-01-17 01:14:00 »

Oooh I understand! Right here is the code for the TowerCannon class. Keep in mind that in the constructor the methods I am calling are just setting the values needed in the super class, Tower.
 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 `package Engine;import java.awt.Graphics;public class TowerCannon extends Tower {   public int fTime = 0;   public int sTime = 500;   public TowerCannon(int x, int y) {      setRadius(200);      setTowerId(Value.airTowerCannonUp);      setResalePrice(10);      setFirstUpgradePrice(5, 10, 15);      setSecondUpgradePrice(5, 10, 15);      drawTowerAt(x, y, Screen.room.block[y][x].x, Screen.room.block[y][x].y, this);   }   public void fight(Graphics g) {      if (shooting) {         if (fTime >= sTime) {            //call shooting method to shoot and whatnot here....            fTime = 0;         } else {            fTime += 1;         }      }   }   @Override   public void physic() {      if (shooting) {         for (int i = 0; i < Screen.mobs.length; i++) {            if (Screen.mobs[i].inGame) {               if (Screen.mobs[i] != null && circle != null) {                  targetX = Screen.mobs[i].x;                  targetY = Screen.mobs[i].y;               }            }         }      }      if (shotMob != -1 && circle.intersects(Screen.mobs[shotMob])) {         shooting = true;      } else {         shooting = false;      }      if (!shooting) {         if (Screen.room.block[y][x].airID == id) {            for (int i = 0; i < Screen.mobs.length; i++) {               if (Screen.mobs[i].inGame) {                  if (Screen.mobs[i] != null && circle != null) {                     if (circle.intersects(Screen.mobs[i])) {                        shooting = true;                        shotMob = i;                     }                  }               }            }         }      }   }}`

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Agro
 « Reply #13 - Posted 2013-01-17 01:38:34 »

Yeah, so pass in the information of the target to the constructor and calculate everything there for a linear cannonball.

Slyth2727
 « Reply #14 - Posted 2013-01-17 02:19:17 »

Is this what you meant? Because it's not exactly working...

 1  2  3  4  5  6  7  8  9  10 `   public void fight(Graphics g) {      if (shooting) {         // if (fTime >= sTime) {         shoot(g);         // fTime = 0;         // } else {         // fTime += 1;         // }      }   }`

 1  2  3  4  5  6  7  8  9  10  11 `   public void shoot(Graphics g) {      direction = new Vector2D(targetX - Screen.room.block[y][x].x, targetY - Screen.room.block[y][x].y);      direction.normalize();      xPos += direction.x;      yPos += direction.y;      System.out.println("X: " + xPos + " :: Y: " + yPos);      g.drawImage(Screen.tileset_res[Value.resCannonball], xPos, yPos, null);   }`

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Agro
 « Reply #15 - Posted 2013-01-17 02:55:01 »

Is that in the cannon itself or the cannon tower shooter?

Slyth2727
 « Reply #16 - Posted 2013-01-17 02:56:04 »

This is in the TowerCannon class. So your saying make a whole nother class for the cannon ball? Thats interesting, I never thought of that...

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
wreed12345

JGO Knight

Medals: 25
Projects: 2
Exp: 2 years

http://linebylinecoding.blogspot.com/

 « Reply #17 - Posted 2013-01-18 01:11:20 »

You dont have to do that although it might help; it is really your decision

Slyth2727
 « Reply #18 - Posted 2013-01-18 01:52:43 »

I got it all working so happy haha but now I gotta figure out which way to rotate the image to face its target... can you point me towards any resources or ideas?

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Jimmt
« League of Dukes »

JGO Kernel

Medals: 159
Projects: 4
Exp: 3 years

 « Reply #19 - Posted 2013-01-18 17:30:28 »

For rotating or for finding the rotation angle?
Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 ClaasJG (30 views) 2015-04-27 13:36:51 BurntPizza (34 views) 2015-04-23 03:42:11 theagentd (37 views) 2015-04-22 16:23:07 Riven (51 views) 2015-04-16 10:48:47 Duke0200 (60 views) 2015-04-16 01:59:01 Fairy Tailz (44 views) 2015-04-14 20:13:12 Riven (47 views) 2015-04-12 21:36:37 bus hotdog (66 views) 2015-04-10 02:39:32 CopyableCougar4 (67 views) 2015-04-10 00:51:04 BurntPizza (72 views) 2015-04-06 22:06:58
 theagentd 23x BurntPizza 17x Spasi 17x wessles 15x alwex 11x kingroka123 11x Riven 7x kevglass 7x Olo 7x Ecumene 7x ra4king 7x Rayvolution 7x Hanksha 7x chrislo27 7x KevinWorkman 6x trollwarrior1 5x
 How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27
 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