I'm trying to use Box2D within an iPhone application. I have no problem compiling or getting it to work, but I am running into an issue when I try to tweak gravity.
Setting zero on the gravity vector does not seem to zero out gravity, as the body object will still fall as I step through the world. In fact, changing the Y on the vector doesn't seem to do anything at all. Oddly enough, changing X on the vector does have some effect, although it's just as weird - the body seems to fall "further", etc.
Here's the relevant code:
b2AABB worldAABB;
worldAABB.lowerBound.Set(-100.0f, -100.0f);
worldAABB.upperBound.Set(100.0f, 100.0f);
b2Vec2 gravity;
gravity.Set(0.0f, 0.0f);
physWorld = new b2World(worldAABB, gravity, true);
//physWorld->SetGravity(b2Vec2(0.0f, 0.0f));
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(10.0f, 10.0f);
b2Body *groundBody = physWorld->CreateBody(&groundBodyDef);
b2PolygonDef groundShapeDef;
groundShapeDef.SetAsBox(50.0f, 10.0f);
groundBody->CreateShape(&groundShapeDef);
b2BodyDef bodyDef;
bodyDef.position.Set(0.0f, 5.0f);
body = physWorld->CreateBody(&bodyDef);
b2PolygonDef shapeDef;
shapeDef.SetAsBox(1.0f, 1.0f);
//shapeDef.restitution = 0.2f;
shapeDef.density = 1.0f;
shapeDef.friction = 0.3f;
body->CreateShape(&shapeDef);
body->SetMassFromShapes();
And the stepping portion:
physWorld->Step(1.0f / 30.0f, 10.0f);
b2Vec2 position = body->GetPosition();
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [UIColor greenColor].CGColor);
CGContextSetLineWidth(context, 1.0);
CGContextAddEllipseInRect(context, CGRectMake(position.x * 40, position.y * 40, 40.0, 40.0));
CGContextStrokePath(context);
NSNumber *myAngle = [NSNumber numberWithFloat:position.y * 40];
NSLog(@"%@", myAngle);
I'm pulling my hair out over this. Any thoughts?
Setting zero on the gravity vector does not seem to zero out gravity, as the body object will still fall as I step through the world. In fact, changing the Y on the vector doesn't seem to do anything at all. Oddly enough, changing X on the vector does have some effect, although it's just as weird - the body seems to fall "further", etc.
Here's the relevant code:
Code:
b2AABB worldAABB;
worldAABB.lowerBound.Set(-100.0f, -100.0f);
worldAABB.upperBound.Set(100.0f, 100.0f);
b2Vec2 gravity;
gravity.Set(0.0f, 0.0f);
physWorld = new b2World(worldAABB, gravity, true);
//physWorld->SetGravity(b2Vec2(0.0f, 0.0f));
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(10.0f, 10.0f);
b2Body *groundBody = physWorld->CreateBody(&groundBodyDef);
b2PolygonDef groundShapeDef;
groundShapeDef.SetAsBox(50.0f, 10.0f);
groundBody->CreateShape(&groundShapeDef);
b2BodyDef bodyDef;
bodyDef.position.Set(0.0f, 5.0f);
body = physWorld->CreateBody(&bodyDef);
b2PolygonDef shapeDef;
shapeDef.SetAsBox(1.0f, 1.0f);
//shapeDef.restitution = 0.2f;
shapeDef.density = 1.0f;
shapeDef.friction = 0.3f;
body->CreateShape(&shapeDef);
body->SetMassFromShapes();
And the stepping portion:
Code:
physWorld->Step(1.0f / 30.0f, 10.0f);
b2Vec2 position = body->GetPosition();
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [UIColor greenColor].CGColor);
CGContextSetLineWidth(context, 1.0);
CGContextAddEllipseInRect(context, CGRectMake(position.x * 40, position.y * 40, 40.0, 40.0));
CGContextStrokePath(context);
NSNumber *myAngle = [NSNumber numberWithFloat:position.y * 40];
NSLog(@"%@", myAngle);
I'm pulling my hair out over this. Any thoughts?