3
\$\begingroup\$

I'm making a simple bullet hell game but I can't figure out how to get my character to shoot. Lets say I have bulletBody and shipBody, how would I continually spawn bulletBodies using the shipBody coordinates. I've tried a function that uses an array of b2bodies and just assigns them the bodydef and fixture but that causes the game to crash. C++ sample code would be best but any help is appreciated.

EDIT: It looks like any reference to my b2World in a function will cause the game to crash. How do I declare the bodies without using a b2World as an argument in the function.

\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

b2World, b2Body, etc, cannot be copied. You must pass them by pointer or reference:

void process(b2World world);  // this is bad.
void process(b2World *world); // this is fine.
void process(b2World &world); // this is probably best.

Likewise, b2Bodys must be generated from b2World::CreateBody, and cannot be copied, so must be passed by pointer or reference:

std::vector<b2Body *> bodies;
b2World world(...);
...
b2BodyDef dfn = ...;
bodies.push_back(world.CreateBody(&dfn));

It sounds like you might be doing:

std::vector<b2Body> bodies;
bodies.push_back(*world.CreateBody(&dfn));

Which will not work. The b2World instance owns the body.

The Box2D manual goes into detail, with code examples for most simple tasks.

\$\endgroup\$
0
\$\begingroup\$

but I can't figure out how to get my character to shoot

Well, lets see, i did something like this the first time i did a shooter game... You can do a bullet class with int X, int Y, bool active... then you can do an array of bullet. Then, when the player shoots, you set the bullet[0] X and Y at the same position of the sprite, the gun, whatever, and put bool active = true... when you update the state of the game, you search the bullet array, and if active, you modify X and or Y. At your draw phase, you search the bullet array again, and if activr, you draw it at bullet.X, bullet.Y. Also, at update stage, you check for collisions or check if the bullet is still inside the screen, so events are trigged like kill the guy and reset the bullet (active=false)

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .