Slyth2727
|
 |
«
Posted
2013-01-17 01: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
|
|
|
|
|
wreed12345
|
 |
«
Reply #1 - Posted
2013-01-17 01:15:50 » |
|
|
|
|
|
|
Agro
|
 |
«
Reply #2 - Posted
2013-01-17 01: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!
|
|
Best Username Ever
|
 |
«
Reply #3 - Posted
2013-01-17 01: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 01:24:01 » |
|
Yeah, practically *everything* can be represented with vectors. 
|
|
|
|
Slyth2727
|
 |
«
Reply #5 - Posted
2013-01-17 01:27:36 » |
|
This is just what I was looking for! Thanks guys, this is a great community.
|
|
|
|
|
wreed12345
|
 |
«
Reply #6 - Posted
2013-01-17 01: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 01: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...
|
|
|
|
|
Agro
|
 |
«
Reply #8 - Posted
2013-01-17 01: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):
|
|
|
|
|
|
Games published by our own members! Check 'em out!
|
|
Slyth2727
|
 |
«
Reply #10 - Posted
2013-01-17 02: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?
|
|
|
|
|
Agro
|
 |
«
Reply #11 - Posted
2013-01-17 02: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 02: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) { 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; } } } } } } } } |
|
|
|
|
|
Agro
|
 |
«
Reply #13 - Posted
2013-01-17 02: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 03: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) { shoot(g); } } |
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); } |
|
|
|
|
|
Agro
|
 |
«
Reply #15 - Posted
2013-01-17 03:55:01 » |
|
Is that in the cannon itself or the cannon tower shooter?
|
|
|
|
Slyth2727
|
 |
«
Reply #16 - Posted
2013-01-17 03: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...
|
|
|
|
|
wreed12345
|
 |
«
Reply #17 - Posted
2013-01-18 02:11:20 » |
|
You dont have to do that although it might help; it is really your decision
|
|
|
|
|
Slyth2727
|
 |
«
Reply #18 - Posted
2013-01-18 02: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?
|
|
|
|
|
Jimmt
|
 |
«
Reply #19 - Posted
2013-01-18 18:30:28 » |
|
For rotating or for finding the rotation angle?
|
|
|
|
|
|