Tower Defense: Computing distance between a tower and an enemy

Hello guys, I’ve mean to post this like a week ago, and I wrote a really cool post… and then, unintentionally I pressed Crl+W (in Chrome) and, bye bye nice post… So, I don’t know if this one is gonna be as explicative as the other one. Btw, I don’t really know if “General” is the category for this kind of troubleshooting, so feel free to move this post to where it fits. And, I must say, I’m spanish speaker, so you’ll probably find some writing mistakes…

Well, im working on this Tower Defense game, the first game in jME that I intend to finish, the others where just to get knowing the SDK.
The game won’t be commercial, it’s mainly for learning purposes and fun.
The main question in this thread, is going to be what is giving me trouble right now… It doesn’t seem to hard to fix, but i’m overwhelmed with this issue, so i’m probably not looking it the right way.
Context:
I have Towers and Creeps (Creep == Monster/Enemy). Towers attacks any creep that is in their range of fire, each x seconds, wich is the tower fire rate. The only reason a Tower stops attacking a Creep, is when neither the creep is out of range, or death.
A group of Creeps is generated each X seconds (depending on the user’s level), and this group will approach the user’s base (for now position (0,0,0) ).
When a Creep is created, it’s also added to an ArrayList of creeps, wich are the creeps in the game right now. (they’re also detached when they die)
So, I’ve a TowerControl added to each tower Spatial, wich is in charge of computing the distance from this Tower to each creep in the world, till it find’s one (the first) that is in the tower’s range, and shoot at it, every x seconds until the creep is dead or not in range any more.
I’m gonna attach some code from the TowerControl class, just the relevant parts. You can see the whole code at the repository.

[java]@Override
protected void controlUpdate(float tpf) {
// if (gamePlayAppState.getCreepList().size() > 0) {
if (target == null || target.getState() == CREEP_STATE.DEATH) {
target = getNextTarget();
}
if ((timeToNextShoot <= 0) && (target != null && target.getState() == CREEP_STATE.ALIVE)) {
if (isInRange(target)) {
shoot(target);
timeToNextShoot = tower.getFireRate();
} else {
target = getNextTarget();
}
}
if (timeToNextShoot <= tower.getFireRate() - 1) {
int c = gamePlayAppState.getTowerNode().getChildIndex(bulletSp);
if (c > 0) {
gamePlayAppState.getTowerNode().getChild©.removeFromParent();
}
}
// if (target != null) {
timeToNextShoot -= tpf;
// } else {
// timeToNextShoot = 0;
// }
}
private Creep getNextTarget() {
for (Creep creep : gamePlayAppState.getCreepList()) {
if (isInRange(creep)) {
return creep;
// System.out.println("Tower " + tower.getName() + " Found Creep " + creep.getName());
}
}
return null;
}
private Boolean isInRange(Creep creep) { //spatial is the tower.
// float distanceFromTower = creep.getSpatial().getLocalTranslation().subtract(spatial.getLocalTranslation()).length();
float distanceFromTower = spatial.getLocalTranslation().distance(creep.getSpatial().getLocalTranslation());
System.err.println(creep.getName() + " Dis > " + distanceFromTower);
return distanceFromTower <= tower.getRange();
}

[/java]

Now, this works. And it works pretty good for what I need right now…
But
For some reason, it only works the first time Creep’s are created. To explain:
Creeps are created, towers shoot at them, they die or reach user base without problem. Everything works fine till here.
Then, the second “wave” of creeps is created (at same position as the first one) and for some reason, the distanceFromTower variable in the isInRange() method in the TowerControl class is giving a higher value than it did at the first wave although the creeps are at the same position.
At the third wave of creeps, this is getting even worst and tower’s wont attack any creep, obviously becouse they think creeps are not in range.

So, I belive that the computing distance part is fine, I took the formula from the math cheat sheet. Also, when the first, second, and third (and every) wave of creep is created, the spatials are showing at the same position in the screen.

