Sign up ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

so just to be more specific this question isn't in regards to how to target enemies or anything like that rather its purposed towards a specific targeting issue i have with me enemies in its current state.

So the issue im having is that if i fire a bullet towards an enemy and then before that bullet hits the desired enemy it was initially set too, if my tower changes its target then suddenly my bullet does a u-turn and then targets this new enemy. Which isn't what i want.

I'm using C++. I know this is a pointer issue, that my pointer's memory address changes to point at the new enemy which the tower would be facing, the problem is i just don't know how to solve this, personally for me i don't use pointers that much and unfortunately im just not very good at using them it seems :/. Any help on this issue is greatly appreciated, its been annoying me now for the better part of a few hours, and if anyone could explain what I'm doing wrong here as-well that would be great! So here's my targeting code

for (int numOfEnemies = 0; numOfEnemies < lib.numberOfEnemies; numOfEnemies++)
    float y =  pow(enemies[numOfEnemies].position.y - position.y, 2);
    float x = pow(enemies[numOfEnemies].position.x - position.x, 2);

    if (sqrt(y + x) < range && enemies[numOfEnemies].alive)
        cEnemy =  &enemies[numOfEnemies];
        acquiredTarget = true;

Simple enough, then this is how i'm passing it in my Projectile class;

bullets.push_back(Projectile(bulletPosX, bulletPosY, 8, 8, damage, 1, speed, *cEnemy, currentUpgradeLevel));

Which specifically then uses this constructor;

Projectile::Projectile(float x, float y, int w, int h, int dmg, int type, float mxSpeed, Enemies& bulletTarget, int towerLevel)
     position.x = x;
     position.y = y;
     width = w;
     height = h;
     bulletAngle = 0;
     renderingAngle = 0;
     damage = dmg;
     active = true;
     ySpeed = 0;
     xSpeed = 0;
     maxSpeed = mxSpeed;
     typeOfProjectile = type;
     target = &bulletTarget;

There you see target is then a pointer to an enemy address, the cEnemy (currentEnemy). What my desired effect would be to have my Projectile store the enemy that I pass into it which then until it hits that enemy, the targeted enemy will forever stay focused on the enemy passed in when the projectile was first created.

Again any help greatly appreciated!

For reference

Enemies = Class Name. enemies = vector array of active Enemies.

cEnemy = (Pointer to Enemies Object)/Enemies* cEnemy.

share|improve this question
What is definition of Enemies and enemies? – kravemir Jul 14 '12 at 13:35
The question requires more forum thread. It's not formed for StackExchange question. You are talking about existing wrong code and there is no general use. – kravemir Jul 14 '12 at 13:39
Enemies = Class Name. enemies = vector array of active Enemies. – dan369 Jul 14 '12 at 13:51

2 Answers 2

up vote 0 down vote accepted

Does it have to be a pointer to an enemy?

If it really does, make a copy of your enemy, and make a new pointer to that. Pass the pointer to the copy and that should resolve your issue.

If it doesn't? Then pass a copy of the target and store the copy of your tower's enemy in your projectile object.

share|improve this answer
In terms of making a copy of the enemy, do you mean i should make a copy of cEnemy? I am a tiny bit confused by the copying part. – dan369 Jul 14 '12 at 13:09
for example would this be correct? Enemies& newEnemy = *cEnemy; bullets.push_back(Projectile(bulletPosX, bulletPosY, 8, 8, damage, 1, speed, newEnemy, currentUpgradeLevel)); – dan369 Jul 14 '12 at 13:16
Well the truth is that I haven't done much C++, and it's been awhile since I've done anything with it. nekome's answer gets around the issue in that you're making a new target. This is probably a better question for stack exchange incidentally. I was a little hesitant about saying anything frankly, but I wanted to point (ha ha) you in the right direction. – xuincherguixe Jul 15 '12 at 0:33
Well not the specific answer to my problem, you did however despite the irony of it all point my in the right direction :P. I got it solved now, it was because i was removing the enemies from the vector array which when a faster enemy overtook a slower one, thats when things started messing up. Thanks again :D! – dan369 Jul 15 '12 at 2:43

I think it would be best if you use vector to represent projectile direction. So inside your Projectile constructor you can have:

    directionVec = Util::getVector(Point(x, y), bulletTarget.getLocationPoint());

and getVector can be some static method inside some Util class which returns Vector when given start and end point.

Using vector to store your direction is convenient because you can add it to the current location and thus advance projectile. Scalar Multiplying of normalized vector then adding it to current location point gives you desired advance step.

share|improve this answer
Unless bullets are supposed to follow the enemy (like a heat-seeker missile), this is the way to go. – bummzack Jul 14 '12 at 8:37

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.