Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (494)
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  
  Make cannon ball follow target in tower defense game  (Read 2158 times)
0 Members and 1 Guest are viewing this topic.
Offline 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?
Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


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

This may be of help
http://www.java-gaming.org/topics/creating-projection-of-a-projectile-using-linear-algebra/28310/msg/256834/view.html

Offline 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.

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

Junior Member





« 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.
Offline Agro
« Reply #4 - Posted 2013-01-17 00:24:01 »

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

Offline 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?
Offline wreed12345

JGO Knight


Medals: 24
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!

Offline 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?
Offline 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);

Offline 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

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 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?
Offline 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? Smiley It'll be easier that way.

Offline 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?
Offline 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.

Offline 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?
Offline Agro
« Reply #15 - Posted 2013-01-17 02:55:01 »

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

Offline 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?
Offline wreed12345

JGO Knight


Medals: 24
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

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

I got it all working Smiley 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?
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 130
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.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

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

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

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

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

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

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

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

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

Longarmx (28 views)
2014-09-07 01:10:19

mitcheeb (36 views)
2014-09-04 23:08:59
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!