Page 1 of 1

Programming/Drawing Projectiles

Posted: Tue Jun 09, 2009 6:15 pm
by hurstshifter
Hey Everyone,

So I'm at work on a Space Invaders clone and I'm at the point where I'd like to start implementing the projectiles(bullets). I've never done something like this before and was hoping I could get a few ideas as how to go about it. I'm not entirely sure if I should store the bullets in a class all their own or just have shoot() be a function in the player class. My biggest problem is wrapping my head around how to create an object and then have it move at a certain speed in one direction each frame until it either a.)hits a block or b.) exits the screen. I setup another keystate in my player::playerinput() function so it checks for the spacebar and then draws a bullet directly in front of the player character, but I'm stuck here. I'll be working on this tonight so I'll post my code if I get anywhere with this.

Re: Programming/Drawing Projectiles

Posted: Tue Jun 09, 2009 8:15 pm
by MarauderIIC
Projectiles should be their own class. Implement a velocity member and a Projectile::update() function, which is called every frame (either by iterating through a list of projectiles, or whatever). The Projectile::update() function increments the position by the velocity. The function that calls Projectile::update() would have to check to see if the position is out of the screen and delete the projectile instance. Or Projectile can be a derived class of like a MovingObject function so you can iterate through a list of pointers to "MovingObject"s and increment the position in this function from the MovingObject's velocity member and check the new position and delete it if necessary.

So:
1) update() is a member function of moving object and every moving object updates its own position, and position is checked in the same engine loop that calls update() for every moving object, and if the position is outside the bounds, it is deleted.
2) update() is a member function and the class updates its own position and position is checked for removal in the class and a flag is set, an engine::cleanup() function is called that iterates through every object and checks for this removal flag and deletes it if necessary.
2) update() is a function in the engine class that accesses every moving object's velocity and position and changes the position accordingly, if the position is outside the bounds, it is deleted.

Essentially it's a player that moves itself and checks its own collision and that the engine removes (it needs draw and move and such, like a player does -- so you might consider deriving Player and Projectile from a common class).

You might skim this http://elysianshadows.com/phpBB3/viewto ... f=6&t=3647

Re: Programming/Drawing Projectiles

Posted: Tue Jun 09, 2009 8:26 pm
by hurstshifter
MarauderIIC wrote:Projectiles should be their own class. Implement a velocity member and a Projectile::update() function, which is called every frame (either by iterating through a list of projectiles, or whatever). The Projectile::update() function increments the position by the velocity. The function that calls Projectile::update() would have to check to see if the position is out of the screen and delete the projectile instance. Or Projectile can be a derived class of like a MovingObject function so you can iterate through a list of pointers to "MovingObject"s and increment the position in this function from the MovingObject's velocity member and check the new position and delete it if necessary.

So:
1) update() is a member function of moving object and every moving object updates its own position, and position is checked in the same engine loop that calls update() for every moving object, and if the position is outside the bounds, it is deleted.
2) update() is a member function and the class updates its own position and position is checked for removal in the class and a flag is set, an engine::cleanup() function is called that iterates through every object and checks for this removal flag and deletes it if necessary.
2) update() is a function in the engine class that accesses every moving object's velocity and position and changes the position accordingly, if the position is outside the bounds, it is deleted.

Essentially it's a player that moves itself and checks its own collision and that the engine removes (it needs draw and move and such, like a player does -- so you might consider deriving Player and Projectile from a common class).

You might skim this http://elysianshadows.com/phpBB3/viewto ... f=6&t=3647

Excellent. Wish I would have read this before I started implementing the projectile functions directly into my player class. I was all excited that I got the player to shoot, the projectile to continue on its course, and then remove itself when it reached the end of the screen. But, I then found out the hard way that calling my killblock() function from another class was not going to work. Looks like the projectiles are definitely going to get their own class. Thanks marauder.