help with vectors again :(
Hi again all, thanks for your help last time :)
At the moment im having trouble implimenting a singleton class that i am using to hold a list of projectiles (representing bullets fired from an NPC).
I have set up two functions, one that is called when a NPC fires a projectile (so it accepts parametres of the x,y,z (Point), yaw, pitch of the NPC when he fired the bullet). The second function is called when the whole list of projectiles needs to be updated (ie. some moved further along thier vectors, or removed from the list). I havent implimented the collision detection calls or anything yet, i really just wanna get the elements to act properly on the screen first.
I really need help on how to get the path of the vector that the bullet is travelling along, the bullet wont stop at its 'destination' point thats just so it knows what angle to travel along really, it is spose to be deleted when its ticTime reaches a certain value. But i was wondering how do i get this destination point? it really only needs to be a vector heading in the direction the NPC fired the projectile at.
Also i think im deleting the elements from the vector wrong also, but im not sure of the correct way to do it.
I will post the two functions, i would love it if anyone could give me some advice :)
thankyou,
Code:
void EntityHandler::Shoot(Point pos, float y, float p) {
Point path;
Projectile *bullet;
try {
//Create a new projectile
bullet = new Projectile;
}
catch(...) {std::cerr << "No memory to new 'Projectile'" << std::endl;}
bullet->position = pos;
bullet->yaw = y;
bullet->pitch = p;
bullet->ticTime = 0;
path = bullet->position;
path.Normalize();
//Set the projectiles end point
//bullet->destination = ???
//Add the projectile to the vector
this->Tracers.push_back(bullet);
}
void EntityHandler::Update() {
if(!Tracers.empty()) {
for(int i = 0; i < this->Tracers.size(); i++) {
if(Tracers[i]->ticTime == 20) {
//Its time to remove projectile from the vector
Tracers.erase(Tracers.begin()+i);
}
else {
Point path = Tracers[i]->destination - Tracers[i]->position;
path.Normalize();
Point step = path.scalarMultipy(static_cast<float>(BULLET_SPEED));
Tracers[i]->position = Tracers[i]->position + step;
std::cerr << "current BULLET position is: " << Tracers[i]->position << std::endl;
///
//Did this entity hit a player at its current position???
//Call collision detection here i think
///
//Parametre 1 means its an bullet to be rendered!!!
this->am->render(Tracers[i]->position, Tracers[i]->yaw, Tracers[i]->pitch, 1);
}
Tracers[i]->ticTime++;
}
}
}