Thread: help with vectors again :(

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    28

    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++;
    		}
    	}
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Without your classes, it's hard to tell.
    Without a driver, it's hard to test.

    Code:
    int main ( ) {
      EntityHandler npc;
      npc.shoot ( ... );
      npc.update();
      npc.shoot ( ... );
      npc.update();
      npc.update();
      npc.update();
      return 0;
    }
    Create really simple test programs around your class. Then try debugging them.

    Create a class member function called test, which exercises all the code in the class in a progressive manner (starting with the simple cases, then building up to more complex cases).
    Then in main, you can just do
    npc.test();


    Use http://msdn.microsoft.com/library/de...Debug_Heap.asp around the various calls into your class to check how much memory has been allocated, and more importantly, freed.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> i think im deleting the elements from the vector wrong
    Yes, you are. The reason is that erase() causes the elements after the erased object to move down to fill in the empty spot, but you are still incrementing your index i which will skip past one element. The solution is to decrement i if you erase from the vector.

    Another issue is that bullet is allocated with new when you add it to the vector, but when you erase it you never delete it. You need to delete the pointer when you erase it. You also should be deleting the pointers if you clear the vector and in the EntityHandler destructor. Another solution is to use a boost ptr_vector, which does the deletion for you automatically, or store a shared_ptr instead of a raw pointer in your vector.

    Finally, it doesn't hurt, but the if(!Tracers.empty()) is not necessary, since your loop will not run if the vector is empty anyway.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vectors
    By naseerhaider in forum C++ Programming
    Replies: 11
    Last Post: 05-09-2008, 08:21 AM
  2. How can i made vectors measuring program in DevC++
    By flame82 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 02:05 PM
  3. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 10:35 AM
  4. How to use Vector's in C++ !?!
    By IndioDoido in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2007, 11:13 AM
  5. Points, vectors, matrices
    By subnet_rx in forum Game Programming
    Replies: 17
    Last Post: 01-11-2002, 02:29 PM