Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

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.

share|improve this answer
add comment

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)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.