Jump to content


How do i implement bullets?

c++

18 replies to this topic

#1 TTTNL

    Member

  • Members
  • PipPip
  • 47 posts

Posted 22 February 2013 - 09:31 PM

My simple demo game now consist of one moving player and multple enemies coming from the bottom of the screen that you have to avoid. I want to add bullets to my game but i don't really know how to make them spawn and have them keep moving when the player is not holding the fire button. I'm trying to do something with a class but when you press the fire button a new object should be made so it would need to be done with a vector because an array is fixed in size. And when having a function to move the bullets it need to be applied to all of the bullets.

Could someone explain in (pseudocode) how i go around classes & vectors and spawning & moving of multiple bullets with vectors? I'm out of ideas. I am using the template from the "introduction to c with game development" tutorial in.

#2 rouncer

    Senior Member

  • Members
  • PipPipPipPip
  • 2722 posts

Posted 23 February 2013 - 09:00 AM

you need an x and y variable, and an "active" boolean for an array of possible onscreen bullets.

when you want to spawn a bullet, find the first active=false slot, and set x and y to be the player location and make active=true.

each frame, run through the bullet array, and if they are active decrement their y coordinate, if y<0 then active=false, kill the bullet.

thatll give you bullets going upwards.
you used to be able to fit a game on a disk, then you used to be able to fit a game on a cd, then you used to be able to fit a game on a dvd, now you can barely fit one on your harddrive.

#3 Stainless

    Member

  • Members
  • PipPipPipPip
  • 578 posts
  • LocationSouthampton

Posted 23 February 2013 - 09:52 AM

bullets are very simple

You need a container to hold them in, if you are using c++ then the best is probably a vector. There are plenty of tutorials on using vectors

The sequence goes like this.

1) Add bullet to vector
2) Every pass ....
3) Move bullet
4) Check to see if the bullet has hit anything
5) if it has.
6) delete bullet, damage bad guy
7) Check to see if bullet has gone off screen
8) If it has.
9) delete bullet


That applies regardless of the type of game, 2d 3d first person third person whatever, the principle is the same

#4 TTTNL

    Member

  • Members
  • PipPip
  • 47 posts

Posted 23 February 2013 - 11:44 AM

When i try to make a vector in the template like this:

vector<bulletClass> bullets;

I get a red line underneath vector and when i hover over it it says: "Error vector is not a template", and that's with or without #include <vector>

#5 rouncer

    Senior Member

  • Members
  • PipPipPipPip
  • 2722 posts

Posted 23 February 2013 - 03:36 PM

haha when i first saw this question it amused me, i remember when i first needed to ask myself "now how do i do a bullet" when i was 8-9 years old. the one concept i came up with as the solution was "ah yes, i have to remember the positions of all my objects on the screen" thats pretty much the eureka moment.

spawning bullets is a eureka moment for a beginner programmer, so my heart goes out to you. :)
you used to be able to fit a game on a disk, then you used to be able to fit a game on a cd, then you used to be able to fit a game on a dvd, now you can barely fit one on your harddrive.

#6 fireside

    Senior Member

  • Members
  • PipPipPipPip
  • 1586 posts

Posted 23 February 2013 - 04:49 PM

You will need to bone up on a little math at this point. A vector is a way of describing position and direction in 3d space. If you use a coordinate system, with the gun at point zero, then you can describe the angle and location where the bullet will be on the next frame and each next frame will use that same formula by using the same angle and length of a vector on the last position of the bullet, since the bullet is going at a fixed speed. That formula comes from pythagorean theorum of a right triangle. You can always form a right triangle with the beginning point at the center, using x, and y coordinates, so your speed of the bullet is the length of the vector in space. In 3d, it gets a little more complicated because of the extra plane, but same principle.
here's a vector math tutorial:
http://chortle.ccsu....orIndex.html#05
or you may just want to use pythagorean theorum:
http://en.wikipedia....agorean_theorem if it's 2d. The speed of the bullet represents the hypotenuse of the right triangle. If you know that and the angle, you can determine the other sides of the triangle which are the x and y distances. You would be assigning the angle based on the direction of the gun.
Currently using Blender and Unity.

#7 TTTNL

    Member

  • Members
  • PipPip
  • 47 posts

Posted 23 February 2013 - 09:37 PM

View Postfireside, on 23 February 2013 - 04:49 PM, said:

