#1
Posted 22 February 2013 - 09:31 PM
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
Posted 23 February 2013 - 09:00 AM
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.
#3
Posted 23 February 2013 - 09:52 AM
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
Posted 23 February 2013 - 11:44 AM
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
Posted 23 February 2013 - 03:36 PM
spawning bullets is a eureka moment for a beginner programmer, so my heart goes out to you.
#6
Posted 23 February 2013 - 04:49 PM
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.
#7
Posted 23 February 2013 - 09:37 PM
fireside, on 23 February 2013 - 04:49 PM, said:
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
Posted 24 February 2013 - 12:06 AM
Quote
Oh, never mind.
try
std::vector<whatever> bullets;
with the include
#9
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
Posted 24 February 2013 - 11:38 AM
#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
Posted 24 February 2013 - 12:08 PM
#12
Posted 28 February 2013 - 02:23 PM
Stainless, 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
Posted 28 February 2013 - 02:53 PM
#14
Posted 28 February 2013 - 03:43 PM
#15
Posted 28 February 2013 - 09:10 PM
fireside, on 28 February 2013 - 03:43 PM, said:
rouncer, on 28 February 2013 - 02:53 PM, said:
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
Posted 28 February 2013 - 09:19 PM
#17
Posted 28 February 2013 - 11:22 PM
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?
#18
Posted 01 March 2013 - 08:21 AM
#19
Posted 04 April 2013 - 07:52 AM
fireside, on 28 February 2013 - 11:22 PM, said:
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?
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.
Beverly Sills
2 user(s) are reading this topic
0 members, 2 guests, 0 anonymous users












