Template for multiple object?

i`m making a game in c++ (+allegro), and im gonna make a template for a object, lets say a "bullet". now this "bullet" should have its own varibles(x, y , dir ....) and its own functions.This can be done with a simple class, BUT i want to have multiple objects with there own varibles.I know one: making a 2d array:
x = id
y = varible

bX bY bDIR
ID 0 [] [] []
ID 1 [] [] []
ID 2 [] [] []
ID 3 [] [] []

but i`ll like to know if there is anyway else i can do this differently.
You would make the class, then either store the individual bullets in an array or a vector. There is absolutely no need for a 2d array. I would suggest using a vector for it. If there ever ends up being a considerable amount of bullets that need attention, you are going to need some function to first go down the list and update all their values, then a second to go down and draw them to the screen.

1
2
3
4
5
6
struct Bullet
{
         float x, y, Direction;
}

vector<Bullet> MetalStorm;


You would use the vectors functions to add a Bullet to MetalStorm, and the when it comes time to use it you could either MetalStorm[i] or use another one of the functions. Use iterators to move down the entire list, and update/output each bullet of MetalStorm in order.
it can indeed be done with a simple class, that's actually exactly what you described:
have multiple objects with there own varibles
___________________________*their_____ *variables ;)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
struct position{
float x,y;
}

class bullet{
protected:
float x,y;
float xvel,yvel;
public:

position getPos();
void setPos(float nx, float ny);

position addVel(float deltax,float deltay);
void jmpVel(float deltax,float deltay);

void shoot(float origx, float origy, float xspeed, float yspeed);

void updatePos();
};

///in your main
///you can make an array or a vector of your new objects

vector<bullet> v_bullets;
v_bullets.push_back(new bullet);
v_bullets.back()->shoot(somex, somey, somexvel, someyvel);
i get it so kinda like this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
int countBullet = 0;
struct Bullet
{
int ID;
float x , y , Direction;
}
vector<Bullet> MetalStorm;

void addBullet()
{
Bullet b;
b.x = 10;
MetalStorm.push_back(b);         
MetalStorm.at(index).ID = index;
countBullet++;
}

void moveBullet()
{
int i;
for(i = 0; i<countBullet;i++)
{
MetalStorm[i].x++;
}
}
I suppose you could do that. But if you're going to follow that route, I would make another class called gun with your vector of bullets, because that is a titch bit messy. If you need an example just ask.
I just want to drop my two cents in here. In modern video games bullets are NEVER their own objects like characters are. Imagine what that would be like in a game like Modern Warfare, "He shot 5 rounds at him while he shot 4 rounds back at him and that third guy was in view shooting...", "He was hit here by this guy with that gun at the same time that guy hit him here with this gun...", "Two bullets occupied the same space! Do we ignore it? Yes ok moving on... Two more bullets occupied a different space! Do we ignore it? Yes ok Moving on..." Ad Infinum. Also rendering a bullet like this would bring collision into the mix and you really don't want that mess.

The trick seems to be that when you tell the game to 'fire', it plays and animation that shows the bullet moving overlaid on the scene. "Collision" with an object or a target is faked simply by taking the position of the crosshair and detemining what you were aiming at when you sent the 'fire' command. This is why when you are "Leading a Target" you use the same spot on the crosshair when it is 50 feet away as you do when they are 500 feet away. Rockets in most games seem to be a different story and in that case you would have to use the method that you are doing now.
Indeed...however this is not a complicated 3d environment (i'm assuming)
how would you suggest implementing that?

edit: I actually like that quite a bit.
Last edited on
:D Thank you, I wish I could take credit for it.

I would do it basically the same way, you already have the position of the player and the target, you determine if it would be a hit THEN you play the animation. If it was a hit you return where to the animation and use that as the end point. if not you allow it to play through to the edge of the screen.
Last edited on
Hmm I'm going to implement that in my next game. Thanks =)
hmmm.. you could take the distance to the vanishing point(draw a line thrugh crossair starting from the origen and ending at either where the bullet "die out" or where it dissaper from the room, find the length of that line and the divide it by the bullet speed. now we have the step length(S))then find out what the angel(DIR) is of that line. now do a line collision test between the current bullet position and the bullet positon + S amount of pixel in the direction of DIR.
to slow?
Could be feasible, but if the point is optimization it maaay be a slight bit too slow... I was thinking if you have to calculate the position, you would do a small calculation per frame, instead of a large single calculation. However I think that would be a perfect system for a 2d game!
Last edited on
@ComputerGeek: No one ever said it was an fps (from the OP's third response it sounds like it is tho, despite him only showing an x and a y coordinate hinting otherwise). When OP presented the problem, I immediately thought of something like metal slug or contra, in which case each bullet WOULD be its own object.
meh two different implementations, a million possibilities, it's entirely based on what his needs are.
im only going for 2d. at least right know :P
a little note im not completely new at programming i have tryed out many languades. but on the simple console part.
i programmend in game maker in a while the logic of programming is near me :P
Awesome, why did you choose allegro? Have you looked at sfml or sdl?
Oh and what kind of game are you making?
hmm i havent looket at sfml. but sdl i guess im just to lazy to write more code.
*checking out sfml..
yeah im to lazy :p
i`ll start with allegro migth go on the sfml or sdl when i fell confedent with allegro.
cout << "sorry for my bad english, im dane :P\n";
Last edited on
No problem. I've spent time in all three. I would highly recommend SFML, it's pretty much as easy as it gets.
SFML for the win. Also I would like to add that the SDL wiki might as well not exist.
kk :p i`ll try and code with it :D
**sfml.. super fml?
Last edited on
Topic archived. No new replies allowed.