You will need to bone up on a little math at this point. A vector is a way of describing position and direction in 3d space. If you use a coordinate system, with the gun at point zero, then you can describe the angle and location where the bullet will be on the next frame and each next frame will use that same formula by using the same angle and length of a vector on the last position of the bullet, since the bullet is going at a fixed speed. That formula comes from pythagorean theorum of a right triangle. You can always form a right triangle with the beginning point at the center, using x, and y coordinates, so your speed of the bullet is the length of the vector in space. In 3d, it gets a little more complicated because of the extra plane, but same principle.
here's a vector math tutorial:
http://chortle.ccsu....orIndex.html#05
or you may just want to use pythagorean theorum:
http://en.wikipedia....agorean_theorem if it's 2d. The speed of the bullet represents the hypotenuse of the right triangle. If you know that and the angle, you can determine the other sides of the triangle which are the x and y distances. You would be assigning the angle based on the direction of the gun.

I'm not talking about a vector as in "line which the bullet must follow". I'm talking about the storage containter vector used in C++ to store data in. I can't seem to make one using the template from the tutorials and i want to figure out how i can.

It's a simple 2d game/demo, i have to add an "object spawner", when i press space it must spawn a bullet that travels downwards, nothing fancy with direction of the gun. And i need a vector to store those bullet objects in but when i try to make a vector i get an error (see previous comment).

#8 fireside

    Senior Member

  • Members
  • PipPipPipPip
  • 1586 posts

Posted 24 February 2013 - 12:06 AM

Quote

I'm not talking about a vector as in "line which the bullet must follow". I'm talking about the storage containter vector used in C++ to store data in. I can't seem to make one using the template from the tutorials and i want to figure out how i can.


Oh, never mind.


try
std::vector<whatever> bullets;

with the include
Currently using Blender and Unity.

#9 Stainless

    Member

  • Members
  • PipPipPipPip
  • 578 posts
  • LocationSouthampton

Posted 24 February 2013 - 10:13 AM

std::vector<BulletClass * > bullets;

to add a bullet

bullets.push_back(new BulletClass());

To iterate them
for (int i=0; i<bullets.size(); i++)
{
       BulletClass * cur = bullets[i];
}


#10 TTTNL

    Member

  • Members
  • PipPip
  • 47 posts

Posted 24 February 2013 - 11:38 AM

When i add

#include <vector>

i get a whole list of errors from different files and places of the template

it's basically this for every error

1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\tmmintrin.h(81): error C2146: syntax error : missing ';' before identifier '_mm_sign_epi32'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\tmmintrin.h(81): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\tmmintrin.h(81): error C2146: syntax error : missing ')' before identifier 'a'

And this:

fatal error C1003: error count exceeds 100; stopping compilation


#11 geon

    Senior Member

  • Members
  • PipPipPipPip
  • 939 posts

Posted 24 February 2013 - 12:08 PM

Strange errors like that can be caused by errors earlier in other files.

#12 TTTNL

    Member

  • Members
  • PipPip
  • 47 posts

Posted 28 February 2013 - 02:23 PM

View PostStainless, on 24 February 2013 - 10:13 AM, said:

std::vector<BulletClass * > bullets;

to add a bullet

bullets.push_back(new BulletClass());

To iterate them
for (int i=0; i<bullets.size(); i++)
{
	   BulletClass * cur = bullets[i];
}

Ok i've got it working now, but without the * after BulletClass. that doesn't seem to be a problem.

for (int i=0; i<bullets.size(); i++)
{
  bulletClass cur = bullets[i];

  bullets[i].move();
  bullets[i].draw( m_Screen );
}

how can i remove a bullet which y coordinate is higher than 500?

Because i can't use bullets[i].delete or something similar to delete the current bullets[i] bullet ( i think )
I've tried this:

if (bullets[i].y > 500)
{
   bullets.pop_back();
}

but that will delete all of the bullets on screen and not just the last one.

and this:

if (bullets[i].y < 500)
{
bullets[i].move();
bullets[i].draw( m_Screen );
}

But this will create a wall of invisible bullets at y=500 because although they won't be drawn, y will still be 500.

Any ideas?

#13 rouncer

    Senior Member

  • Members
  • PipPipPipPip
  • 2722 posts

Posted 28 February 2013 - 02:53 PM

