Using functions with Box2D results in a crash?

In both cases it compiles fine but well this runs perfectly

Code:
#include <stdio.h>
#include <time.h>
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_mixer.h>
#include <SDL_opengl.h>
#include <Box2D.h>
#include <string>
#include <math.h>

bool quit = false;
#include "Generic\generic.h"
#include "Game\Game_main.h"
SDL_Event event;

const float PIXELS_PER_UNIT     = 100.0;
const float32 timeStep          = 1.0f / 30.0f;
const int32 iterations          = 1;
b2World *world = 0;b2AABB worldAABB;
b2Body *youbody;b2Body *citybody;

int main( int argc, char *argv[] ){
init();
Game_init();

worldAABB.lowerBound.Set(-100000,-100000);
worldAABB.upperBound.Set(100000,100000);
b2Vec2 gravity(0,0);bool doSleep = true;world = new b2World(worldAABB,gravity,doSleep);

b2BodyDef youbodydef;
youbodydef.position.Set((float32)youx/PIXELS_PER_UNIT,(float32)youy/PIXELS_PER_UNIT);
youbodydef.linearDamping=4.5;
youbodydef.angularDamping=4.5;
youbodydef.allowSleep = false;
b2Body* youbody = world->CreateBody(&youbodydef);
b2CircleDef youShapeDef;
youShapeDef.radius = 17/PIXELS_PER_UNIT;
youShapeDef.density = 6;
youShapeDef.friction = 0;
youbody->CreateShape(&youShapeDef);
youbody->SetMassFromShapes();

Timer fps;

while( quit == false ){

fps.start();
Game_run();

if (up+down+left+right>0){
float32 x=youbody->GetLinearVelocity().x+(float32)(sin(moveangle*PIover180)*walkspeed);
float32 y=youbody->GetLinearVelocity().y+(float32)(cos(moveangle*PIover180)*walkspeed);
youbody->SetLinearVelocity(b2Vec2(x,y));}
youx=youbody->GetPosition().x*PIXELS_PER_UNIT;
youy=youbody->GetPosition().y*PIXELS_PER_UNIT;
world->Step(timeStep,iterations);

while( SDL_PollEvent( &event ) ){if( event.type == SDL_QUIT ){quit = true;}}
SDL_GL_SwapBuffers();
if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND ){SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );}

}SDL_Quit();return 0;}


this results in the game crashing.

Code:
#include <stdio.h>
#include <time.h>
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_mixer.h>
#include <SDL_opengl.h>
#include <Box2D.h>
#include <string>
#include <math.h>

bool quit = false;
#include "Generic\generic.h"
#include "Game\Game_main.h"
SDL_Event event;

const float PIXELS_PER_UNIT     = 100.0;
const float32 timeStep          = 1.0f / 30.0f;
const int32 iterations          = 1;
b2World *world = 0;b2AABB worldAABB;
b2Body *youbody;b2Body *citybody;

void box2dblah(){
if (up+down+left+right>0){
float32 x=youbody->GetLinearVelocity().x+(float32)(sin(moveangle*PIover180)*walkspeed);
float32 y=youbody->GetLinearVelocity().y+(float32)(cos(moveangle*PIover180)*walkspeed);
youbody->SetLinearVelocity(b2Vec2(x,y));}
youx=youbody->GetPosition().x*PIXELS_PER_UNIT;
youy=youbody->GetPosition().y*PIXELS_PER_UNIT;
world->Step(timeStep,iterations);}

int main( int argc, char *argv[] ){
init();
Game_init();

worldAABB.lowerBound.Set(-100000,-100000);
worldAABB.upperBound.Set(100000,100000);
b2Vec2 gravity(0,0);bool doSleep = true;world = new b2World(worldAABB,gravity,doSleep);

b2BodyDef youbodydef;
youbodydef.position.Set((float32)youx/PIXELS_PER_UNIT,(float32)youy/PIXELS_PER_UNIT);
youbodydef.linearDamping=4.5;
youbodydef.angularDamping=4.5;
youbodydef.allowSleep = false;
b2Body* youbody = world->CreateBody(&youbodydef);
b2CircleDef youShapeDef;
youShapeDef.radius = 17/PIXELS_PER_UNIT;
youShapeDef.density = 6;
youShapeDef.friction = 0;
youbody->CreateShape(&youShapeDef);
youbody->SetMassFromShapes();

Timer fps;

while( quit == false ){

fps.start();
Game_run();

box2dblah();

while( SDL_PollEvent( &event ) ){if( event.type == SDL_QUIT ){quit = true;}}
SDL_GL_SwapBuffers();
if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND ){SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );}

}SDL_Quit();return 0;}


Does anybody know what I'm doing wrong and how to correct it?
yep:

this line is the culprit
Code:
b2Body* youbody = world->CreateBody(&youbodydef);


Essentially, when you call this line in main(), it creates a local variable named "youbody" and for the rest of main() it refers to that variable every time you type "youbody". However, when it runs the external function it looks at the global "youbody" again which hasn't been set which is a very consistent way to get a program to crash completely.

The replacement line would be
Code:
youbody = world->CreateBody(&youbodydef);


but a better solution is to learn to code properly, which means no global variables, one statement per line, indent stuff please (maybe you do and that's just the forums, dunno) and a fair few other things. 😛
But indentations use up horizontal screen space and I have an admittedly somewhat illogical hatred for needing to and/or risking the need to use horizontal scroll bars when reading text! 😳 lol yeah I know I'm doing a lot of stuff wrong. I've never claimed to be a particularly good programmer. But anyways thanks a lot for explaining that specific mistake. 😀
ThatOtherPerson wrote:
But indentations use up horizontal screen space and I have an admittedly somewhat illogical hatred for needing to and/or risking the need to use horizontal scroll bars when reading text!

Yes, not indenting your code to conserve horizontal space on your monitor is illogical :)

*You* might be able to read it, but not indenting your code will make it a nightmare for anyone else to look at. So, if you want to be able to get help on forums such as this one, you should indent (and that alone should probably be reason enough to do so).

If you're worried about long lines, you can a) use 2 spaces (rather than, say, 4 or 8) for indentation, and b) limit your line lengths. For example, in my own code I clamp my line lengths to about 80 characters (which probably isn't really necessary, but works well for my monitor setup), and I have no problem doing this and *also* indenting my code properly. Furthermore, if you have lines that are so long that they run off the edge of the screen if you indent a few spaces, you're probably using expressions that are too complex, or just trying to cram too much into one statement. And lastly, if you're running into problems because you have many levels of indentation, then you should arrange your code differently (multiple levels of indentation is often a sign that some refactoring is in order).

Anyway, just some things to consider :)

Original thread: https://box2d.org/forum/viewtopic.php?t=4437