Bullet targeting an Enemy

Anything related in any way to game development as a whole is welcome here. Tell us about your game, grace us with your project, show us your new YouTube video, etc.

Moderator: PC Supremacists

Post Reply
Duncan
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 1
Joined: Sun Sep 20, 2009 2:19 am

Bullet targeting an Enemy

Post by Duncan »

Hey everyone. I'm working on making a Tower Defense game. The designer tells me he wants the towers to shoot out bullets that will "follow" an enemy until they collide. He also stated that if two towers fire at one enemy and the enemy is killed before one bullet reaches the enemy, then the bullet that doesn't reach the enemy should just disappear.

I'm having a bit of trouble implementing this. The "following" mechanism is not too bad cuz that's just a matter of some subtraction and whatnot but the tricky bit is how to inform the bullets when their targeted Enemy dies. The only solution I could think of would be to give each Enemy a std::vector of Bullet pointers and then when each bullet is assigned a target have that Push its own "this" pointer into the Enemy's vector... and THEN in the Enemy destructor do something like

Code: Select all

for (int i=0; i < followers.size(); i++)
  followers[i]->cease();
That's the only solution I can think of. I feel like there might be a more efficient/less hackish way? Obviously if this way works then it works but... if you guys can think of anything else I'd be glad to hear it.

Thanks!
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: Bullet targeting an Enemy

Post by K-Bal »

The best way I can think of is giving each bullet a pointer to their target (which could also be used for calculating the target vector) and each frame they ask the target if it is still alive. If not they let themselfs vanish.

I would not just let them disappear, let them fly in the last given direction until they collide with something else ;)
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Bullet targeting an Enemy

Post by MarauderIIC »

Duncan wrote: The only solution I could think of would be to give each Enemy a std::vector of Bullet pointers and then when each bullet is assigned a target have that Push its own "this" pointer into the Enemy's vector... and THEN in the Enemy destructor do something like

Code: Select all

for (int i=0; i < followers.size(); i++)
  followers[i]->cease();
This would also work. It'd be more of an event-driven thing, than a polling thing, which is what K-Bal's suggestion was. Event-driven is better than polling from a CPU usage suggestion, but not necessarily from an organizational one. Personally, I sort of like the event-driven way. Although you should use an accessor, really.

You should do the loop this way:

Code: Select all

for (vector<Bullet*>::iterator i = followers.begin(); i != followers.end(); ++i)
    delete *i; //or i->cease(); if you prefer. Syntax might be (*i)->cease, though.
size() is "slow", so you shouldn't use it on every loop iteration. If you wanted to use size, you should save it first:

Code: Select all

unsigned int vecSize = followers.size();
for (int i = 0;i < vecSize; ++i)
    followers[i]->cease();
iterators is "faster" than the [] operator, so you should use the iterator method above, if you can.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Bullet targeting an Enemy

Post by dandymcgee »

That's an interesting approach. Personally I would have most likely done it K-Bal's way, but only because I would've never thought of your idea. ;)
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
Bakkon
Chaos Rift Junior
Chaos Rift Junior
Posts: 384
Joined: Wed May 20, 2009 2:38 pm
Programming Language of Choice: C++
Location: Indiana

Re: Bullet targeting an Enemy

Post by Bakkon »

The way I did it for the homing missiles in Ace of Space was each entity held an std::vector point to what it could collide with. During the missiles' update, it would check to see if its target pointer was null or dead, if it wasn't, it would call a method that picked a random object from its collision list.
internetfx
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 11
Joined: Mon Sep 28, 2009 1:11 am

Re: Bullet targeting an Enemy

Post by internetfx »

dandymcgee wrote:That's an interesting approach. Personally I would have most likely done it K-Bal's way, but only because I would've never thought of your idea. ;)
I think the answer involves what happens to this entity that is destroyed. If it is going to be deleted, you don't want any pointers to it hanging around, so storing a linked list of the missiles or players that are targeting it is a simple sort of event-driven model to prevent this from happening and to allow immediate clean-up.

On the other hand, if the programmer wants to persist with running checks and not store the data about what is targeting the entity in the entity itself (per K-Bal), then to be safe from dangling pointer references, the entity could have only a reference count that is updated when another object targets or de-targets it. Even if it is removed from the world, it cannot be deleted from memory until that reference count reaches zero, which would likely happen after the next game logic cycle. Suddenly I smell a garbage collection list, but I am not afraid! On to battle!

Every game designer runs into two-way reference and deletion issues here and there, but the best answer lies in the game engine's model. Most often, you do the simplest thing first until necessity forces you to do otherwise.
Post Reply