making them invisible is as good as deleting them, i say, as long as the vector is a fixed size.
you used to be able to fit a game on a disk, then you used to be able to fit a game on a cd, then you used to be able to fit a game on a dvd, now you can barely fit one on your harddrive.

#14 fireside

    Senior Member

  • Members
  • PipPipPipPip
  • 1586 posts

Posted 28 February 2013 - 03:43 PM

You could probably just break from the loop and then delete the first one that's off screen, chances are there will only be one. If not, you will catch up eventually.
Currently using Blender and Unity.

#15 TTTNL

    Member

  • Members
  • PipPip
  • 47 posts

Posted 28 February 2013 - 09:10 PM

View Postfireside, on 28 February 2013 - 03:43 PM, said:

You could probably just break from the loop and then delete the first one that's off screen, chances are there will only be one. If not, you will catch up eventually.

View Postrouncer, on 28 February 2013 - 02:53 PM, said:

making them invisible is as good as deleting them, i say, as long as the vector is a fixed size.
Do you mean making the position outside of the screen and outside any posibility of collision? But then after a while i would have hundreds of bullets up there and when i implement collision detection i don't think that is going to be really good for the performance. I could however implement the ability to only do collision detection for active bullets.

but isn't there a simple way i can delete the bullet that is outside the screen? Or do i have to dive into memory allocation and all that good stuff?

#16 rouncer

    Senior Member

  • Members
  • PipPipPipPip
  • 2722 posts

Posted 28 February 2013 - 09:19 PM

you could reuse the dead bullets when you spawn a new bullet.
you used to be able to fit a game on a disk, then you used to be able to fit a game on a cd, then you used to be able to fit a game on a dvd, now you can barely fit one on your harddrive.

#17 fireside

    Senior Member

  • Members
  • PipPipPipPip
  • 1586 posts

Posted 28 February 2013 - 11:22 PM

For a vector in c++ you use erase. I meant iterate through and delete one bullet that's off screen each loop.

int found = -1;
for(int i =0; i < bullets.size;i++)
{
  if(bullets[i].y<500) // move code;
  else found = i;
}
if(found > -1) bullets[found].erase;

or make it invisible or whatever.
Otherwise you could put the off screen bullets at the front and iterate at a higher starting number each time one went off screen, or at the back an stop iterating earlier. For that matter, there's about a million ways you could do it and why am I suggesting anything?
Currently using Blender and Unity.

#18 TTTNL

    Member

  • Members
  • PipPip
  • 47 posts

Posted 01 March 2013 - 08:21 AM

I've just gone with placing the bullet at x=-20 when it needs to be removed, and it only checks for collision, moves and draws the bullets when x>=0, so that shouldn't be really performance heavy. But i will check out fireside's way to delete them.

#19 Hyper

    Valued Member

  • Members
  • PipPipPip
  • 195 posts

Posted 04 April 2013 - 07:52 AM

View Postfireside, on 28 February 2013 - 11:22 PM, said:

For a vector in c++ you use erase. I meant iterate through and delete one bullet that's off screen each loop.

int found = -1;
for(int i =0; i < bullets.size;i++)
{
  if(bullets[i].y<500) // move code;
  else found = i;
}
if(found > -1) bullets[found].erase;

or make it invisible or whatever.
Otherwise you could put the off screen bullets at the front and iterate at a higher starting number each time one went off screen, or at the back an stop iterating earlier. For that matter, there's about a million ways you could do it and why am I suggesting anything?
Pardon me, but that's an odd way to solve the problem, I think! :P

This is more or less how I solved it (in English):
I created a self-contained entity (a "bullet"), and assigned that to a "gun," and assigned that to a "player."
The gun can fire a bullet (if it contains any). The bullet will update it's own position. The bullet knows when it has been "fired."
The bullet also knows when to quit updating itself (due to an 'out-of-range' issue). That's about it!

An example of this (horray YouTube) in action:


Of course: The specifics of what library (if any) that you're using don't really apply here.
I figure if you're going to use C++: Why not do it in a C++-fashion (with objects)? :)

Yes! I am implying C++ should be more object-oriented. It's a beautiful feature for solving these type problems.

From a pure coding point, this is all that exists in the code that carries any real weight:
player.at(3).gun.loadRound(99);

Player "3" is the yellow box.
“You may be disappointed if you fail, but you are doomed if you don't try.”
Beverly Sills





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users