I decided also to add a video, where you can see the console showing the distance from one tower to creeps.
You must be aware that I aint no graphic designer :stuck_out_tongue:
[video]TD Bugs - YouTube
This tower has a range of 60, as you can see, at the begening, the distance is ~30, at the second wave its like ~45, and the third wave it’s ~ 55.
This has made me think, that maybe my entire messure system (for tower range) is wrong…

Remeber that you can check the whole source code here

Thank you very much for your time. If you have any suggestion beyond this problem, please feel free to suggest.
Thanks!!

1 Like

I don’t think the problem is in the code you posted. Print the location values of the two objects and I think you will see that they locations are wrong.

1 Like
@pspeed said: I don't think the problem is in the code you posted. Print the location values of the two objects and I think you will see that they locations are wrong.

Mate I didn’t think the problem was in the code I posted either… but I wasnt sure… I did as you said, printed both locations and in a matter of seconds realized the issue. lol, I said I was overwhelmed.
I’m gonna keep testing, but for now, I’d say it’s solved.
Thank you very much!

1 Like
@turco.chaia said: Mate I didn't think the problem was in the code I posted either.. but I wasnt sure.. I did as you said, printed both locations and in a matter of seconds realized the issue. lol, I said I was overwhelmed. I'm gonna keep testing, but for now, I'd say it's solved. Thank you very much!

If you haven’t already, I’d love to see a post in the User Code & Projects section to read about updates, see screen shots and vids as you progress with this.

I’ve heard lots of people talk about their Tower Defense type games, but I think this is the first vid I have see to get an idea of what one might be. (I’m not overly familiar with the type of game… but it looks like it would be fun)

Maybe for us less than knowledgeable sorts about the genre, you could give a quick description of game play as well (more a what is unique about your take on the game type, etc)?

And lastly… maybe you could post what you plan to do with the game once you finish it up (i.e. what platforms you are targeting, where you see the game being purchased from, etc)

1 Like

@t0neg0d I haven’t posted in the User Code & Projects sections yet, I will do it when I have a more playable game so I can upload some sort of alpha/beta test, for now I’ m just working on the game logics and the gui, and trying to make everything really easy-to-update, and also easy to add new Creeps and Towers.
Although since this fix, it’s looking pretty playable!
I promiss I’ll make a post in the Projects sections and explain about the genre and the uniqueness of my proyect as soon as I’ve some sort of beta test.

@t0neg0d said: And lastly... maybe you could post what you plan to do with the game once you finish it up (i.e. what platforms you are targeting, where you see the game being purchased from, etc)
That can be answered right away, I'm not planning on selling this game, as I said in the original thread this is just for learning purposes, and fun (many of my friends used to enjoy this sorts of game when we were young). About platforms, I'm targeting Windows for now, but I'm a Ubuntu user too so I may add Linux to.
1 Like
@turco.chaia said: @t0neg0d I haven't posted in the User Code & Projects sections yet, I will do it when I have a more playable game so I can upload some sort of alpha/beta test, for now I' m just working on the game logics and the gui, and trying to make everything really easy-to-update, and also easy to add new Creeps and Towers. Although since this fix, it's looking pretty playable! I promiss I'll make a post in the Projects sections and explain about the genre and the uniqueness of my proyect as soon as I've some sort of beta test.

That can be answered right away, I’m not planning on selling this game, as I said in the original thread this is just for learning purposes, and fun (many of my friends used to enjoy this sorts of game when we were young).
About platforms, I’m targeting Windows for now, but I’m a Ubuntu user too so I may add Linux to.

Awesome… thanks for this. And looking forward to seeing updates.

Will you be sharing the final game for others to play? I’d be very interested in trying it when it is completed :wink:

@t0neg0d said: Awesome... thanks for this. And looking forward to seeing updates.

Will you be sharing the final game for others to play? I’d be very interested in trying it when it is completed :wink:

Yep! the game will be free, and open source. I’ll probably upload it to my website.
As a matter of fact, I was also thinking about doing an English traslation, so that you guys in the forum can check it out!

2 Likes
@turco.chaia said: Yep! the game will be free, and open source. I'll probably upload it to my website. As a matter of fact, I was also thinking about doing an English traslation, so that you guys in the forum can check it out!

Can’t wait!

1 Like